java - 独立应用程序的DataSource - 无应用程序服务器 [英] java - DataSource for standalone application - no application server
问题描述
我想从独立应用程序连接到MySQL数据库。没有应用程式/网路伺服器。
I want to connect to a MySQL database from a standalone application. There is no application/web server.
这页面建议我应该使用DriverManager创建连接。但是,此页面建议创建连接
This page suggests that I should use DriverManager to create the connection. However this page suggests that connections created from DriverManager cannot be pooled.
但是连接池是我必须的,因为应用程序使用各种线程同时写入数据库,因此关闭/重建连接不是最佳的。
However connection pooling is a must for me because the application uses various threads to write to the database concurrently, so closing/recreating connections is not optimal.
-
我应该使用哪个DataSource实现这个目的?
我查看了 MysqlConnectionPoolDataSource 但我不知道如果它实际上实现一个连接池。我还查看了Tomcat DataSource
Which DataSource implementation should I use for this purpose? I looked at MysqlConnectionPoolDataSource but I am not sure if it actually implements a connection pool. I also looked at the Tomcat DataSource which I am sure implements a connection pool but I am not sure if it can be used outside Tomcat.
当我关闭独立的应用程序时我应该如何正确地释放连接/池?
When I close the standalone application what should I do to release the connections/pool properly?
编辑:看起来像Tomcat DataSource 可用从独立应用程序,所以我要使用它。它还有一个关闭方法,我假设它将释放连接。
It looks like the Tomcat DataSource is usable from standalone application, so I am going to use it. It also has a close method which I assume will release the connections.
推荐答案
看看Apache DBCP和DBUtils
Take a look at Apache DBCP and DBUtils
DBCP 将为您提供连接池等功能。
DBCP will give you features for connection pool etc.
DBUtils 会使jdbc更容易。
DBUtils will make jdbc easier for you.
As mentioned DBPool is also one, there is BoneCP and plenty pooling tools out there.
查看一些示例:
- Connection Pooling using Apache common DBCP And DBUtils
- Using Apache Commons DBUtils and DBCP.
这篇关于java - 独立应用程序的DataSource - 无应用程序服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!