Home >Java >javaTutorial >How to Establish a JDBC Connection Pool: C3P0, Application Servers, or javax.sql/java.sql?

How to Establish a JDBC Connection Pool: C3P0, Application Servers, or javax.sql/java.sql?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-15 08:19:10680browse

How to Establish a JDBC Connection Pool: C3P0, Application Servers, or javax.sql/java.sql?

JDBC Connection Pool Establishment

The creation of a connection pool using JDBC can be approached through various methods. For standalone applications, C3P0 is a reliable option. Its simplicity is demonstrated below:

ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("org.postgresql.Driver");
cpds.setJdbcUrl("jdbc:postgresql://localhost/testdb");
cpds.setUser("swaldman");
cpds.setPassword("test-password");

cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);

Alternatively, if utilizing an application server, it is advisable to leverage its built-in connection pool. This requires configuration and retrieval of the DataSource via JNDI:

DataSource ds = (DataSource) new InitialContext().lookup("jdbc/myDS");

Why Not javax.sql or java.sql?

Although javax.sql and java.sql provide pooled connection implementations, they offer advantages within specific contexts:

  • javax.sql's javax.sql.DataSource is a generic interface for accessing data sources, including connection pools. It enables the use of connection pooling across different JDBC drivers.
  • java.sql's javax.sql.PooledConnection is an interface that represents a pooled connection. It supports connection pooling in a vendor-specific manner.

However, for a comprehensive and flexible solution, third-party connection pools like C3P0 or DBCP are preferred, as they provide extensive configuration options and optimized performance under heavy loads.

The above is the detailed content of How to Establish a JDBC Connection Pool: C3P0, Application Servers, or javax.sql/java.sql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn