[英]Transactions fails due to “Lock wait timeout exceeded; try restarting transaction”
When trying to update a certain table, it fails with the exception of: "Lock wait timeout exceeded; try restarting transaction". 尝试更新某个表时,它失败,但以下情况除外:“超出了锁定等待超时;尝试重新启动事务”。 Some info: I have two tables, profile and profile_units. 一些信息:我有两个表,profile和profile_units。 ID is the primary key of the profile table, and ID is part of a primary key in profile_units, and is also a foreign key to ID in profile. ID是概要文件表的主键,ID是profile_units中的主键的一部分,也是概要文件中ID的外键。 Whan I call saveProfileChanges, the updateAllFields method succeeds, but the addStmt.executeUpdate(); 我调用saveProfileChanges时,updateAllFields方法成功,但是addStmt.executeUpdate();成功。 in handleActivityUnitsChanges fails with the above exception. 在handleActivityUnitsChanges中失败,但出现上述异常。 I use MySQL v5.0 for a data base. 我使用MySQL v5.0作为数据库。 What am I doing wrong? 我究竟做错了什么?
I try to execute the following code: 我尝试执行以下代码:
public static Profile saveProfileChanges(Profile profile, List unitsToAdd)
throws Exception
{
Connection con = null;
try
{
con = ConnectionManager.getConnection();
updateAllFields(con, profile);
handleActivityUnitsChanges(con, profile, unitsToAdd);
con.commit();
return profile;
}
finally
{
ConnectionManager.closeConnection(con);
}
}
private static void handleActivityUnitsChanges(Connection con, Profile profile, List<ActivityUnit> unitsToAdd) throws Exception
{
PreparedStatement addStmt = null;
try
{
for (ActivityUnit currentUnitToAdd : unitsToAdd)
{
String sqlStatement = "insert into profile_units (ID, ActivityUnit) values (?, ?)";
addStmt = con.prepareStatement(sqlStatement);
addStmt.setLong(1, profile.getId());
addStmt.setLong(2, currentUnitToAdd.getId());
System.out.println(sqlStatement);
addStmt.executeUpdate();
addStmt.close();
}
}
catch (Exception e)
{
con.rollback();
throw e;
}
finally
{
ConnectionManager.closeStatement(addStmt);
}
}
public static Connection getConnection() throws Exception
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql:///someproject", "a", "a");
con.setAutoCommit(false);
return con;
}
OK, I figured out what the problem was - In updateAllFields (which for some odd reason i didn't showed here) I obtained a new connection, so the two transactions got mixed. 好的,我发现了问题所在-在updateAllFields中(出于某种奇怪的原因,我没有在此处显示),我获得了一个新的连接,因此这两个事务混合在一起了。 Thanks for the help anyone! 感谢任何人的帮助!
Take a look at innodb_lock_wait_timeout variable. 看一下innodb_lock_wait_timeout变量。 It is - the timeout in seconds an InnoDB transaction may wait for a lock before being rolled back . 这是-InnoDB事务在回滚之前可能等待锁定的超时时间(以秒为单位) 。 The default value is 50 seconds, you could increase this value; 默认值为50秒,您可以增加此值; but I think is better to rewrite loop-inserts code or to optimize something. 但我认为最好重写循环插入代码或进行优化。
As a variant - try to run less insert statements in transaction. 作为一种变体-尝试在事务中运行较少的插入语句。 To speed up transaction you could use multiple inserts, eg - 'INSERT INTO table1(column1, column2) VALUES(1,'text1'),(2,'text2')...;' 为了加快交易,您可以使用多个插入,例如-'INSERT INTO table1(column1,column2)VALUES(1,'text1'),(2,'text2')...;'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.