我正在使用WebLogic,Spring和Hibernate。我得到以下异常:
Caused by: org.hibernate.TransactionException: Could not register synchronization for container transaction
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.afterTransactionBegin(CMTTransaction.java:72)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:166)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1471)
at com.abc.framework.xyz.dao.MyDAOImpl.getHibSession(MyDAOImpl.java:209)
造成这种情况的代码是:
private Session getHibSession() {
Session session = getSessionFactory().openSession();
session.beginTransaction(); // this line is causing the issue
我应该使用'openSession'还是'getCurrentSession'?
由于
答案 0 :(得分:0)
对于Spring - Hibernate应用程序,建议打开和关闭会话 例如。你可以自动装配sessionFactory bean然后使用session for a 数据库事务
session=sessionFactory.openSession();
Transaction tx = session.beginTransaction();
CustomObj obj= new CustomObj ();
session.save(obj);
tx.commit();
session.close();
确保关闭finally块中的会话以便没有打开的连接 如果是例外情况。