通常,我们使用Spring Atomikos来管理JTA会话并将hibernate.auto_close_session设置为true . 现在,如果我们手动打开无状态会话(对于某些异步作业),如下所示:
StatelessSession sl = sessionFactory.openStatelessSession();
我们如何关闭StatelessSession?如果我们打电话
sl.close()
然后“currentSession”将回滚 . 如果我们注释
@Transactional(propagation = Propagation.NOT_SUPPORTED)
在使用StatelessSession的方法上,提交挂起,Atomikos日志显示它不断产生新事务并且永不停止 .
完整代码:
public Vendor findByCode(String code) {
StatelessSession slsession = null;
Transaction tx = null;
try {
slsession = getStatelessSession();
tx = slsession.beginTransaction();
return (Vendor) slsession.createQuery("from Vendor"
+ " where code = :code")
.setParameter("code", code)
.uniqueResult();
} catch (HibernateException e) {
e.printStackTrace();
return null;
} finally {
if (slsession != null && tx != null) {
tx.commit();
slsession.close();
}
}
}
已经有一个Spring托管会话,并调用了此方法 .
你能说出什么问题吗?
1 回答
很老的未回答的问题,但在这里可能是一个答案:您必须回滚您的交易,但您不需要关闭会话 . 除非你真的想要 .
有一些工具可以防止你在代码中使用这个注释,但这更像是这样的: