JDBC中事务机制以及行级锁(悲观锁)

JDBC事务机制:
1、JDBC中的事务是自动提交的,什么是自动提交的?
     只要执行任意一条DML语句,则自动提交一次,这是JDBC默认的事务行为。
     但是在实际业务中,通常都是N条DML语句共同联合才能完成的,必须保证
     他们这些DML语句在同一个事务中同时成功或者同时失败。
2、以下程序先来验证以下JDBC的事务是否是自动提交机制!
     测试结果,JDBC中只要执行任意一条DML语句,就提交一次。

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

    try {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "333");
        //获取预处理数据库操作对象
        String sql="update dept set dname=? where deptno=?";
        ps=conn.prepareStatement(sql);
        ps.setString(1,"x部门");
        ps.setInt(2,30);
        int count =ps.executeUpdate();
        System.out.println(count);

        ps.setString(1,"y部门");
        ps.setInt(2,20);
        count=ps.executeUpdate();
        System.out.println(count);
        //处理查询结果集
        //释放资源
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    } finally {
        if (ps == null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn == null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

sql脚本:
drop table if exists t_act;
create table t_act(
     actno int,
     balance double(7,2)//注意7表示有效数字的个数,2表示小数位的个数。
);
insert into t_act(actno,balance)values(111,20000);
insert into t_act(actno,balance)values(222,0);
commit;
select *from t_act;

 重点三行代码?
     conn.setAutoCommit(false);
     conn.commit();
     conn.rollback();

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

    try {
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "333");
        //将自动提交机制改为手动提交
        conn.setAutoCommit(false);//开启事务
        //获取预处理数据库操作对象
        String sql="update t_act set balance=? where actno=?";
        ps=conn.prepareStatement(sql);

        ps.setDouble(1,0);
        ps.setInt(2,111);
        int count=ps.executeUpdate();

        //String s=null;
        //s.toString();

        ps.setDouble(1,20000);
        ps.setInt(2,222);
        count+=ps.executeUpdate();

        System.out.println(count==2?"转账成功":"转账失败");

        //程序走到这里说明以上程序没有异常,事务结束,手动提交
        conn.commit();
    } catch (ClassNotFoundException | SQLException e) {
        if (conn == null) {
            try {
                conn.rollback();//回滚事务
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        e.printStackTrace();
    } finally {
        if (ps == null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn == null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

悲观锁(行级锁)

这个程序开启一个事务,这个事务专门进行查询,并且使用行级锁/悲观锁,锁住相关记录

public static void main(String[] args) {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    try{
        //注册驱动
        //获取连接
        conn=DBUtil.getConnection();
        //开启事务
        conn.setAutoCommit(false);
        //获取数据库操作对象
        String sql="select ename,job,sal from emp where job=? for update";
        ps=conn.prepareStatement(sql);
        ps.setString(1,"MANAGER");
        //执行SQL语句
        rs=ps.executeQuery();
        //处理查询结果集
        while(rs.next()){
            String ename=rs.getString("ename");
            String job=rs.getString("job");
            Double sal=rs.getDouble("sal");
            System.out.println(ename+'\t'+job+'\t'+sal);
        }

        //提交事务(事务结束)
        conn.commit();
    }catch(Exception e){
        if (conn == null) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        e.printStackTrace();
    }finally {
        //释放资源
        DBUtil.close(conn,ps,rs);
    }
}

这个程序负责修改被锁定的记录

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

    try{
        //注册驱动,获取连接
        conn=DBUtil.getConnection();
        //开启事务
        conn.setAutoCommit(false);
        //获取数据库操作对象
        String sql="update emp set sal=sal*1.1 where job=?";
        ps=conn.prepareStatement(sql);
        ps.setString(1,"MANAGER");
        //执行sql语句
        int count=ps.executeUpdate();
        System.out.println(count);
        conn.commit();
    }catch(SQLException e){
        if (conn == null) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        e.printStackTrace();
    }finally {
        //释放资源
        DBUtil.close(conn,ps);
    }

}

以上两段代码,在第一段代码中加断点,执行第二段代码,验证悲观锁

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值