首页 > 其他 > 详细

事务工具类

时间:2020-06-17 11:51:04      阅读:76      评论:0      收藏:0      [点我收藏+]

public
class TransactionsUtil { //创建线程 private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>(); //获取连接 public static Connection getConnection() throws PropertyVetoException, SQLException { Connection conn = threadLocal.get();//获取链接,如果没有自动创建副本 if (conn == null) { //c3p0数据源 conn = ComboPooledDataSourceUtil.getDataSourceByxml().getConnection(); threadLocal.set(conn); } return conn; } //开启事务 public static void begin() throws PropertyVetoException, SQLException { Connection conn = getConnection();//先获取链接 if (conn != null) conn.setAutoCommit(false);//手动提交/开启事务 } //提交事务 public static void commit() throws PropertyVetoException, SQLException { Connection connection = getConnection(); if (connection != null) connection.commit(); } //事务回滚 public static void rollback() throws PropertyVetoException, SQLException { Connection connection = getConnection(); if (connection == null) connection.rollback(); } //关闭线程和连接 public static void close() throws PropertyVetoException, SQLException { Connection conn = getConnection(); if (conn != null) conn.close(); threadLocal.remove(); } }

 

事务工具类

原文:https://www.cnblogs.com/hyy9527/p/13151394.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!