爲了保持數據的一致性,當全局表更新數據時會經過鎖機制來保證數據統一。
當時這也致使了併發更新的時候死鎖的發生,全局表須要慎用。
如下是測試代碼:java
z_dict表配置的爲全局表
兩個線程併發更新一條記錄
基本上每次都會形成鎖超時的問題sql
public static void testThreadUpdate(DataSourceFactory ds) throws SQLException { Connection con1 = ds.getConnection(); Connection con2 = ds.getConnection(); UpdateThread thread1 = new UpdateThread(con1, "update z_dict set name=? where id=?", new ArrayList<String>() { private static final long serialVersionUID = 1L; { add("Q1"); add("1"); } }); UpdateThread thread2 = new UpdateThread(con2, "update z_dict set name=? where id=?", new ArrayList<String>() { private static final long serialVersionUID = 1L; { add("Q2"); add("1"); } }); thread1.start(); thread2.start(); try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } }
package com.am.mycatclient; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; public class UpdateThread extends Thread { private Connection con; private List<String> values; private PreparedStatement ps; public UpdateThread(Connection con, String updateSql, List<String> values) { super(); this.con = con; this.values = values; try { con.setAutoCommit(false); ps = con.prepareStatement(updateSql); } catch (SQLException e) { e.printStackTrace(); } } @Override public void run() { try { for (int i = 1; i <= values.size(); i++) { ps.setString(i, values.get(i - 1)); } int executeUpdate = ps.executeUpdate(); System.out.println("=="+executeUpdate); con.commit(); ps.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } }