JDBC_3 database transaction

Database transaction


Once the data is submitted, it cannot be rolled back
. Which operations will cause the data to be automatically submitted?

  1. Once the DDL operation is executed, it will be automatically submitted
    -. set autocommit = false does not work
  2. By default, DML will be automatically submitted once executed
    -. You can set set autocommit = false
  3. It will be submitted automatically when the connection is closed
		Connection connection = DriverManager.getConnection(url, user, password);
        connection.setAutoCommit(false);
        String sql1 = "update user_table set balance = balance - 100";
        update(connection,sql1,"AA");
        String sql2 = "update user_table set balance = balance + 100"
        update(connection,sql2,"BB");
        connection.commit();

Note If the Connection is not closed, it may be reused, you need to restore its automatic submission status. Especially when using database connection pool technology.

ACID properties of the transaction

  1. Atomicity: Atomicity is the value transaction is an indivisible unit of work, the operations in the transaction either all happen or never happen
  2. Consistency (Consistency): The transaction must be the database from a consistent state to another consistent state
  3. Isolation: The isolation of a transaction means that the execution of a thing cannot be interfered by other transactions, that is, the internal operations and data used by a thing are isolated from other transactions of the ice method, and the concurrent execution of various things cannot Mutual interference
  4. Durability: Durability means that once a transaction is committed, its changes to the data in the database are permanent, and other operations and database failures should not have any concurrency issues affecting the
    database.
  • Dirty read: For two transactions T1, T2, and T1 read the fields that have been updated by T2 but have not yet been committed. Later, if T2 rolls back, the content read by T1 is temporary and invalid.
  • Non-repeatable read: For two transactions T1, T2, T1 read a field, then T2 updates the field, after that, T1 reads the same field again, the value is different
  • Phantom read: For two transactions T1, T2, T1 read a field from a table, then T2 inserts some new rows in the table, and then if T1 reads the same table again, there will be a few more Row.

Examples of transaction operations in four isolation levels
Insert picture description here

java

        Connection connection = DriverManager.getConnection(url, user, password);
        //获取当前数据库的隔离级别
        System.out.println(connection.getTransactionIsolation());
        //设置数据库的隔离级别
        connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        connection.setAutoCommit(false);
        String sql = "select user,password,balance = ? from user_table where user = ?";
        User user = getInstance(connection,User.class,sql,"CC");
        System.out.println();

        Connection connection = DriverManager.getConnection(url, user, password);

        connection.setAutoCommit(false);
        String sql = "update user_table set balance = ? where user = ?";
        update(connection,sql,5000,"CC");
        Thread.sleep(15000);
        System.out.println("修改结束");

Guess you like

Origin blog.csdn.net/m0_46656833/article/details/112519719