MySQL 学习笔记 044(JDBC操作事务)

内容来自B站【狂神说Java】MySQL最新教程通俗易懂
要么都成功,要么都失败。

ACID原则

原子性:要么全部完成,要么都不完成
一致性:总数不变
隔离性:多个进程互不干扰
持久性:一旦提交不可逆,持久化到数据库了

隔离性的问题:
脏读:一个事务读取了另一个没有提交的事务
不可重复读:在同一个事务内,重复读取表中的数据,表数据发生了改变
虚读(幻读):在一个事务内,读取到了别人插入的数据,导致前后读出来的结果不一致

代码实现

  1. 开启事务 conn.setAutoCommit(false);
  2. 一组业务执行完毕,提交事务
  3. 可以在catch语句中显式地定义回滚语句,但默认失败就会回滚。
    成功的代码:
package com.mqc.lesson04;

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

import com.mqc.lesson02.utils.JdbcUtils;

public class TestTransaction {

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement st = null;

        try {
            conn = JdbcUtils.getConncection();
            // 关闭数据库的自动提交,自动会开启事务
            conn.setAutoCommit(false);
            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();
            // 业务完毕提交事务
            conn.commit();
            System.out.println("成功!");
        } catch (SQLException throwables) {
        	try {
				conn.rollback();// 如果失败则回滚事务,不写也会回滚,因为它默认失败回滚
			} catch (SQLException e) {
				e.printStackTrace();
			}
            throwables.printStackTrace();
        } finally {
            JdbcUtils.release(conn, st, null);
        }
    }

}

失败的代码(第二条SQL字段名错误,模拟失败):

package com.mqc.lesson04;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.mqc.lesson02.utils.JdbcUtils;

public class TestTransaction2 {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		
		try {
			conn = JdbcUtils.getConncection();
			conn.setAutoCommit(false);
			String sql1 = "update account set money = money - 100 where name = 'A'";
			ps = conn.prepareStatement(sql1);
			ps.executeUpdate();
			String sql2 = "update account set monye = money - 100 where name = 'B'";
			ps = conn.prepareStatement(sql2);
			ps.executeUpdate();
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.release(conn, ps, null);
		}
	}
	
}

posted @ 2022-04-30 17:16  君子键  阅读(37)  评论(0)    收藏  举报