Home >Java >javaTutorial >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:
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!