• JDBC中setTransactionIsolation


    JDBC中,设置传播特性需要使用到 java.sql.Connection 接口中的 setTransactionIsolation(int level) 方法和 setAutoCommit(boolean autoCommit) 方法。

    假设你有两个方法 method1() 和 method2(),它们都需要在不同的事务中运行。你可以将这些方法封装在一个父级方法中,并使用传播特性来控制它们之间事务的传播方式。

    以下是如何在JDBC中设置传播特性:

    java
    public void parentMethod() {
    Connection conn = null;
    try {
    // get a new connection
    conn = DriverManager.getConnection(dbUrl, username, password);

        // begin transaction
        conn.setAutoCommit(false);
    
        // set transaction isolation level
        conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    
        // call method1() with PROPAGATION_REQUIRED propagation
        method1(conn);
    
        // call method2() with PROPAGATION_REQUIRES_NEW propagation
        method2(conn);
    
        // commit transaction
        conn.commit();
    } catch (SQLException e) {
        // handle exception and rollback transaction
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    } finally {
        // close connection
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    }

    public void method1(Connection conn) throws SQLException {
    // set transaction isolation level
    conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

    // execute SQL statements within the current transaction
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2')");
    
    // commit transaction
    conn.commit();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    }

    public void method2(Connection conn) throws SQLException {
    // set propagation to REQUIRES_NEW
    conn.setAutoCommit(false);

    // set transaction isolation level
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    
    // execute SQL statements within a new transaction
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("UPDATE my_table SET column1 = 'new_value' WHERE column2 = 'value2'");
    
    // commit transaction
    conn.commit();
    
    // reset auto-commit mode to true
    conn.setAutoCommit(true);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    }
    在上述代码中,parentMethod() 是父级方法,它包含两个方法 method1() 和 method2()。method1() 使用默认的传播特性(PROPAGATION_REQUIRED),也就是如果当前存在事务,则使用该事务。如果没有事务,则开启一个新的事务。

    method2() 显式地设置传播特性为 PROPAGATION_REQUIRES_NEW,也就是无论当前是否存在事务,都会开启一个新的事务。

    注意:以上示例中的代码仅用于演示目的,实际应用中可能需要更复杂的事务处理。

  • 相关阅读:
    Oracle P6 -SQLServer数据库乱码案例分享
    SpringBoot的Data开发篇:整合JDBC、整合Mybatis&MP,YAML文件加密的实现,数据&项目监控平台的使用和实现
    MAC配置VScode中C++项目debug环境
    jsonpath介绍与使用
    【杂记】Windows首页挟持病毒查杀过程记录
    Linux系统编程(一)——环境搭建
    .NET性能优化-使用SourceGenerator-Logger记录日志
    关注|这些规则策略调优的内容周末划下重点
    修剪二叉搜索树likou669
    液压插装式比例阀放大器SP08-47R-0-N-24DG、SP08-47R-0-N-12DG
  • 原文地址:https://blog.csdn.net/u014244856/article/details/133419528