C3P0是一個開源的JDBC連接池,它實現了數據源和JNDI綁定,支持JDBC3規范和JDBC2的標准擴展,設計非常簡單易用
C3P0的基本使用
添加maven依賴
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5</version>
</dependency>
編寫C3P0工具類
public class C3P0Utils {
private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");
public static DataSource getDataSource() {
return dataSource;
}
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//釋放連接回連接池
public static void close(Connection conn, PreparedStatement pst, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
classpath下面配置文件
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://192.168.47.151:3306/web</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
<named-config name="mysql">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://192.168.47.151:3306/web</property>
<property name="user">root</property>
<property name="password">root</property>
</named-config>
</c3p0-config>
測試代碼
@Test
public void test1(){
Connection conn = null;
PreparedStatement pstmt = null;
// 1.創建自定義連接池對象
DataSource dataSource = new DataSourcePool();
try {
// 2.從池子中獲取連接
conn = C3P0Utils.getConnection();
String sql = "insert into USER values(?,?)";
//3.必須在自定義的connection類中重寫prepareStatement(sql)方法
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "李四");
pstmt.setString(2, "1234");
int rows = pstmt.executeUpdate();
System.out.println("rows:"+rows);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
C3P0Utils.close(conn,pstmt,null);
}
}
C3P0的其他細化配置
<!--acquireIncrement:鏈接用完了自動增量3個。 -->
<property name="acquireIncrement">3</property>
<!--acquireRetryAttempts:鏈接失敗后重新試30次。-->
<property name="acquireRetryAttempts">30</property>
<!--acquireRetryDelay;兩次連接中間隔1000毫秒。 -->
<property name="acquireRetryDelay">1000</property>
<!--autoCommitOnClose:連接關閉時默認將所有未提交的操作回滾。 -->
<property name="autoCommitOnClose">false</property>
<!--automaticTestTable:c3p0測試表,沒什么用。-->
<property name="automaticTestTable">Test</property>
<!--breakAfterAcquireFailure:出錯時不把正在提交的數據拋棄。-->
<property name="breakAfterAcquireFailure">false</property>
<!--checkoutTimeout:100毫秒后如果sql數據沒有執行完將會報錯,如果設置成0,那么將會無限的等待。 -->
<property name="checkoutTimeout">100</property>
<!--connectionTesterClassName:通過實現ConnectionTester或QueryConnectionTester的類來測試連接。類名需制定全路徑。Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester-->
<property name="connectionTesterClassName"></property>
<!--factoryClassLocation:指定c3p0 libraries的路徑,如果(通常都是這樣)在本地即可獲得那么無需設置,默認null即可。-->
<property name="factoryClassLocation">null</property>
<!--forceIgnoreUnresolvedTransactions:作者強烈建議不使用的一個屬性。-->
<property name="forceIgnoreUnresolvedTransactions">false</property>
<!--idleConnectionTestPeriod:每60秒檢查所有連接池中的空閑連接。-->
<property name="idleConnectionTestPeriod">60</property>
<!--initialPoolSize:初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。 -->
<property name="initialPoolSize">3</property>
<!--maxIdleTime:最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。-->
<property name="maxIdleTime">60</property>
<!--maxPoolSize:連接池中保留的最大連接數。 -->
<property name="maxPoolSize">15</property>
<!--maxStatements:最大鏈接數。-->
<property name="maxStatements">100</property>
<!--maxStatementsPerConnection:定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->
<property name="maxStatementsPerConnection"></property>
<!--numHelperThreads:異步操作,提升性能通過多線程實現多個操作同時被執行。Default: 3-->
<property name="numHelperThreads">3</property>
<!--overrideDefaultUser:當用戶調用getConnection()時使root用戶成為去獲取連接的用戶。主要用於連接池連接非c3p0的數據源時。Default: null-->
<property name="overrideDefaultUser">root</property>
<!--overrideDefaultPassword:與overrideDefaultUser參數對應使用的一個參數。Default: null-->
<property name="overrideDefaultPassword">password</property>
<!--password:密碼。Default: null-->
<property name="password"></property>
<!--preferredTestQuery:定義所有連接測試都執行的測試語句。在使用連接測試的情況下這個一顯著提高測試速度。注意: 測試的表必須在初始數據源的時候就存在。Default: null-->
<property name="preferredTestQuery">select id from test where id=1</property>
<!--propertyCycle:用戶修改系統配置參數執行前最多等待300秒。Default: 300 -->
<property name="propertyCycle">300</property>
<!--testConnectionOnCheckout:因性能消耗大請只在需要的時候使用它。Default: false -->
<property name="testConnectionOnCheckout">false</property>
<!--testConnectionOnCheckin:如果設為true那么在取得連接的同時將校驗連接的有效性。Default: false -->
<property name="testConnectionOnCheckin">true</property>
<!--user:用戶名。Default: null-->
<property name="user">root</property>
<!--usesTraditionalReflectiveProxies:動態反射代理。Default: false-->
<property name="usesTraditionalReflectiveProxies">false</property>