有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java解决MySQL死锁问题

服务:我们有一个接收Request的服务,它只是将其保存在数据库中。以下是相同的hbm文件

<hibernate-mapping package="com.project.dao">
        <class name="Request" table="requests">
                <id name="requestId" type="long" column="req_id">
                        <generator class="native" />
                </id>
        <class>
        <discriminator column="req_type" type="string" force="true"/>
        <property name="status" type="string" column="status"/>
        <property name="processId" type="string" column="process_id"/>
        <subclass name="RequestType1" discriminator-value="Type1">
          ..
        </subclass>     
        <subclass name="RequestType2" discriminator-value="Type2">
          ..
        </subclass>     
</hibernate-mapping>

获取会话并保存Request的代码如下

try{
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  Transaction tx = session.beginTransaction();
  requestDAO.save(request);
  tx.commit();
}catch(Exception e){
  log.error(e);
}

客户端:客户端有两台主机读取这些已接收和未处理的请求。每个客户端(主机1、主机2)执行以下操作

将未处理请求的进程id更新为其主机名

update requests set process_id='" + hostName + "' where status='Received' and process_id is null order by req_id asc limit 100"

检索上面更新的请求并处理它们

select * from requests where process_id='"+ hostName + "' where status='Received';

现在的问题是,在一段时间内,这些客户会工作得很好。但一段时间后,他们开始抛出以下异常

org.hibernate.exception.GenericJDBCException: could not execute native bulk manipulation query at  org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)

Caused by: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction

如果我们重新启动客户端,客户端将再次正常工作

在服务端,我们有时也会看到以下异常

org.hibernate.AssertionFailure: null id don't flush the Session after an exception occurs

我们一直试图解决这个问题,但无法找出根本原因。任何对可能问题的洞察都会有所帮助

谢谢


共 (0) 个答案