This article is note about learning Connection Pool.
Connection Pool
Connection pooling means that connections are reused rather than created each time a connection is requested. To facilitate connection reuse, a memory cache of database connections, called a connection pool, is maintained by a connection pooling module as a layer on top of any standard JDBC driver product.
Some open source Connection Pool
DBCP: offered by Apache, faster than c3p0, but has some bugs
C3P0: offered by open source organization. relatively slow, but with good stability. Advised by hibernate.
Proxool: an open project under sourceforge, can monitor the status of connection pool, but not so stable compared to c3p0
BoneCP: fast speed
Druid: offered by Alibaba, combine the best of BDCP, C3P0, Proxool, but speed is not sure to faster than BoneCP
<named-config name="intergalactoApp"> <!-- four base information for getting a connection --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl ">jdbc:mysql://localhost:3306/test</property> <property name="user">root</property> <property name="password">123abc</property>
<!-- basic properties to manage connection pool --> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">10</property> <property name="maxPoolSize">100</property> <property name="maxStatements">50</property> <property name="maxStatementsPerConnection">2</property>
</named-config> </c3p0-config>
acquireIncrement: when the connection is not enough, the number of connection that c3p0 will apply for to database server.
initialPoolSize: number of connection when initialize
minPoolSize: minimum number of connection the pool will maintain
maxPoolSize: maximum number of connection the pool will maintain
maxStatements: maximum number of Statement the pool will maintain
maxStatementsPerConnection: maximum number of Statement each connection can use
but actually, it’s the new ComboPooledDataSource("intergalactoApp") should be write out side function wrapper. To avoid creating a pool each time getting a connection.
public Connection testGetConnection()throws Exception { // create DBCP connection pool BasicDataSourcesource=newBasicDataSource();
// set basic information source.setDriverClassName("com.mysql.jdbc.Driver"); source.setUrl("jdbc://mysql///test"); source.setUsername("root"); source.setPassword("1122");
// settting about manage pool source.setInitialSize(10); source.setMaxTotal(10);