当前位置:Gxlcms > mysql > c3p0连接池模板

c3p0连接池模板

时间:2021-07-01 10:21:17 帮助过:40人阅读

连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。 我现在做一个p3c0连接池的模板。 首先p3c0是开源的,所以去官网下载p3c0的jar包。在工程中导入,同时要下载你连接数据库的驱动 连接池模板代码如下: package com.fish

连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。

我现在做一个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 信息: 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

人气教程排行