简体   繁体   中英

I want to use Connection Pooling but my java application is not a servlet

Part of my project is to enhance the thread-safety part of my application. I want to be able to store and retrieve data from a mysql database through JDBC Connector/J and I know that I need to use connection pooling for this but my application is not a servlet ... Should I still install Tomcat and change the config.xml file for the connection pooling datasource, connection numbers etc...?

You don't need a servlet or webapp to use db connection pooling. I'm sure there are a plenty of pools to use, my default is apache dbcp http://commons.apache.org/dbcp/

To use dbcp you need to have the commons-dbcp-1.4.jar (for version 1.4) and the commons-pool (http://commons.apache.org/pool/) in your classpath. A simple way to use pooling, is to use the org.apache.commons.dbcp.BasicDataSource

BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUsername(username);
ds.setPassword(password);
ds.setUrl(jdbcUrl);
ds.setInitialSize(4);

Then, you can get connections out of the pool by calling ds.getConnection() . Furthermore, you need to configure the maximum count of active connections, have a look at the BasicDataSource API .

You can use c3p0 instead.

You can find the entire documentation here : c3p0 - JDBC3 Connection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM