Hibernate save()和事务回滚 [英] Hibernate save() and transaction rollback
问题描述
update()
或 delete()
方法中不会发生,只需 save()
。下面是我使用的代码:
DbEntity dbEntity = getDbEntity();
HibernateUtil.beginTransaction();
会话会话= HibernateUtil.getCurrentSession();
session.save(dbEntity);
HibernateUtil.rollbackTransaction();
这里是 HibernateUtil 类(仅涉及函数,我保证 getSessionFactory()
方法运行良好 - 有一个Interceptor处理程序,但现在并不重要):
private static final ThreadLocal< Session> threadSession = new ThreadLocal< Session>();
private static final ThreadLocal< Transaction> threadTransaction = new ThreadLocal< Transaction>();
/ **
*检索线程本地的当前会话。
*< p />
*如果没有Session打开,则为正在运行的线程打开一个新的Session。
*
* @return Session
* /
public static Session getCurrentSession()
throws HibernateException {
Session s =(Session)threadSession.get );
尝试{
if(s == null){
log.debug(打开此线程的新会话。);
if(getInterceptor()!= null){
log.debug(Using interceptor:+ getInterceptor()。getClass());
s = getSessionFactory()。openSession(getInterceptor());
} else {
s = getSessionFactory()。openSession();
}
threadSession.set(s);
}
} catch(HibernateException ex){
throw new HibernateException(ex);
}
return s;
}
/ **
*开始一个新的数据库事务。
* /
public static void beginTransaction()
throws HibernateException {
Transaction tx =(Transaction)threadTransaction.get();
尝试{
if(tx == null){
log.debug(在此线程中启动新的数据库事务。);
tx = getCurrentSession()。beginTransaction();
threadTransaction.set(tx);
}
} catch(HibernateException ex){
throw new HibernateException(ex);
}
}
/ **
*回滚数据库事务。
* /
public static void rollbackTransaction()
throws HibernateException {
Transaction tx =(Transaction)threadTransaction.get();
尝试{
threadTransaction.set(null);
if(tx!= null&&!tx.wasCommitted()&&!tx.wasRolledBack()){
log.debug(Tyring回滚此线程的数据库事务。 );
tx.rollback();
}
} catch(HibernateException ex){
throw new HibernateException(ex);
} finally {
closeSession();
谢谢
In Hibernate when i save()
an object in a transaction, and then i rollback it, the saved object still remains in the DB. It's strange because this issue doesn't happen with the update()
or delete()
method, just with save()
.
Here is the code i'm using:
DbEntity dbEntity = getDbEntity();
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getCurrentSession();
session.save(dbEntity);
HibernateUtil.rollbackTransaction();
And here is the HibernateUtil class (just the involved functions, i guarantee the getSessionFactory()
method works well - there is an Interceptor handler, but it doesn't matter now):
private static final ThreadLocal<Session> threadSession = new ThreadLocal<Session>();
private static final ThreadLocal<Transaction> threadTransaction = new ThreadLocal<Transaction>();
/**
* Retrieves the current Session local to the thread.
* <p/>
* If no Session is open, opens a new Session for the running thread.
*
* @return Session
*/
public static Session getCurrentSession()
throws HibernateException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
return s;
}
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
tx = getCurrentSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
}
/**
* Rollback the database transaction.
*/
public static void rollbackTransaction()
throws HibernateException {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
log.debug("Tyring to rollback database transaction of this thread.");
tx.rollback();
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
} finally {
closeSession();
}
}
Thanks
Check if your database supports a roll back i.e. if you're using InnoDB tables and not MyISAM (you can mix transactional and non-transactional tables but in most cases, you want all your tables to be InnoDB).
这篇关于Hibernate save()和事务回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!