ruby 中的数据库连接池 [英] database connection pooling in ruby

查看:56
本文介绍了ruby 中的数据库连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 Ruby,正在使用 Sinatra,但找不到在请求之间共享数据库连接的方法.

I just started with Ruby and I am playing with Sinatra, but could not find a way to share database connections between requests.

我来自 Java Web 开发,您必须做的一项基本工作是将数据库连接池化,所以我确信 Ruby 中存在类似的东西,但我就是找不到.

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.

ActiveRecord 和 DataMapper 提供此功能,但我不需要 ORM,只想进行常规 SQL 查询.

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

对于 Sinatra 是否有一些特定的方法,或者对于所有基于 Rack 的应用程序是否有通用方法?

Is there some specific approach for Sinatra or there are general ways for all Rack-based applications?

推荐答案

要保持连接,您只需创建一个实例变量(Sinatra 应用程序无论如何都只是对象)或一个全局变量.或者为您管理连接的类.我见过的大多数 Ruby 数据库库都是数据库适配器或只是客户端.

To persist a connection, you need only create an instance variable (Sinatra Applications are just objects anyway) or a global variable. Or a class that manages connections for you. Most Ruby database libraries I've seen are Database Adapters or just clients.

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

或者一个全局变量:

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

连接池只是一种在应用程序的生命周期内跨多个线程/光纤共享少量连接的方法.据我所知,Java、JVM 不共享进程之间的连接.

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.

但是,有一个通用的用于 Ruby 的连接池库.

这篇关于ruby 中的数据库连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆