zoukankan      html  css  js  c++  java
  • Jfinal事务操作

    添加删除更新均可用以下事务

    import java.sql.Connection;
    import java.sql.SQLException;
    
    import com.jfinal.plugin.activerecord.Db;
    import com.jfinal.plugin.activerecord.DbKit;
    
    /**
     * @author 马家立
     * @version 创建时间:2019年11月27日下午3:14:10
     * @Description:TODO 事务测试类
     */
    public class Transaction {
        /**
         * @Title:addTransaction
         * @author:马家立
         * @date:2019年11月27日 下午3:23:06
         * @Description:TODO 事务测试
         * @return boolean
         */
        public boolean addTransaction() {
            // 返回结果状态
            boolean result = false;
            // 声明数据库连接
            Connection conn = null;
            try {
                conn = DbKit.getConfig().getDataSource().getConnection();
                DbKit.getConfig().setThreadLocalConnection(conn);
                // 自动提交表成fasle:手动提交
                conn.setAutoCommit(false);
                // 设置事务隔离级别:可默认
                // conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
                Db.update("delete from nice where note = '111'");
                Db.update("delete from nice where note = '222'");
                Db.update("delete from nice where note = '333'");
                // 无异常提交
                conn.commit();
                result = true;
                System.out.println("事务操作成功");
            } catch (Exception e) {
                result = false;
                e.printStackTrace();
                System.err.println("事务操作失败");
                try {
                    // 异常回滚
                    conn.rollback();
                } catch (SQLException eSql) {
                    eSql.printStackTrace();
                    System.err.println("Connection 回滚失败");
                }
            } finally {
                if (null != conn) {
                    try {
                        // 关闭连接
                        conn.close();
                    } catch (SQLException eSql) {
                        eSql.printStackTrace();
                        System.err.println("Connection 关闭失败");
                    }
                }
                DbKit.getConfig().removeThreadLocalConnection();
            }
            return result;
        }
    }
  • 相关阅读:
    换行符 CR
    c# 定义的属性名与保留关键字冲突
    Redis 以window 服务启动
    c# Guid.NewGuid().ToString(format
    select 下拉源动态赋值
    html 控制input标签只能输入数字
    HTTP 错误 500.19
    Android debugger 出现提示Connected to the target VM, address: 'localhost:xxxx', transport: 'socket'
    siege--Web性能压测工具
    python+selenium上传文件注意点
  • 原文地址:https://www.cnblogs.com/mjtabu/p/11942606.html
Copyright © 2011-2022 走看看