日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql innodb 多线程插入_mysql innodb 并发插入问题,包大量死锁错误

發布時間:2023/12/4 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql innodb 多线程插入_mysql innodb 并发插入问题,包大量死锁错误 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開了10個并發寫線程,沒1000條記錄批量提交一次,結果mysql包大量死鎖錯誤!

"Deadlock found when trying to get lock; try restarting transaction"

引擎用的是Innodb 主鍵字段是auto_increament.

mysql 有這么脆弱嗎?create table ASIA_ODDS(

id int NOT NULL AUTO_INCREMENT,

match_id INT DEFAULT 0,

match_bet007_id INT NOT NULL,

company_id SMALLINT NOT NULL ,

first_pk DECIMAL(9,4) DEFAULT 0,

first_host_odds DECIMAL(9,4) DEFAULT 0,

first_guest_odds DECIMAL(9,4) DEFAULT 0,

pk DECIMAL(9,4) ,

host_odds DECIMAL(9,4) DEFAULT 0,

guest_odds DECIMAL(9,4) DEFAULT 0,

is_end TINYINT DEFAULT 0,

is_zd TINYINT DEFAULT 0 ,

changeState char(3) DEFAULT '000',

last_update_time timestamp ,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我的測試java代碼如下:public class TestInsertAsiaOdds {

public static void main(String args[]) {

final long s = System.currentTimeMillis();

CyclicBarrier latch = new CyclicBarrier(10, new Runnable() {

public void run() {

System.out.print("耗時:" + (System.currentTimeMillis() - s));

}

});

//10個線程并發插入500w條數據

List lt = new ArrayList();

for (int i = 0; i < 10; i++) {

Thread t = new Thread(new Task(5000000, latch));

lt.add(t);

}

//啟動線程

for (Thread t : lt) {

t.start();

}

}

private static class Task implements Runnable {

private int count;

private CyclicBarrier latch;

private String insSql = "insert into asia_odds2(match_id, match_bet007_id, company_id, first_pk, first_host_odds, first_guest_odds, pk, host_odds, guest_odds, is_end, is_zd, changeState) values(?,?,?,?,?,?,?,?,?,?,?,?)";

public Task(int count, CyclicBarrier latch) {

this.count = count; this.latch = latch;

}

public void run() {

Connection conn = null;

PreparedStatement ps = null;

try {

conn = getConn(); //設為自動提交

conn.setAutoCommit(true);

ps = conn.prepareStatement(insSql);

for (int i = 0; i < count; i++) {

ps.setInt(1, 1);

ps.setInt(2, 2);

ps.setInt(3, 3);

ps.setDouble(4, 1.1);

ps.setDouble(5, 1.1);

ps.setDouble(6, 1.1);

ps.setDouble(7, 1.1);

ps.setDouble(8, 1.1);

ps.setDouble(9, 1.1);

ps.setInt(10, 0);

ps.setInt(11, 0);

ps.setString(12, "000");

ps.addBatch();

//1000一批提交一次

if (i % 1000 == 0) {

ps.executeBatch();

ps.clearBatch();

}

}

ps.executeBatch();

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

try {

latch.await();

} catch (BrokenBarrierException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

運行一段時間后就會報:java.sql.BatchUpdateException: Deadlock found when trying to get lock; try restarting >transaction at >com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669) at >com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的mysql innodb 多线程插入_mysql innodb 并发插入问题,包大量死锁错误的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。