要在出现错误时回滚,我使用javax.ejb.SessionContext来做到这一点,
首先我标记一个要回滚的点sessionContext.getUserTransaction().begin()
然后我使用sessionContext.getUserTransaction().rollback()
回滚并sessionContext.getUserTransaction().commit();
使用should complete transaction before returning.
但问题是如果没有任何改变,这将抛出org.hibernate.exception.GenericJDBCException: could not prepare statement
. 结论:无论如何要停止 SessionContext ?