JDBC中如何建立连接池?
任何人都可以提供有关如何建立 JDBC 连接池的示例或链接吗?
通过搜索谷歌,我看到了许多不同的方法来做到这一点,这相当令人困惑。
最终我需要代码来返回 java.sql.Connection 对象,但我在入门时遇到了困难......欢迎任何建议。
更新:javax.sql
或 java.sql
没有池化连接实现吗?为什么不最好使用这些呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您需要独立的连接池,我优先选择 C3P0 而不是 DBCP (我在此 上一个答案),我在重负载下的 DBCP 遇到了太多问题。使用 C3P0 非常简单。来自 文档:
但是如果您在应用程序服务器内运行,我建议使用它提供的内置连接池。在这种情况下,您需要配置它(请参阅应用程序服务器的文档)并通过 JNDI 检索数据源:
If you need a standalone connection pool, my preference goes to C3P0 over 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:
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:
HikariCP
它现代、快速、简单。我将它用于每个新项目。
与 C3P0 相比,我更喜欢它,不太了解其他池。
HikariCP
It's modern, it's fast, it's simple. I use it for every new project.
I prefer it a lot over C3P0, don't know the other pools too well.
通常,如果您需要连接池,那么您正在编写在某些托管环境中运行的应用程序,即在应用程序服务器内运行。如果是这种情况,请务必检查连接池是什么在尝试任何其他选项之前,应用程序服务器提供的设施。
开箱即用的解决方案将与其他应用程序服务器设施最好地集成。但是,如果您没有在应用程序服务器内运行,我会推荐 Apache Commons DBCP 组件< /强>。它被广泛使用并提供大多数应用程序所需的所有基本池功能。
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.
不要重新发明轮子。
尝试使用现成的第 3 方组件之一:
Tomcat 内部使用,以及
敬上。
Apache DBCP 附带了有关如何设置池的不同示例 javax.sql.DataSource。这是一个可以帮助您入门的示例 。
Don't reinvent the wheel.
Try one of the readily available 3rd party components:
used internally by Tomcat, and by
yours truly.
Apache DBCP comes with different example on how to setup a pooling javax.sql.DataSource. Here is one sample that can help you get started.
我建议使用 commons-dbcp 库。这里列出了许多关于如何使用它的示例是移动的链接简单的< /a>.使用方法非常简单:
您只需要创建一次数据源,因此如果您不知道如何执行此操作,请务必阅读文档。如果您不知道如何正确编写 JDBC 语句以免泄漏资源,您可能还需要阅读此 维基百科页面。
I would recommend using the commons-dbcp library. There are numerous examples listed on how to use it, here is the link to the move simple one. The usage is very simple:
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 Wikipedia page.
在我工作的地方使用的应用程序服务器(我记得是 Oracle 应用服务器 10g)中,池由应用程序服务器处理。我们检索
javax.sql.DataSource
使用带有 < 的 JNDI 查找代码>javax.sql.InitialContext
。它做了类似的事情
(我们没有编写这段代码,它是从 本文档。)
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 ajavax.sql.InitialContext
.it's done something like this
(We didn't write this code, it's copied from this documentation.)
2017 年底,Proxool、BoneCP、C3P0、DBCP 此时大多已不复存在。 HikariCP(创建于 2012 年)看起来很有前途,超越了我所知道的任何其他产品。
http://www.baeldung.com/hikaricp
Proxool 有许多问题:
- 在重负载下可能会超过最大连接数并且不会返回低于最大连接数
- 即使在连接过期后也可以设法不返回到最小连接
- 如果在 HouseKeeper 线程期间连接到数据库时遇到问题,可以锁定整个池(以及所有服务器/客户端线程)(不使用 .setQueryTimeout)
- HouseKeeper 线程在其进程具有连接池锁定的同时,请求 Prototyper 线程重新创建连接(清除),这可能会导致竞争条件/锁定。在这些方法调用中,最后一个参数在循环期间应始终为 swing:false,只有在其下方的 swing:true。
- HouseKeeper 最后只需要单个 PrototypeController 扫描,并且还有更多[上面提到的]
- HouseKeeper 线程在查看哪些连接可能过期之前检查连接测试[测试过期连接的一些风险,这些连接可能会因防火墙等数据库中的其他超时而被破坏/终止]
- 项目有未完成的代码(已定义但未执行的属性)
- 如果未定义,默认最大连接寿命为 4 小时(过多)
- 每个池的 HouseKeeper 线程每五秒运行一次(过多)
您可以修改代码并进行这些改进。但由于它创建于 2003 年,并于 2008 年更新,因此缺乏 hikaricp 等解决方案所利用的近 10 年的 java 改进。
In late 2017 Proxool, BoneCP, C3P0, DBCP are mostly defunct at this time. HikariCP (created in 2012) seems promising, blows the doors off anything else I know of.
http://www.baeldung.com/hikaricp
Proxool has a number of issues:
- Under heavy load can exceed max number of connections and not return below max
- Can manage to not return to min connections even after connections expire
- Can lock up the entire pool (and all server/client threads) if it has trouble connecting to the database during HouseKeeper thread (does not use .setQueryTimeout)
- HouseKeeper thread, while having connection pool lock for its process, requests the Prototyper thread to recreate connections (sweep) which can result in race condition/lockup. In these method calls the last parameter should always be sweep:false during the loop, only sweep:true below it.
- HouseKeeper only needs the single PrototypeController sweep at the end and has more [mentioned above]
- HouseKeeper thread checks for testing of connections before seeing what connections may be expired [some risk of testing expired connection that may be broken/terminated through other timeouts to DB in firewall, etc.]
- The project has unfinished code (properties that are defined but not acted upon)
- The Default max connection life if not defined is 4 hours (excessive)
- HouseKeeper thread runs every five seconds per pool (excessive)
You can modify the code and make these improvements. But as it was created in 2003, and updated in 2008, its lacking nearly 10 years of java improvements that solutions like hikaricp utilize.
Pool
性能
[通过重新使用相同的对象对对象数据执行任何操作] &内存
[分配和取消分配许多对象会产生大量的内存管理开销]。« 池 [
对象
池,字符串
常量池,线程
池、连接池]<字符串常量池
« 如果字符串复制在池中可用,则返回引用。
« 否则,String 对象将被添加到池中并返回引用。
示例:要验证的字符串 唯一对象< /a> 来自池。
使用 Type-4 驱动程序的连接池 使用第 3 方库[
DBCP2
,
c3p0
,Tomcat JDBC
]类型 4 - Thin 驱动程序将 JDBC 调用直接转换为供应商特定的数据库协议 Ex[Oracle - Thick、MySQL - Quora]。
wiki在连接池机制中,当加载类时,它会得到
物理 JDBC 连接
对象,并向用户提供包装的物理连接对象。PoolableConnection
是实际连接的包装器。
getConnection()
从连接中选择一个免费的包装连接objectpool 并返回它。close()
不是关闭,而是将包装的连接返回到池中。示例:在 Java 7 中使用 ~ DBCP2 连接池[
try- with-resources
]jdbc:::::
jdbc:
<代码>oracle:thin:@localhost:1521:myDBName
jdbc:
mysql
://localhost:3306/myDBName
connectionpool.properties
Web 应用程序:为了避免所有连接都关闭时出现连接问题[MySQL“wait_timeout”默认 8 小时],以便重新打开与底层数据库的连接。
您可以通过设置 testOnBorrow = true 和validationQuery= "SELECT 1" 来测试每个连接,并且不要将 autoReconnect 用于 MySQL 服务器它已被弃用。 问题< /em>
另请参阅:
Pool
performance
[By re using same object's to perform any action on Object-Data] &memory
[allocating and de-allocating many objects creates a significant memory management overhead].« Pooling [
Object
pool,String
Constant Pool,Thread
Pool, Connection pool]String Constant pool
« If String-copy is available in the Pool then returns the reference.
« Otherwise, String object is added to the pool and returns the reference.
Example: String to verify Unique Object from pool.
Connection pool using Type-4 Driver using 3rd party libraries[
DBCP2
,c3p0
,Tomcat JDBC
]Type 4 - The Thin driver converts JDBC calls directly into the vendor-specific database protocol Ex[Oracle - Thick, MySQL - Quora].
wikiIn Connection pool mechanism, when the class is loaded it get's the
physical JDBC connection
objects and provides a wrapped physical connection object to user.PoolableConnection
is a wrapper around the actual connection.getConnection()
pick one of the free wrapped-connection form the connection objectpool and returns it.close()
instead of closing it returns the wrapped-connection back to pool.Example: Using ~ DBCP2 Connection Pool with Java 7[
try-with-resources
]jdbc:<DB>:<drivertype>:<HOST>:<TCP/IP PORT>:<dataBaseName>
jdbc:
oracle
:thin:@localhost:1521:myDBName
jdbc:
mysql
://localhost:3306/myDBName
connectionpool.properties
Web Application: To avoid connection problem when all the connection's are closed[MySQL "wait_timeout" default 8 hours] in-order to reopen the connection with underlying DB.
You can do this to Test Every Connection by setting testOnBorrow = true and validationQuery= "SELECT 1" and donot use autoReconnect for MySQL server as it is deprecated. issue
See these also:
正如其他人的回答,您可能会对 Apache Dbcp 或 c3p0。两者都很受欢迎,而且效果都很好。
关于您的疑问
它们不提供实现,而是提供接口和一些支持类,仅与实现第三方库(池或驱动程序)的程序员相关。通常你甚至不会看它。您的代码应该以透明的方式处理池中的连接,就像它们是“普通”连接一样。
As answered by others, you will probably be happy with Apache Dbcp or c3p0. Both are popular, and work fine.
Regarding your doubt
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.
Vibur DBCP 是另一个用于此目的的库。可以在其网站上找到几个示例,展示如何配置它以与 Hibernate、Spring+Hibernate 或以编程方式一起使用:http ://www.vibur.org/
另外,请参阅此处的免责声明。
Vibur DBCP is 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/
Also, see the disclaimer here.
Apache Commons 有一个用于此目的的库:DBCP。除非您对池有奇怪的要求,否则我会使用库,因为它肯定比您希望的更棘手和更微妙。
Apache Commons has a library for that purpose: DBCP. Unless you have strange requirements around your pools, I'd use a library as it's bound to be trickier and more subtle than you would hope.
您应该考虑使用 UCP。
通用连接池(UCP)是一个Java连接池。它是一个功能丰富的连接池,并与 Oracle 的真正应用集群 (RAC)、ADG、DG 数据库紧密集成。
有关 UCP 的更多详细信息,请参阅此页面。
You should consider using UCP.
Universal Connection Pool (UCP) is a Java connection pool. It is a features rich connection pool and tightly integrated with Oracle's Real Application Clusters (RAC), ADG, DG databases.
Refer to this page for more details about UCP.
MiniConnectionPoolManager
是一个单 java 文件实现,如果您'正在寻找可嵌入的解决方案,并且不太关心性能(尽管我还没有在这方面进行过测试)。它是多重许可的EPL,LGPL 和 MPL。
它的文档还提供了值得检查的替代方案(在 DBCP 和 C3P0 之上):
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).It is multi-licensed EPL, LGPL and MPL.
Its documentation also gives alternatives worth checking (on top of DBCP and C3P0):