一、c3p0

public class ConnectionManager {

    public static ComboPooledDataSource dataSource;
static {
try {
dataSource = new ComboPooledDataSource();
dataSource.setUser("freeswitch");
dataSource.setPassword("freeswitch");
dataSource.setJdbcUrl("jdbc:postgresql://数据库地址:数据库端口/freeswitch");
dataSource.setDriverClass("org.postgresql.Driver");
dataSource.setInitialPoolSize();
dataSource.setMinPoolSize();
dataSource.setMaxPoolSize();
dataSource.setMaxStatements();
dataSource.setMaxIdleTime();
} catch (Exception e) {
e.printStackTrace();
}
} public static Connection getConnection3() {
Connection conn = null;
if (null != dataSource) {
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
}

二、dbcp2

public class DataBaseHelper {

    // 保证一个线程一个Connection,线程安全
private static final ThreadLocal<Connection> connHolder;
// 线程池
private static final BasicDataSource dataSource;
static {
connHolder = new ThreadLocal<Connection>();
dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://数据库地址:数据库端口/freeswitch");
dataSource.setUsername("freeswitch");
dataSource.setPassword("freeswitch");
/// 设置空闲和借用的连接的最大总数量,同时可以激活。
dataSource.setMaxTotal();
// 设置初始大小
dataSource.setInitialSize();
// 最小空闲连接
dataSource.setMinIdle();
// 最大空闲连接
dataSource.setMaxIdle();
// 超时等待时间毫秒
dataSource.setMaxWaitMillis( * );
// 只会发现当前连接失效,再创建一个连接供当前查询使用
dataSource.setTestOnBorrow(true);
// removeAbandonedTimeout :超过时间限制,回收没有用(废弃)的连接(默认为 300秒,调整为180)
dataSource.setRemoveAbandonedTimeout();
// removeAbandoned :超过removeAbandonedTimeout时间后,是否进
// 行没用连接(废弃)的回收(默认为false,调整为true)
// DATA_SOURCE.setRemoveAbandonedOnMaintenance(removeAbandonedOnMaintenance);
dataSource.setRemoveAbandonedOnBorrow(true);
// testWhileIdle
dataSource.setTestOnReturn(true);
// testOnReturn
dataSource.setTestOnReturn(true);
// setRemoveAbandonedOnMaintenance
dataSource.setRemoveAbandonedOnMaintenance(true);
// 记录日志
dataSource.setLogAbandoned(true);
// 设置自动提交
dataSource.setDefaultAutoCommit(true); } /**
* 获取数据库连接
*/
public static Connection getConnection() {
Connection conn = connHolder.get();
if (conn == null) {
try {
conn = dataSource.getConnection();
System.out.println("get connection success");
} catch (SQLException e) {
System.out.println("get connection failure:" + e);
} finally {
connHolder.set(conn);
}
}
return conn;
} /**
* 关闭数据库连接
*/
public static void closeConnection() {
Connection conn = connHolder.get();
if (conn != null) {
try {
conn.close();
System.out.println("close connection success");
} catch (SQLException e) {
System.out.println("close connection failure:" + e);
throw new RuntimeException(e);
} finally {
connHolder.remove();
}
}
} }

最新文章

  1. psr的规范
  2. How can I protect derived classes from breaking when I change the internal parts of the base class?
  3. springboot一个service内组件的加载顺序
  4. Spring mvc 验证码的做法
  5. XML入门级的简单学习
  6. c++ 复习练习
  7. mybatis入门,基本案例和xml讲解
  8. 浙大玉泉ubuntu L2TP VPN连接设置
  9. JS添加类似C# string.Format方法
  10. iOS 图片本地存储、本地获取、本地删除
  11. Android中build&#160;target,minSdkVersion,targetSdkVersion,maxSdkVersion概念区分
  12. vue.js使用webpack发布,部署到服务器上之后在浏览器中可以查看到vue文件源码
  13. 微信小程序基础之表单Form的使用
  14. 为什么局部变量必须以final修饰(或者有final实效:java8)才可以在内部类中使用?
  15. Python3 tkinter基础 event keysym 查看按键的按键名
  16. linux下recv 、send阻塞、非阻塞区别和用法
  17. HTTPS的误解(二)
  18. LINUX下PHP编译添加相应的动态扩展模块so(不需要重新编译PHP,以openssl.so为例)
  19. 添加jquery脚本文件
  20. C# 实现Excel读取接口写入数据

热门文章

  1. struts2类型转化器详解(带例子)
  2. python笔记24-unittest单元测试之mock.patch
  3. TCP常用网络和木马使用端口对照表,常用和不常用端口一览表
  4. 服务器主机上RAID Card的Write Caching Policy
  5. JavaScript逻辑and、or、not运算符详解
  6. MyEclipse中Ctrl+Shift+F快捷键格式化代码时不换行
  7. 转:Android应用开发性能优化完全分析
  8. win8.1安装开发工具 vs2015 Visual Studio 2015 Preview Downloads
  9. C经典之9-system,if,do,while---ShinePans
  10. UI设计经常使用站点