zoukankan      html  css  js  c++  java
  • 事务工具类


    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(); } }
  • 相关阅读:
    Ubuntu各种indicator汇总
    python 命令行参数获取
    python 内置方法的时间复杂度
    SkipList 跳表
    HBase Snapshot功能介绍
    HBase内部操作日志说明
    HBase参数配置及说明
    HBase 在HDFS 上的目录树
    第一章 重构,第一个案例
    shell来start、stop、restart应用程序模板
  • 原文地址:https://www.cnblogs.com/hyy9527/p/13151394.html
Copyright © 2011-2022 走看看