Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 休眠多个更新查询单个事务_Mysql_Hibernate - Fatal编程技术网

Mysql 休眠多个更新查询单个事务

Mysql 休眠多个更新查询单个事务,mysql,hibernate,Mysql,Hibernate,我正在将hibernate与mysql db一起使用,但在单个事务中进行多个更新时遇到问题 这是我的密码 public void updateOEMUser(OEMUserDetailsDTO userDTO) throws Exception{ Session session = GCCPersistenceMangerFactory.getSessionFactory().openSession(); Transaction tx = null; try{

我正在将hibernate与mysql db一起使用,但在单个事务中进行多个更新时遇到问题

这是我的密码

public void  updateOEMUser(OEMUserDetailsDTO userDTO) throws Exception{
        Session session = GCCPersistenceMangerFactory.getSessionFactory().openSession();
    Transaction tx = null;
    try{
        String query = "update oemuserdata set full_name=\""+userDTO.getFullName()+ "\" ,contact_no=\""+userDTO.getContactNo() + "\" where user_id="+userDTO.getUserId();
        String query1 = "update usermasterdata set account_status="+userDTO.getAccountStatus() + " ,user_name=\"" + userDTO.getUserName() + "\" where user_id="+userDTO.getUserId();
        Query q = session.createSQLQuery(query);
        Query q1 = session.createSQLQuery(query1);
        tx = session.beginTransaction();
        q.executeUpdate();
        q1.executeUpdate();
        tx.commit();
    }catch (HibernateException e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace();
        throw new RuntimeException("HibernateException_updateOEMUser");
    }finally {
        session.close(); 
    }
}
代码可以工作,但当我使“q1.executeUpdate()”失败时,“oemuserdata”中的记录在Mysql中被锁定


我做错什么了吗?

尝试使用这种模式

StringBuilder query = new StringBuilder();
query.append("UPDATE  CLASSNAME_FIRST a ,");
query.append("CLASSNAME_SECOND b,");
query.append("SET a.full_name='"+userDTO.getFullName()",");
.....

int var = stmt.executeUpdate(query);  

好吧,我没有找到解决办法,但我发现了这里发生了什么

当我在成功执行q后删除连接时,执行q1.executeUpdate()时抛出异常,在catch块之后,它进入最后一个块并尝试执行session.close(),但由于没有DB连接,因此也会失败


但是Mysql仍然有q.executeUpdate()请求的锁。这就是为什么在Mysql释放锁之前,行会被锁定。

为什么你不使用HQL执行更新?你是如何使q1.executeUpdate()失败的?@will-我在语句中添加了一个断点并断开了DB。我认为查询没有任何问题,因为我的函数不仅正常工作,除非我让它失败,我有另一个函数,其行为与我在查询中使用POJO类和使用session.createQuery()时的行为完全相同。