Ruby database pooling

I just started with Ruby and I play with Sinatra, but could not find a way to exchange connections between requests.

I came from Java web developmentement, and one of the main things you should do is combine the database connections, so I'm sure there is something similar in Ruby, but I just can't find it.

ActiveRecord and DataMapper offer this feature, but I don't need ORM and just want to make regular SQL queries.

Is there any specific approach for Sinatra or are there common ways for all rack applications?

+6
source share
1 answer

To save the connection, you only need to create an instance variable (in any case, Sinatra applications are just objects) or a global variable. Or a class that manages connections for you. Most of the Ruby database libraries I've seen are database adapters or just clients.

@db = Mysql2::Client.new #... 

Or a global variable:

 $db = Mysql2::Client.new #... 

A connection pool is just a way to split a small number of connections across multiple threads / fibers by the life of the application. Java, the JVM, as far as I know, does not support communication between processes.

However, there is a common connection pool library for Ruby .

+4
source

Source: https://habr.com/ru/post/923062/


All Articles