收到警告:SQL 错误:1205,SQLState:41000 错误:超过锁定等待超时;尝试重新启动事务。使用休眠保存记录

Posted

技术标签:

【中文标题】收到警告: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 【发布时间】:2015-02-18 07:01:44 【问题描述】:

我是 Java Web 应用程序开发的新手,我尝试在 mysql 数据库中使用 hibernate 保存记录,但在尝试保存记录时出现错误。

POJO 类

package defaultpackage;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


public class Sirs  implements java.io.Serializable 


 private int sirsid;
 private String sirsDescription;
 private String submitter;

public Sirs() 



public Sirs(int sirsid) 
    this.sirsid = sirsid;

public Sirs(int sirsid, String sirsDescription, String submitter) 
   this.sirsid = sirsid;
   this.sirsDescription = sirsDescription;
   this.submitter = submitter;

@GeneratedValue(strategy=GenerationType.TABLE)
public int getSirsid() 
    return this.sirsid;


public void setSirsid(int sirsid) 
    this.sirsid = sirsid;

public String getSirsDescription() 
    return this.sirsDescription;


public void setSirsDescription(String sirsDescription) 
    this.sirsDescription = sirsDescription;

public String getSubmitter() 
    return this.submitter;


public void setSubmitter(String submitter) 
    this.submitter = submitter;


sirs.hbm.xml 文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 12 Dec, 2014 1:32:06 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="defaultpackage.Sirs" table="sirs" catalog="sirsdb" optimistic-lock="version">
    <id name="sirsid" type="int">
        <column name="SIRSID" />
        <generator class="assigned" />
    </id>
    <property name="sirsDescription" type="string">
        <column name="`SIRS Description`" length="45" />
    </property>
    <property name="submitter" type="string">
        <column name="Submitter" length="45" />
    </property>
</class>
</hibernate-mapping>

Hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">       
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sirsdb?
zeroDateTimeBehavior=convertToNull</property>             
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="defaultpackage/Sirs.hbm.xml"/>
</session-factory>
</hibernate-configuration>

addsirs.jsp 代码

<%@page import="defaultpackage.Sirs"%>
<!-- A jsp to insert record through hibernate -->
<%@ page import="java.util.*,org.hibernate.*,org.hibernate.cfg.*,
org.hibernate.boot.registry.StandardServiceRegistryBuilder,
org.hibernate.service.ServiceRegistry, org.hibernate.SessionFactory" %>
<%!
int sirsid;String submitter;String sirsDescription; Session session1 = null;
%>
<body>
<%
String num1=request.getParameter("t1");
if(num1 != null)

out.println("<h1>Data</h1>");
sirsid=Integer.parseInt(num1);
sirsDescription=request.getParameter("t2");
submitter=request.getParameter("t3");
try

Configuration conf = new Configuration().configure("hibernate.cfg.xml");

ServiceRegistry sr = new
StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();

SessionFactory sf = conf.buildSessionFactory(sr);

Session session1 = sf.openSession();
Sirs e=new Sirs(sirsid,sirsDescription,submitter);
Transaction transaction = session1.beginTransaction();
session1.save(e);
//session1.flush();
transaction.commit();
session1.close();
out.println("<h1>Data Inserted Successfully</h1>");

catch(Exception e)

System.out.println("e="+e.getMessage());


%>

<form>
<table  border="1">
<tr>
  <th>SIRS ID</th>
  <td><input name="t1" type="text"></td>
</tr>
<tr>
  <th> Description </th>
  <td><input name="t2" type="text"></td>
</tr>
<tr>
  <th> Submitter </th>
  <td><input name="t3" type="text"></td>
</tr>
<tr>
  <th colspan="2"><input type="submit"value="Submit" >
  </th>
</tr>
</table>
</form>
</body>
</html>

web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-   
app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

Glassfish 服务器窗口

Info:   Hibernate: insert into sirsdb.sirs (`SIRS Description`, Submitter, SIRSID) values (?, ?,      
?)
WARN:   SQL Error: 1205, SQLState: 41000
ERROR:   Lock wait timeout exceeded; try restarting transaction
Info:   HHH000010: On release of batch it still contained JDBC statements
Info:   e=could not execute statement

请帮忙!!

【问题讨论】:

我尝试将 innodb_lock_wait_timeout 从 50 增加到 200 但没有帮助。 【参考方案1】:

    您能否检查您的应用程序的先前实例是否仍在运行,或者数据库浏览工具是否已打开并处于事务中间?打开终端浏览和处理同一张表could be the root cause。 你也可以在mysql命令行上执行show full processlist。这些通常会显示锁定查询的完整 sql。

    如果这没有帮助,请验证 mysql 服务是否在更新超时后重新启动。

    使用最大池大小为 10 的休眠连接池。这里是 example 说明如何操作。

【讨论】:

感谢您的解决方案 :) SQL 进程正在运行导致锁定。非常感谢!!

以上是关于收到警告:SQL 错误:1205,SQLState:41000 错误:超过锁定等待超时;尝试重新启动事务。使用休眠保存记录的主要内容,如果未能解决你的问题,请参考以下文章

@Column(unique = true) 产生一个 WARN o.h.engine.jdbc.spi.SqlExceptionHelper:SQL 警告代码:0,SQLState:00000

Error!: SQLSTATE[00000] [1040] Too many connections是啥意思

Cakephp SQLSTATE [42000]:语法错误或访问冲突:1064

SQL0103 数字文字“00XAB”无效 SQLSTATE = 42604

DB2 错误代码 命令大全

在 Laravel 项目中收到“SQLSTATE [HY000] [2002] 连接被拒绝错误”