简体   繁体   English

由于“超过了锁定等待超时,导致事务失败; 尝试重新开始交易”

[英]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.

相关问题 MySQL错误:超过了锁定等待超时; 尝试重新启动事务查询 - MySQL Error: Lock wait timeout exceeded; try restarting transaction Query 超过了锁定等待超时; 尝试使用spring和Mysql重新启动事务Java - Lock wait timeout exceeded; try restarting transaction Java with spring and Mysql 超过锁定等待超时; 尝试使用JDBC重新启动事务 - Lock wait timeout exceeded; try restarting transaction using JDBC java.sql.SQLException:超出了锁定等待超时; 尝试重新启动事务异常在MYSQL中发生 - java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction exception occur in MYSQL 发出java.lang.Exception:超出了锁定等待超时; 尝试重新启动事务 - Issue java.lang.Exception: Lock wait timeout exceeded; try restarting transaction 超过了锁定等待超时; 尝试在spring_Hibernate_Mysql中重新启动事务 - Lock wait timeout exceeded; try restarting transaction in spring_Hibernate_Mysql 如何修复 Mysql 表之前工作时的“超出锁定等待超时;尝试重新启动事务”? - How to fix "Lock wait timeout exceeded; try restarting transaction" for Mysql table when it was working before? 获取WARN:SQL错误:1205,SQLState:41000错误:超出锁定等待超时; 尝试重新启动事务。 使用休眠保存记录 - Getting WARN: SQL Error: 1205, SQLState: 41000 ERROR: Lock wait timeout exceeded; try restarting transaction. Saving a record in using hibernate hibernate锁定等待超时超时; - hibernate Lock wait timeout exceeded; 由于挂起事务 FK 插入,Mysql 事务获得“超出锁定等待超时” - Mysql transaction getting 'Lock wait timeout exceeded' because of pending transaction FK insert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM