jdbc 12: 悲观锁

2年前 (2022) 程序员胖胖胖虎阿
169 0 0

jdbc连接mysql,简单演示行级锁

通过debug模式进行演示

在Test1程序设置断点,让程序1,查询并锁定数据,且程序不执行完(此时停在debug断点处)
这时启动Test2程序,去修改已经被锁定的数据,发现此时,程序2出现等待情况
让程序1的断点结束,此时程序2可以顺利修改数据
原因: 程序1的行级锁已经释放

Test1

package com.examples.jdbc.o12_悲观锁;


import com.examples.jdbc.utils.DBUtils;

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

/**
 * 简单的行级锁的演示
 */
public class Test1 {
    public static void main(String[] args) {
        //3个数据库资源对象
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //jdbc数据库操作

        try {
            //1.
            connection = DBUtils.getConnection();
            connection.setAutoCommit(false);

            //2.
            String sql = "select * from tb_user where uname like ? for update";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "_");

            //3.
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                int id = resultSet.getInt("id");
                String uname = resultSet.getString("uname");
                String upasswd = resultSet.getString("upasswd");
                System.out.println("id: " + id + " uname: " + uname + " upasswd: " + upasswd);
            }
            connection.commit();

        } catch (SQLException e) {
            if(connection != null){
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        }finally {
            //4.
            DBUtils.close(connection, preparedStatement, resultSet);
        }
    }
}

Test2

package com.examples.jdbc.o12_悲观锁;

import com.examples.jdbc.utils.DBUtils;

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

/**
 * 模拟在行级锁起作用期间,修改被锁定的数据
 */
public class Test2 {
    public static void main(String[] args) {
        //2个jdbc资源对象
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //jdbc操作

        try {
            //1.
            connection = DBUtils.getConnection();
            connection.setAutoCommit(false);

            //2.
            String sql = "update tb_user set upasswd = ? where uname like ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "xun");
            preparedStatement.setString(2, "_");

            //3.
            int num = preparedStatement.executeUpdate();
            System.out.println(num == 1 ? "修改成功" : "修改失败");

            connection.commit();

        } catch (SQLException e) {
            if(connection != null){
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        }finally {
            //4.
            DBUtils.close(connection, preparedStatement, null);
        }
    }
}
版权声明:程序员胖胖胖虎阿 发表于 2022年9月20日 下午3:00。
转载请注明:jdbc 12: 悲观锁 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...