简体   繁体   English

超过锁定等待超时; 尝试使用JDBC重新启动事务

[英]Lock wait timeout exceeded; try restarting transaction using JDBC

I have a MySQL table named Student with two columns Student_id and name . 我有一个名为Student的MySQL表,其中有两列Student_idname

I am firing two queries using two connection objects, and it is giving me an Exception: 我使用两个连接对象触发两个查询,它给了我一个例外:

Exception in thread "main" java.sql.SQLException: Lock wait timeout 
exceeded; try restarting transaction
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2713)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2663)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:888)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:730)
    at jdbc.ConnectUsingJdbc.main(ConnectUsingJdbc.java:19)

Here is the code that produces the error: 以下是产生错误的代码:

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectUsingJdbc {

    public static void main(String[] args) 
        throws ClassNotFoundException, SQLException{

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/test","root","root");
        Connection connection1 = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/test","root","root");
        connection.setAutoCommit(false);
        connection1.setAutoCommit(false);
        Statement statement = connection.createStatement();
        statement.execute("insert into student values (3,'kamal')");
        Statement statement1 = connection1.createStatement();
        statement1.execute("delete from student where student_id = 3");
        connection.commit();
        connection1.commit();
    }
}

I am trying to delete the row using the connection1 object that I inserted using the other connection object. 我试图使用我使用另一个connection对象插入的connection1对象删除该行。

Why am I getting this error? 为什么我收到此错误?

Modify your code and reorder the executions as follows. 修改您的代码并按如下方式重新排序执行。 It should work fine: 它应该工作正常:

Statement statement = connection.createStatement();
statement.execute("insert into student values (3,'kamal')");
connection.commit();

Statement statement1 = connection1.createStatement();
statement1.execute("delete from student where student_id = 3");
connection1.commit();

The issue is, previously executed insert statement is not committed yet and holding the lock on the table when you are trying to execute a new delete statement creating a deadlock situation inside DB. 问题是,当您尝试执行新的删除语句在DB中创建死锁情况时,先前执行的insert语句尚未提交并在表上保持锁定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL错误:超过了锁定等待超时; 尝试重新启动事务查询 - MySQL Error: Lock wait timeout exceeded; try restarting transaction Query 由于“超过了锁定等待超时,导致事务失败; 尝试重新开始交易” - Transactions fails due to “Lock wait timeout exceeded; try restarting transaction” 超过了锁定等待超时; 尝试使用spring和Mysql重新启动事务Java - Lock wait timeout exceeded; try restarting transaction Java with spring and Mysql 获取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 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? java jdbc事务中超过了锁定等待超时 - lock wait time out exceeded in java jdbc transaction hibernate锁定等待超时超时; - hibernate Lock wait timeout exceeded;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM