java - DataSource for standalone application - no application server

﹥>﹥吖頭↗ 提交于 2019-12-17 10:57:03

问题


I want to connect to a MySQL database from a standalone application. There is no application/web server.

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.

  1. 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.

  2. When I close the standalone application what should I do to release the connections/pool properly?

EDIT: 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.


回答1:


  1. I would advise to use a library like DBPool. It'll take a lot of headaches away from you because the developer already took care of them.
  2. To ensure proper cleanup of your connection pool, read the documentation about a JVM Shutdown hook.



回答2:


Take a look at Apache Cayenne, in doc read about DataSource and faq about connection pools

EDIT:

As a bonus you get modeler (graphical tool for database) and easy integration with Eclipse.




回答3:


Take a look at Apache DBCP and DBUtils

DBCP will give you features for connection pool etc.

DBUtils will make jdbc easier for you.

As mentioned DBPool is also one, there is BoneCP and plenty pooling tools out there.

Take a look at some samples:

  • Connection Pooling using Apache common DBCP And DBUtils
  • Using Apache Commons DBUtils and DBCP.


来源:https://stackoverflow.com/questions/15588449/java-datasource-for-standalone-application-no-application-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!