连接池C3P0!
连接池技术的目的:解决建立数据库连接耗费资源和时间很多的问题,提高性能 !
下面以案例演示下C3P0的操作流程.
1、测试准备:
①MySql数据库一枚
②database名为mytest
③项目工程
2、代码展示
pom.xml的部分代码
c3p0-config.xml
JdbcUtils.java
package apitest.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtils {
public static final ComboPooledDataSource ds_deault = new ComboPooledDataSource(); //统一数据连接关联
public static DataSource getDataSource(String type) {
if("mysql".equals(type)) {
return ds\_mysql;
}else if ("mysql2".equals(type)) {
return ds\_mysql2;
}
return ds\_deault;
}
public static DataSource getDataSource() {
return getDataSource("");
}
public static Connection getConnection(String type) throws SQLException {
return getDataSource(type).getConnection();
}
public static Connection getConnection() throws SQLException {
return getDataSource("").getConnection();
}
}
public class DbCheckUtils {
public static boolean dbCheck(String dbCheck) throws SQLException {
if(StringUtils.isNotBlank(dbCheck)) {
String\[\] dbcheck\_array=dbCheck.split(",");
String sql=dbcheck\_array\[0\];
sql=ParamsUtils.replace(sql);
QueryRunner runner=null;
if(dbcheck\_array.length<=2) {
runner = new QueryRunner(JdbcUtils.getDataSource());
}else {
runner = new QueryRunner(JdbcUtils.getDataSource(dbcheck\_array\[2\]));
}
System.out.println("sql "+sql);
List<Map<String, Object>> list = runner.query(sql, new MapListHandler());
JsonCheckResult jsonrResult= CheckPointUtils.check(JSON.toJSONString(list), dbcheck\_array\[1\]);
return jsonrResult.isResult();
}
return false;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章