Java 如何在JDBC中建立连接池?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2835090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to establish a connection pool in JDBC?
提问by llm
Can anybody provide examples or links on how to establish a JDBC connection pool?
任何人都可以提供有关如何建立 JDBC 连接池的示例或链接吗?
From searching google I see many different ways of doing this and it is rather confusing.
通过搜索谷歌,我看到了许多不同的方法,这很令人困惑。
Ultimately I need the code to return a java.sql.Connection
object, but I am having trouble getting started..any suggestions welcome.
最终我需要代码来返回一个java.sql.Connection
对象,但我在开始时遇到了麻烦......欢迎提出任何建议。
Update:Doesn't javax.sql
or java.sql
have pooled connection implementations? Why wouldn't it be best to use these?
更新:没有javax.sql
或java.sql
有池连接实现?为什么不最好使用这些?
回答by Alexander Pogrebnyak
Don't reinvent the wheel.
不要重新发明轮子。
Try one of the readily available 3rd party components:
尝试使用现成的第 3 方组件之一:
- Apache DBCP- This one is used internally by Tomcat, and by yours truly.
- c3p0
- Apache DBCP- 这个由 Tomcat 内部使用,你的真正使用。
- c3p0
Apache DBCP comes with different example on how to setup a pooling javax.sql.DataSource. Here is one samplethat can help you get started.
Apache DBCP 附带了关于如何设置池javax.sql.DataSource 的不同示例。这是一个可以帮助您入门的示例。
回答by sblundy
回答by Eric Hauser
I would recommend using the commons-dbcplibrary. There are numerous exampleslisted on how to use it, here is the link to the move simple one. The usage is very simple:
我建议使用commons-dbcp库。列出了许多有关如何使用它的示例,这里是 move simple one的链接。用法很简单:
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver")
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setUrl(connectURI);
...
Connection conn = ds.getConnection();
You only need to create the data source once, so make sure you read the documentation if you do not know how to do that. If you are not aware of how to properly write JDBC statements so you do not leak resources, you also might want to read this Wikipediapage.
您只需要创建一次数据源,因此如果您不知道如何创建,请务必阅读文档。如果您不知道如何正确编写 JDBC 语句以便不泄漏资源,您可能还想阅读此Wikipedia页面。
回答by Tendayi Mawushe
Usually if you need a connection pool you are writing an application that runs in some managed environment, that is you are running inside an application server. If this is the case be sure to check what connection pooling facilities your application server providesbefore trying any other options.
通常,如果您需要一个连接池,您正在编写在某个托管环境中运行的应用程序,即您在应用程序服务器中运行。如果是这种情况,请确保在尝试任何其他选项之前检查您的应用程序服务器提供的连接池工具。
The out-of-the box solution will be the best integrated with the rest of the application servers facilities. If however you are not running inside an application server I would recommend the Apache Commons DBCP Component. It is widely used and provides all the basic pooling functionality most applications require.
开箱即用的解决方案将是与其余应用服务器设施的最佳集成。但是,如果您不是在应用程序服务器内运行,我会推荐Apache Commons DBCP 组件。它被广泛使用并提供大多数应用程序所需的所有基本池功能。
回答by leonbloy
As answered by others, you will probably be happy with Apache Dbcpor c3p0. Both are popular, and work fine.
正如其他人所回答的那样,您可能会对Apache Dbcp或c3p0感到满意。两者都很受欢迎,而且工作正常。
Regarding your doubt
关于你的疑惑
Doesn't javax.sql or java.sql have pooled connection implementations? Why wouldn't it be best to use these?
javax.sql 或 java.sql 没有池化连接实现吗?为什么不最好使用这些?
They don't provide implementations, rather interfaces and some support classes, only revelant to the programmers that implement third party libraries (pools or drivers). Normally you don't even look at that. Your code should deal with the connections from your pool just as they were "plain" connections, in a transparent way.
它们不提供实现,而是提供接口和一些支持类,只与实现第三方库(池或驱动程序)的程序员有关。通常你甚至不看那个。您的代码应该以透明的方式处理来自池的连接,就像它们是“普通”连接一样。
回答by Powerlord
In the app server we use where I work (Oracle Application Server 10g, as I recall), pooling is handled by the app server. We retrieve a javax.sql.DataSource
using a JNDI lookup with a javax.sql.InitialContext
.
在我工作的地方使用的应用服务器(我记得是 Oracle 应用服务器 10g),池由应用服务器处理。我们javax.sql.DataSource
使用带有javax.sql.InitialContext
.
it's done something like this
它做了这样的事情
try {
context = new InitialContext();
jdbcURL = (DataSource) context.lookup("jdbc/CachedDS");
System.out.println("Obtained Cached Data Source ");
}
catch(NamingException e)
{
System.err.println("Error looking up Data Source from Factory: "+e.getMessage());
}
(We didn't write this code, it's copied from this documentation.)
(这段代码不是我们写的,它是从这个文档中复制过来的。)
回答by Pascal Thivent
If you need a standalone connection pool, my preference goes to C3P0over DBCP(that I've mentioned in this previous answer), I just had too much problems with DBCP under heavy load. Using C3P0 is dead simple. From the documentation:
如果您需要一个独立的连接池,我更喜欢C3P0 而不是DBCP(我在之前的答案中提到过),我只是在重负载下使用 DBCP 有太多问题。使用 C3P0 非常简单。从文档:
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("swaldman");
cpds.setPassword("test-password");
// the settings below are optional -- c3p0 can work with defaults
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
// The DataSource cpds is now a fully configured and usable pooled DataSource
But if you are running inside an application server, I would recommend to use the built-in connection pool it provides. In that case, you'll need to configure it (refer to the documentation of your application server) and to retrieve a DataSource via JNDI:
但是如果您在应用程序服务器中运行,我建议使用它提供的内置连接池。在这种情况下,您需要对其进行配置(请参阅应用程序服务器的文档)并通过 JNDI 检索数据源:
DataSource ds = (DataSource) new InitialContext().lookup("jdbc/myDS");
回答by Simeon Malchev
Vibur DBCPis another library for that purpose. Several examples showing how to configure it for use with Hibernate, Spring+Hibernate, or programatically, can be found on its website: http://www.vibur.org/
Vibur DBCP是另一个用于此目的的库。可以在其网站上找到几个显示如何配置它以用于 Hibernate、Spring+Hibernate 或以编程方式使用的示例:http: //www.vibur.org/
Also, see the disclaimer here.
此外,请参阅此处的免责声明。
回答by tobijdc
回答by Matthieu
MiniConnectionPoolManager
is a one-java-file implementation, if you're looking for an embeddable solution and are not too concerned about performances (though I haven't tested it in that regard).
MiniConnectionPoolManager
是一个单 Java 文件实现,如果您正在寻找可嵌入的解决方案并且不太关心性能(尽管我还没有在这方面对其进行测试)。
It is multi-licensed EPL, LGPLand MPL.
Its documentation also gives alternatives worth checking (on top of DBCP and C3P0):
它的文档还提供了值得检查的替代方案(在 DBCP 和 C3P0 之上):