c3p0串連池模板,
串連池是建立和管理一個串連的緩衝池的技術,這些串連準備好被任何需要它們的線程使用。
我現在做一個p3c0串連池的模板。
首先p3c0是開源的,所以去官網下載p3c0的jar包。在工程中匯入,同時要下載你串連資料庫的驅動
串連池模板代碼如下:
package com.fish;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 串連資料庫的工具類,被定義成不可繼承且是私人訪問
*/
public final class DBTool {
//採用設定檔的方式配置串連池的一些信心,這個是設定檔的名字
final private static String OPTION_FILE_NAME = "mysqldatabase";
private static Connection conn;
static ResourceBundle res;
//串連池的類
static ComboPooledDataSource cpds;
static {
//從設定檔中擷取檔案
res = ResourceBundle.getBundle(OPTION_FILE_NAME);
//建立一個串連池
cpds = new ComboPooledDataSource();
//驅動名
String driver = res.getString("jdbc.driver");
//串連url
String url = res.getString("jdbc.url");
//資料庫使用者名稱
String user = res.getString("jdbc.username");
//資料庫密碼
String password = res.getString("jdbc.password");
//串連池的最大串連數
String poolMax = res.getString("c3p0.maxPoolSize");
//串連池的最小串連數
String poolMin = res.getString("c3p0.minPoolSize");
//當資源用盡時,允許串連的數目
String PoolAcquireIncrement = res.getString("c3p0.acquireIncrement");
try {
cpds.setDriverClass(driver);
} catch (PropertyVetoException e) {
System.out.println("驅動沒找到");
}
cpds.setJdbcUrl(url);
cpds.setUser(user);
cpds.setPassword(password);
cpds.setMinPoolSize(Integer.parseInt(poolMin));
cpds.setAcquireIncrement(Integer.parseInt(PoolAcquireIncrement));
cpds.setMaxPoolSize(Integer.parseInt(poolMax));
}
/**
* 擷取資料庫的串連
*
* @return conn
*/
public static Connection getConnection() {
try {
conn = cpds.getConnection();
} catch (SQLException e) {
System.out.println("串連失敗");
}
return conn;
}
/**
* 釋放資源
*
*/
public static void closeJDBC(Connection conn, Statement statement,
ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (null != statement) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
}
}
}
2.mysqldatabase.properties檔案如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/datacenter3
jdbc.username=root
jdbc.password=1234
c3p0.maxPoolSize = 30
c3p0.minPoolSize = 10
c3p0.acquireIncrement =10
以後你要串連什麼資料庫直接在這個檔案裡面修改。
測試:例子
package com.fish;
public class Test2 {
public static void main(String[] args) throws Exception {
System.out.println(DBTool.getConnection());
}
}
輸出結果:
2014-11-16 14:44:04 com.mchange.v2.log.MLog <clinit>
資訊: MLog clients using java 1.4+ standard logging.
2014-11-16 14:44:04 com.mchange.v2.c3p0.C3P0Registry banner
資訊: Initializing c3p0-0.9.2.1 [built 20-March-2013 11:16:28 +0000; debug? true; trace: 10]
2014-11-16 14:44:04 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
資訊: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 10, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge1d1951evdfumup12ui|5ffb18, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge1d1951evdfumup12ui|5ffb18, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/datacenter3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 30, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]
com.mchange.v2.c3p0.impl.NewProxyConnection@d19bc8