Hibernate事务没有成功启动 [英] Hibernate transaction not successfully started

查看:160
本文介绍了Hibernate事务没有成功启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单的Hibernate场景:

  session = getHibernateSession(); 
tx = session.beginTransaction();
SomeObject o =(SomeObject)session.get(SomeObject.class,objectId);
tx.commit();

此代码产生以下异常:

  org.hibernate.TransactionException:事务没有成功启动
在org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
在com.bigco。 package.Clazz.getSomeData(Clazz.java:1234)

发生了什么?

解决方案

好吧,它看起来像一旦我们到达 tx.commit()行,交易已经提交。我唯一的猜测是Hibernate已经在 get()这个对象上提交了事务。



很简单:

  //仅当tx尚未提交时才提交(通过休眠)
if( !tx.wasCommitted())
tx.commit();


Consider this simple Hibernate scenario:

session = getHibernateSession();
tx = session.beginTransaction();
SomeObject o = (SomeObject) session.get(SomeObject.class, objectId);
tx.commit();

This code produces the following exception:

org.hibernate.TransactionException: Transaction not successfully started
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
    at com.bigco.package.Clazz.getSomeData(Clazz.java:1234)

What's going on?

解决方案

Well, it looks like once we reach the tx.commit() line, the transaction has already been committed. My only guess is that Hibernate already commits the transaction when get()ing the object.

The fix for this is simple:

// commit only if tx still hasn't been committed yet (by hibernate)
if (!tx.wasCommitted())
    tx.commit();

这篇关于Hibernate事务没有成功启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆