简体   繁体   English

ruby中的数据库连接池

[英]database connection pooling in ruby

I just started with Ruby and I am playing with Sinatra, but could not find a way to share database connections between requests. 我刚开始使用Ruby,我正在玩Sinatra,但找不到在请求之间共享数据库连接的方法。

I came from Java web developement and one of the basic things you have to do is to pool the database connections, so I am sure that something similar exists in Ruby, but I just can't find it. 我来自Java web developpement,你要做的一件事就是集中数据库连接,所以我确信Ruby中存在类似的东西,但我找不到它。

ActiveRecord and DataMapper offer this feature but I don't need ORM and just want to make regular SQL queries. ActiveRecord和DataMapper提供此功能,但我不需要ORM,只想进行常规SQL查询。

Is there some specific approach for Sinatra or there are general ways for all Rack-based applications? Sinatra是否有一些特定的方法,或者所有基于Rack的应用程序都有通用的方法?

To persist a connection, you need only create an instance variable (Sinatra Applications are just objects anyway) or a global variable. 要保持连接,您只需创建一个实例变量(Sinatra Applications只是对象)或全局变量。 Or a class that manages connections for you. 或者是一个为您管理连接的类。 Most Ruby database libraries I've seen are Database Adapters or just clients. 我见过的大多数Ruby数据库都是数据库适配器或只是客户端。

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

Or a global variable: 或全局变量:

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

Connection pooling is just a way to share a small number of connections across multiple threads/fibers for the lifespan of the application. 连接池只是在应用程序生命周期内跨多个线程/光纤共享少量连接的一种方法。 Java, the JVM, as far as I know doesn't share connections between processes. 就我所知,Java,JVM并不共享进程之间的连接。

However, there is a general purpose Connection Pool library for Ruby . 但是, Ruby有一个通用的连接池库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM