mysql資料庫行級鎖的使用(二)

來源:互聯網
上載者:User

標籤:

項目上的另外一個需求是:

在做統計的時候需要將當前表鎖定不能更新當前表記錄

直接上代碼

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

mysql資料庫行級鎖的使用(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.