架个spring4+hibernate4的demo,dao层直接注入的sessionFactory,而后用getCurrentSession方法获取session,而后问题来了,直接报错:spring
Could not obtain transaction-synchronized Session for current thread
提示没法获取当前线程的事务同步session,略微奇怪,这和事务有什么关系..而后百度一下有人说改为用openSession方法就行了,那我又百度了一下这2个方法的区别:api
(1)openSession每次打开都是新的Session,因此屡次获取的Session实例是不一样的,而且须要人为的调用close方法进行Session关闭。 (2)getCurrentSession是从当前上下文中获取Session而且会绑定到当前线程,第一次调用时会建立一个Session实例,若是该Session未关闭,后续屡次获取的是同一个Session实例;事务提交或者回滚时会自动关闭Sesison,无需人工关闭。
看起来这个getCurrentSession方法的确和事务有点关系.而后我加上事务:session
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>
而后在dao层加上@Transaction注解,再运行ok了..不过好奇驱使吧,看了一下getCurrentSession的源码(个人demo中用的spring的实现类),关键点:this
if (TransactionSynchronizationManager.isSynchronizationActive()) { Session session = this.sessionFactory.openSession(); if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { session.setFlushMode(FlushMode.MANUAL); } SessionHolder sessionHolder = new SessionHolder(session); TransactionSynchronizationManager.registerSynchronization( new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true)); TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder); sessionHolder.setSynchronizedWithTransaction(true); return session; } else { throw new HibernateException("Could not obtain transaction-synchronized Session for current thread"); }
而后点进去看了一下isSynchronizationActive()方法:spa
public static boolean isSynchronizationActive() { return (synchronizations.get() != null); }
get方法说明:hibernate
Returns the value in the current thread's copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of the initialValue method.
而后再看initialValue的说明:线程
This implementation simply returns null; if the programmer desires thread-local variables to have an initial value other than null, ThreadLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used.
到此问题明了了,补充一点以前配置文件中配了事务,不过是原来那种在配置中根据方法名字来定义事务传播的方式,可是在dao中并无继承它,故实际 上是没有事务的,只有实现.而在spring的事务实现中须要判断当前线程中的事务是否同步,而没有事务的时候,那个判断是否同步的方法会由于get返回 初始的null值而返回false,最终致使throw一个Could not obtain transaction-synchronized Session for current thread的异常.code
综上:spring4+hibernate4,使用hibernate的api的时候须要配置事务的,若是不配置事务会致使获取当前session抛出异常.orm