当前位置:网站首页>MySQL learning records 12jdbc operation transactions

MySQL learning records 12jdbc operation transactions

2022-07-06 08:26:00 Jatine

MySQL Learning record 12JDBC Operational transaction

ACID principle

  • Atomicity : Or it's all done , Or it's not done
  • Uniformity : The total remains unchanged
  • persistence : Once submitted, it's irreversible , Persistent to the database
  • Isolation, : Multiple processes do not interfere with each other

The problem of isolation

  • Dirty reading : One transaction reads another uncommitted transaction
  • It can't be read repeatedly : In the same business , Repeatedly read the data in the table , Table data has changed
  • Virtual reading ( Fantasy reading ): In a business , Read the data inserted by others , The results read before and after are inconsistent

Code implementation

  • First step : Open transaction (conn.setAutoCommit(false);
  • The second step : After the execution of a group of business , Commit transaction (conn.commit();
  • The third step : Can be in catch Statement to define the rollback statement , But the default failure will also be rolled back (conn.rollback();

sql Create table statement :

CREATE TABLE account
(
    id    Int PRIMARY KEY AUTO_INCREMENT,
    NAME  VARCHAR(40),
    money FLOAT
);

/* Insert test data */
insert into account(name, money)
values ('A', 1000);
insert into account(name, money)
values ('B', 1000);
insert into account(name, money)
values ('C', 1000);
import com.jatine.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestTransaction1 {
    
    public static void main(String[] args) throws SQLException {
    
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        try {
    
            conn = JdbcUtils.getConnection();
            // Turn off automatic submission of the database , Automatically opens the transaction 

            conn.setAutoCommit(false);  // Open transaction 

            String sql1 = "update account set money = money - 100 where name = 'A'";
            st = conn.prepareStatement(sql1);
            st.executeUpdate();

            String sql2 = "update account set money = money + 100 where name = 'B'";
            st = conn.prepareStatement(sql2);
            st.executeUpdate();

            // Business finished , Commit transaction 
            conn.commit();
            System.out.println(" success !");

        } catch (SQLException e) {
    

            // If it fails, roll back the transaction 
            try {
    
                conn.rollback(); // Even if there is no such sentence , If it fails, it is also the default rollback 
            } catch (SQLException throwables) {
    
                throwables.printStackTrace();
            }
            e.printStackTrace();
        } finally {
    
            JdbcUtils.release(conn, st, rs);
        }
    }
}

 Insert picture description here
 Insert picture description here
Simulate failure scenarios :

            String sql1 = "update account set money = money - 100 where name = 'A'";
            st = conn.prepareStatement(sql1);
            st.executeUpdate();

            int x = 1 / 0; // Report errors 

            String sql2 = "update account set money = money + 100 where name = 'B'";
            st = conn.prepareStatement(sql2);
            st.executeUpdate();

Report errors :
 Insert picture description here
The data has not changed :
 Insert picture description here

原网站

版权声明
本文为[Jatine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131825406271.html