原文:https://www.cnblogs.com/jonny-xu/p/6374163.html

一、需要jar包:

  c3p0-0.9.1.2.jar

  mysql-connector-java-5.1.20-bin.jar

二、Java代码:

  

package com.javaweb.jdbc;

import java.sql.Connection;
import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil { // 饿汉式
private static DataSource ds = new ComboPooledDataSource();
/*ThreadLocal
* 它为null表示没有事务
* 它不为null表示有事务
* 当事物开始时,需要给它赋值
* 当事务结束时,需要给它赋值null
* 并且在开启事务时,让dao的多个方法共享这个Connection
*/
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); public static DataSource getDataSource(){
return ds;
} /**
* 需要手动开始事务时
* dao使用本方法来获取连接
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
Connection con = tl.get();
if(con != null){
return con;
}
return ds.getConnection();
} /**
* 开启事务
* @throws SQLException
*/
public static void beginTransaction() throws SQLException {
Connection con = tl.get();//获取当前线程的事务连接
if(con != null) throw new SQLException("已经开启了事务,不能重复开启!");
con = ds.getConnection();//给con赋值,表示开启了事务
con.setAutoCommit(false);//设置为手动提交
tl.set(con);//把当前事务连接放到tl中
} /**
* 提交事务
* @throws SQLException
*/
public static void commitTransaction() throws SQLException {
Connection con = tl.get();//获取当前线程的事务连接
if(con == null) {
throw new SQLException("没有事务不能提交!");
}
con.commit();//提交事务
con.close();//关闭连接
con = null;//表示事务结束!
tl.remove();
} /**
* 回滚事务
* @throws SQLException
*/
public static void rollbackTransaction() throws SQLException {
Connection con = tl.get();//获取当前线程的事务连接
if(con == null) {
throw new SQLException("没有事务不能回滚!");
}
con.rollback();
con.close();
con = null;
tl.remove();
} /**
* 释放Connection
* @param con
* @throws SQLException
*/
public static void releaseConnection(Connection connection) throws SQLException {
Connection con = tl.get();//获取当前线程的事务连接
if(connection != con) {//如果参数连接,与当前事务连接不同,说明这个连接不是当前事务,可以关闭!
if(connection != null &&!connection.isClosed()) {//如果参数连接没有关闭,关闭之!
connection.close();
}
}
}
}

三、在src下创建名字必须为 c3p0-config.xml文件

 

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
<default-config>
<!-- 四大必要属性 -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_mysqltest</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123</property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement">3</property>
<!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3-->
<property name="initialPoolSize">10</property>
<!-- 连接池中保留的最小连接数,默认为:3-->
<property name="minPoolSize">2</property>
<!--连接池中保留的最大连接数。默认值: 15 -->
<property name="maxPoolSize">10</property>
</default-config>
</c3p0-config>

最新文章

  1. 知方可补不足~sqlserver中对xml类型字段的操作
  2. Leetcode Construct Binary Tree from Preorder and Inorder Traversal
  3. 数据仓库专题(23):总线矩阵的另类应用-Drill Down into a More Detailed Bus Matrix
  4. java 15-4 集合的专用遍历工具 迭代器
  5. POJ2047 Concert Hall Scheduling(最小费用最大流)
  6. java 学习路线《转》
  7. nylg 640 Geometric Sum
  8. 手机抓包 http tcp udp?
  9. 多屏判断css改写
  10. WebSocket 是什么原理?为什么可以实现持久连接
  11. 组建自动化工具Ant
  12. C# Form窗体子窗口关闭时刷新父窗体中的datagridview
  13. web api 2 学习笔记 (OData Batch request)
  14. Python变量和数据类型
  15. mysql iot 主键自增列问题
  16. MVC中实现多按钮提交(转)
  17. Django之csrf防御机制
  18. 如何自学Java开发
  19. gradle问题汇总
  20. weexpack打包weex项目运行/打包记录

热门文章

  1. *p 和p[i] 区别
  2. Disable trigger to avoid the ID is auto-updated
  3. mongodb索引 过期索引
  4. zend studio 13.6.1中文乱码的解决办法
  5. LOAD DATA INFILE读取CSV中一千万条数据至mysql
  6. JDK源码那些事儿之红黑树基础上篇
  7. [原创]VSCode debug jest的配置
  8. MySQL快速生成100W条测试数据
  9. c语言1博客作业09
  10. c语言1博客作业07