Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection Pooling with Apache DBCP

Tags:

I want to use Apache Commons DBCP to enable connection pooling in a Java Application (no container-provided DataSource in this). In many sites of the web -including Apache site- the usage of the library is based in this snippet:

BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("oracle.jdbc.driver.OracleDriver"); ds.setUsername("scott"); ds.setPassword("tiger"); ds.setUrl(connectURI);   

Then you get your DB connections through the getConnection() method. But on other sites -and Apache Site also- the Datasource instance is made through this:

ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory); ObjectPool objectPool = new GenericObjectPool(poolableConnectionFactory); PoolingDataSource dataSource = new PoolingDataSource(objectPool); 

What's the difference between them? I'm using connection pooling with BasicDataSource, or I need an instance of PoolingDataSource to work with connection pooling? Is BasicDataSource thread-safe (can I use it as a Class attribute) or I need to synchronize its access?

like image 502
Carlos Gavidia-Calderon Avatar asked Jan 22 '13 20:01

Carlos Gavidia-Calderon


People also ask

What is DBCP connection pool?

The DBCP Component Opening a connection per user can be unfeasible in a publicly-hosted Internet application where the number of simultaneous users can be very large. Accordingly, developers often wish to share a "pool" of open connections between all of the application's current users.

When should you not use connection pooling?

You reuse a prior database connection, in a new context to avoid the cost of setting up a new database connection for each request. The primary reason to avoid using database connections is that you're application's approach to solving problems isn't structured to accommodate a database connection pool.

How do I set up connection pooling?

Click Resources > JDBC > Data Sources > data_source > [Additional Properties] Connection pool properties. Click Resources > JMS->Queue connection factories-> queue_connection_factory ->[Additional Properties] Connection pool.

What does connection pooling do?

Connection pooling is a technique of creating and managing a pool of connections that are ready for use by any thread that needs them. Connection pooling can greatly increase the performance of your Java application, while reducing overall resource usage.


1 Answers

BasicDataSource is everything for basic needs. It creates internally a PoolableDataSource and an ObjectPool.

PoolableDataSource implements the DataSource interface using a provided ObjectPool. PoolingDataSource take cares of connections and ObjectPool take cares of holding and counting this object.

I would recommend using BasicDataSource. Only, If you really need something special maybe then you can use PoolingDatasource with another implementation of ObjectPool, but it will be very rare and specific.

BasicDataSource is thread-safe, but you should take care to use appropriate accessors rather than accessing protected fields directly to ensure thread-safety.

like image 85
ivi Avatar answered Sep 27 '22 17:09

ivi