zoukankan      html  css  js  c++  java
  • mysql数据库行级锁的使用(二)

    项目上的另外一个需求是:

    在做统计的时候需要将当前表锁定不能更新当前表记录

    直接上代码

    package com.robert.RedisTest;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Calendar;
    import java.util.concurrent.TimeUnit;
    
    public class JDBCCountLockTest {
        
        private static String jdbcUrl = "jdbc:mysql://localhost:3306/test";
        private static String username = "test";
        private static String password = "test";
        
        public static void main(String[] args) {
            
            new Thread(new Runnable(){
                public void run(){                    
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        Connection connection = DriverManager.getConnection(jdbcUrl,username,password);
                        connection.setAutoCommit(false);
                        Statement st = connection.createStatement();
                        st.executeQuery("select count(1) num from table_name where mobile_phone = '13651969037' and rule_id='39' for update");
                        TimeUnit.SECONDS.sleep(5);
                        connection.commit();
                        System.out.println("Thread 1 commit "+Calendar.getInstance().getTime());
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            
            new Thread(new Runnable(){
                public void run(){                    
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        Connection connection = DriverManager.getConnection(jdbcUrl,username,password);
                        connection.setAutoCommit(false);
                        Statement st = connection.createStatement();
                        TimeUnit.SECONDS.sleep(1);
                        st.executeQuery("select * from table_name where id=4139 for update");
                        System.out.println("Thread 2 executeQuery finish "+Calendar.getInstance().getTime());
                        String update_sql_1 = "update table_name set rule_id='40'  where id = '4139'";
                        st.executeUpdate(update_sql_1);
                        System.out.println("Thread 2 executeUpdate finish "+Calendar.getInstance().getTime());
                        connection.commit();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    在Thread 2中 先sleep 1s

    保证了Thread 1先执行

    然后在Thread 1执行结束之后,Thread 2才能执行更新操作。

    执行结果如下:

    Thread 1 commit Thu Sep 01 15:58:51 CST 2016
    Thread 2 executeQuery finish Thu Sep 01 15:58:51 CST 2016
    Thread 2 executeUpdate finish Thu Sep 01 15:58:51 CST 2016

  • 相关阅读:
    求大神回答这个管理系统不知道为啥不成功急!
    这个函数到底什么意思如何调用
    判断浮点数是否为零的问题
    字符串与列表的 常用方法
    变量名命名规范 运算符 流程控制
    ACM C++
    struts s:iterator循环遍历数据 自动生成序号
    JAVA将一个EXCEL多行订单产品字符串分解成一个个子订单 +连接符连接
    JS在HTML中获取到所有选中的checkbox的值
    自己做的java-WEB项目。希望360浏览器能够默认使用极速模式打开
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5830286.html
Copyright © 2011-2022 走看看