我正在获取 org.hibernate.TransactionException:事务未成功启动,涉及下面提到的并发执行(多线程)代码。
Session session = null;
Transaction tx = null;
try {
session = getSession();
tx = session.beginTransaction();
Criteria criteria = session.createCriteria(DataItem.class);
criteria.add(Restrictions.eq("displayName", displayName));
DataItem dataItem = (DataItem) criteria.setMaxResults(1).uniqueResult();
return dataItem;
} catch (HibernateException e) {
if ( tx != null ) tx.rollback();
String errorMsg = getResourceString( CONFIG_READ_ERROR );
logger.error( errorMsg, e );
throw new ConfigException( errorMsg, e);
} finally {
if(!tx.wasCommitted()) {
tx.commit();
}
if ( session != null ) session.close();
}
错误从 tx.commit 中弹出,终于出现在代码块中。下面是根错误堆栈跟踪:
org.hibernate.TransactionException:事务未成功启动 在org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:131)
我怀疑事务出错,因为提到的代码块处于并行执行状态。谁能指出故障代码流是什么?
getSession方法
protected Session getSession() {
SessionFactory sessionFactory = BuildSessionFactory.getSessionFactory();
Interceptor interceptor = BuildSessionFactory.getSessionInterceptor();
if (connection != null) return sessionFactory.openSession(connection,interceptor);
return sessionFactory.openSession(interceptor);
}