Batch data and transaction control

Batch data

You may operate a plurality of data.

public class TestAddBath {
    public static void main(String[] args) throws SQLException {
        TestOne.getConnection();
    }
}

class TestOne {
    static Connection con = null;
    static PreparedStatement ps = null;

    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        con.setAutoCommit(false);
        ps = con.prepareStatement("insert into tanx(name) values(?)");
            for (int i = 0; i < 10; ++i) {
                ps.setObject(1, "111");
                //数据批处理
                ps.addBatch();
            }
        //数据批处理
        ps.executeBatch();
        return con;
    }
}

result:

Here Insert Picture Description

Transaction Control

In the absence of transaction rollback when multiple data operation, an error, the other still to run, and the transaction is rolled back in a number of data when there is a mistake, it will not operate until back, or to with success, or to fail together.

There are four characteristics of the transaction, acronym ACID transactions described:

1, atomic (Atomic), conduct a transaction by one or more bundled together to form, it seems to be a single unit of work. Operations within the atomic transaction guarantees either have occurred or not occurred. If all the operations are successful, then the transaction is successful. Join any operation fails, then the transaction will be rolled back.

2, consistency (consistent), once a transaction is over, regardless of success or failure, in which the state of the system and its business rules are the same. That is, the data should not be destroyed.

3, isolation (Isolation), the transaction should allow multiple users operate on the same data, a user's operation of the operation will not confuse other users. Thus, the transaction must be isolated to prevent the occurrence of the same situation a parallel read and write data. Note that the isolation often means you want to lock the database table or row.

4, the persistence (Durable), once the transaction is completed, the results of the transaction should be persistent. So no matter what kind of system crashes, they all survived.

Example:

public class TestAddBath {
    public static void main(String[] args) throws SQLException {
        TestOne.getConnection();
    }
}

class TestOne {
    static Connection con = null;
    static PreparedStatement ps = null;

    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
      //JDBC默认采用的是自动事务,它的特性就是一个出错,其他的正常运行,不能事务回滚,必须关闭
        con.setAutoCommit(false);
        ps = con.prepareStatement("insert into tanx(name) values(?)");
        try{
            for (int i = 0; i < 10; ++i) {
                ps.setObject(1, "111");
                //数据批处理
                ps.addBatch();	
            }
        }catch (Exception e){
            //回滚操作
            con.rollback();
        }
        //数据批处理
        ps.executeBatch();
        return con;
    }
}
Published 61 original articles · won praise 0 · Views 2169

Guess you like

Origin blog.csdn.net/sabstarb/article/details/104796818