tomcat 7 JDBC连接池 - 每个数据库的独立池? [英] tomcat 7 JDBC connection pool - separate pool for each database?

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

问题描述

我有一个关于Tomcat 7 JDBC连接池的基本问题:是为每个单独的数据库(即URL)创建的单独池吗?或者是创建了一个保存来自任意数量的不同数据库的打开连接的单个池吗?

I have a basic question about the Tomcat 7 JDBC Connection Pool: is a separate pool created for each separate database (i.e., URL)? Or is a single pool created that holds open connections from any number of different databases?

例如,如果我通过这样做打开与数据库A和B的连接:

For example, if I open connections to databases A and B by doing this:

PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName("org.postgresql.Driver");
poolProperties.setUrl("jdbc:postgresql://myserver/db_a");
poolProperties.setInitialSize(1);
poolProperties.setMaxActive(10);
poolProperties.setMaxIdle(1);
poolProperties.setMinIdle(0);

然后这个:

PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName("org.postgresql.Driver");
poolProperties.setUrl("jdbc:postgresql://myserver/db_b");
poolProperties.setInitialSize(1);
poolProperties.setMaxActive(10);
poolProperties.setMaxIdle(1);
poolProperties.setMinIdle(0);

我刚刚创建了一个maxActive为10的池,或者两个池,每个池的maxActive为10?如果它是一个池,如果我在打开数据库B的连接时将maxActive更改为30,那么该怎么办?是第一次调用setMaxActive win,还是第二次调用覆盖,或者这是否会导致创建一个单独的池?

Have I just created one pool with a maxActive of 10, or two pools, each with a maxActive of 10? If it's one pool, what if I had changed maxActive to, say, 30 when opening the connection for database B? Does the first call to setMaxActive win, or does the second call override, or does this cause a separate pool to be created?

推荐答案

好的,我做了一些挖掘并自己弄清楚了。 (感谢tomcat-users邮件列表中的许多人!)

Okay, I did some digging and figured this out myself. (Thanks for the many kind folks on the tomcat-users mailing list!)

JB Nizet是对的:如果从Java代码创建Tomcat数据库连接池,每个DataSource字面上实例化/表示单独的连接池。这让我很惊讶;来自.NET背景,我假设Tomcat连接池将像SqlServer / ADO.NET连接池一样工作:如果使用两个相同的连接字符串来获得两个数据库连接,这些连接池将来自同一个连接池。但是,在Tomcat中,当从Java代码实例化DataSource对象时,每个新的DataSource实例都是一个全新的连接池。因此,例如,如果要在JAX-RS Web服务调用中保留这些连接池,则需要构建自己的数据库池(DataSource)缓存,将DataSource实例(每个数据库一个)放入其中,然后存储它在JAX-RS将持续跨Web服务调用的对象中。我刚刚做了这个,它工作正常。

JB Nizet is right: if you are creating Tomcat database connection pools from Java code, each DataSource you instantiate literally is/represents a separate connection pool. This was surprising to me; coming from a .NET background, I assumed the Tomcat connection pooling would work like SqlServer/ADO.NET connection pooling: if you use two identical connection strings to get two database connections, these will both come from the same connection pool. However, in Tomcat, when instantiating DataSource objects from Java code, each new DataSource instance is a whole new connection pool. So, if you want to persist these connection pools across JAX-RS web service calls, for example, you need to build your own database-pool (DataSource) cache, put the DataSource instances (one per database) into it, and store it in an object that JAX-RS will persist across web service calls. I just did this, and it is working fine.

顺便说一句,Tomcat数据库连接池确实提供类似于SqlServer / ADO.NET连接池的功能,你只需要使用用于创建DataSource实例的JNDI资源。 (在我的情况下,这不是一个选项,因为数据库是在我的应用程序中动态创建的,而JNDI定义通常是从Tomcat在启动时读取的配置文件创建的。)

btw, Tomcat database connection pooling does offer functionality similar to SqlServer/ADO.NET connection pooling, you just have to use JNDI resources to create your DataSource instances. (In my case this is not an option, since databases are created dynamically in my application, and JNDI definitions are generally created from config files that Tomcat reads at startup.)

这篇关于tomcat 7 JDBC连接池 - 每个数据库的独立池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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