• JDBC: Transactions


     

    A transaction is a set of actions to be carried out as a single, atomic action. Either all of the actions are carried out, or none of them are.

    The classic example of when transactions are necessary is the example of bank accounts. You need to transfer $100 from one account to the other. You do so by subtracting $100 from the first account, and adding $100 to the second account. If this process fails after you have subtracted the $100 fromt the first bank account, the $100 are never added to the second bank account. The money is lost in cyber space.

    To solve this problem the subtraction and addition of the $100 are grouped into a transaction. If the subtraction succeeds, but the addition fails, you can "rollback" the fist subtraction. That way the database is left in the same state as before the subtraction was executed.

    You start a transaction by this invocation:

    connection.setAutoCommit(false);
    

    Now you can continue to perform database queries and updates. All these actions are part of the transaction.

    If any action attempted within the transaction fails, you should rollback the transaction. This is done like this:

    connection.rollback();
    

    If all actions succeed, you should commit the transaction. Committing the transaction makes the actions permanent in the database. Once committed, there is no going back. Committing the transaction is done like this:

    connection.commit();
    

    Of course you need a bit of try-catch-finally around these actions. Here is a an example:

    Connection connection = ...
    try{
        connection.setAutoCommit(false);
    
        // create and execute statements etc.
    
        connection.commit();
    } catch(Exception e) {
        connection.rollback();
    } finally {
        if(connection != null) {
            connection.close();
        }
    }
    

    Here is a full example:

    Connection connection = ...
    try{
        connection.setAutoCommit(false);
    
    
        Statement statement1 = null;
        try{
            statement1 = connection.createStatement();
            statement1.executeUpdate(
                "update people set name='John' where id=123");
        } finally {
            if(statement1 != null) {
                statement1.close();
            }
        }
    
    
        Statement statement2 = null;
        try{
            statement2 = connection.createStatement();
            statement2.executeUpdate(
                "update people set name='Gary' where id=456");
        } finally {
            if(statement2 != null) {
                statement2.close();
            }
        }
    
        connection.commit();
    } catch(Exception e) {
        connection.rollback();
    } finally {
        if(connection != null) {
            connection.close();
        }
    }
    
  • 相关阅读:
    Cow Rectangles&Moovie Mooving
    Sound静音问题
    Spring MVC 流程图(转)
    centos6.5配置redis服务 很好用谢谢
    如何用70行Java代码实现深度神经网络算法(转)
    java中枚举(enum)小例子。之前学过枚举但是一直没用,这里有个枚举类帮你我理解下(很肤浅)
    幸福很简单(一直不知道怎么去阐述幸福,今天终于看到一个台词觉得这个阐述还行,作一个笔记)-----------穷人好像都是这么觉得的
    ExecutorService中submit和execute的区别(转)
    spring batch部分
    java 堆栈的区别(转百度)
  • 原文地址:https://www.cnblogs.com/hephec/p/4562822.html
Copyright © 2020-2023  润新知