mysql中的hibernate锁异常

时间:2015-09-13 03:40:10

标签: java mysql hibernate

当我尝试从表中获取实体并尝试更新同一实体时,它会发出错误。

我的示例代码:

public String updateProfile(Hospital hospital)
    {
        System.out.println("here");
        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session=sessionFactory.openSession();
        try{
        Transaction t=session.beginTransaction();
        HttpSession sess=ServletActionContext.getRequest().getSession(false);
        Long hospitalId=(Long)sess.getAttribute("pid");
        Hospital hos = (Hospital)session.get(Hospital.class, hospitalId); 

        hos.setAbout(hospital.getAbout());
        hos.setHospitalName(hospital.getHospitalName());
        session.update(hos); 
        session.flush();

        t.commit();
        }
        catch(HibernateException e)
        {
            e.printStackTrace();
        }
        finally{
            session.close();
        }
        return "success";
    }

我得到的错误是

  

org.hibernate.exception.LockTimeoutException:无法执行语句       在org.hibernate.dialect.MySQLDialect $ 1.convert(MySQLDialect.java:447)       在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)       在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)       at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)       在org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)       在org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3281)       在org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3183)       在org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3525)       在org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:159)       在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)       在org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)       在org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)       在org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)       在org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)

请帮忙。 hHospital对象中的关系是一对多。

0 个答案:

没有答案