`
gauss2008
  • 浏览: 40264 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

SessionFactory.getCurrentSession

    博客分类:
  • java
阅读更多

在使用SessionFactory.getCurrentSession进行hibernate操作的时候,遇到org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here的错误,

查看spring源码:

private static Session doGetSession(

SessionFactory sessionFactory, Interceptor entityInterceptor,

SQLExceptionTranslator jdbcExceptionTranslator, boolean allowCreate)

throws HibernateException, IllegalStateException {

 

Assert.notNull(sessionFactory, "No SessionFactory specified");

 

SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);

if (sessionHolder != null && !sessionHolder.isEmpty()) {

// pre-bound Hibernate Session

Session session = null;

if (TransactionSynchronizationManager.isSynchronizationActive() &&

sessionHolder.doesNotHoldNonDefaultSession()) {

// Spring transaction management is active ->

// register pre-bound Session with it for transactional flushing.

session = sessionHolder.getValidatedSession();

if (session != null && !sessionHolder.isSynchronizedWithTransaction()) {

logger.debug("Registering Spring transaction synchronization for existing Hibernate Session");

TransactionSynchronizationManager.registerSynchronization(

new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false));

sessionHolder.setSynchronizedWithTransaction(true);

// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session

// with FlushMode.NEVER, which needs to allow flushing within the transaction.

FlushMode flushMode = session.getFlushMode();

if (flushMode.lessThan(FlushMode.COMMIT) &&

!TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {

session.setFlushMode(FlushMode.AUTO);

sessionHolder.setPreviousFlushMode(flushMode);

}

}

}

else {

// No Spring transaction management active -> try JTA transaction synchronization.

session = getJtaSynchronizedSession(sessionHolder, sessionFactory, jdbcExceptionTranslator);

}

if (session != null) {

return session;

}

}

 

logger.debug("Opening Hibernate Session");

Session session = (entityInterceptor != null ?

sessionFactory.openSession(entityInterceptor) : sessionFactory.openSession());

 

// Use same Session for further Hibernate actions within the transaction.

// Thread object will get removed by synchronization at transaction completion.

if (TransactionSynchronizationManager.isSynchronizationActive()) {

// We're within a Spring-managed transaction, possibly from JtaTransactionManager.

logger.debug("Registering Spring transaction synchronization for new Hibernate Session");

SessionHolder holderToUse = sessionHolder;

if (holderToUse == null) {

holderToUse = new SessionHolder(session);

}

else {

holderToUse.addSession(session);

}

if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {

session.setFlushMode(FlushMode.NEVER);

}

TransactionSynchronizationManager.registerSynchronization(

new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true));

holderToUse.setSynchronizedWithTransaction(true);

if (holderToUse != sessionHolder) {

TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);

}

}

else {

// No Spring transaction management active -> try JTA transaction synchronization.

registerJtaSynchronization(session, sessionFactory, jdbcExceptionTranslator, sessionHolder);

}

 

// Check whether we are allowed to return the Session.

if (!allowCreate && !isSessionTransactional(session, sessionFactory)) {

closeSession(session);

throw new IllegalStateException("No Hibernate Session bound to thread, " +

   "and configuration does not allow creation of non-transactional one here");

}

 

return session;

}

然后在查看日志会发现session在打开后马上close了,从上面的代码中不难看出,session必须存在于某个transation中,仔细检查你的配置,应该不难找出问题了。

 

 

在spring2.5中用AbstractTransactionalJUnit4SpringContextTests进行TU测试的时候很难发现这个问题,因为他本身会打开一个stransaction

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics