org.hibernate.HibernateException: No Hibernate Session bound to thread,
and configuration does not allow creation of non-transactional one here
DAO 方法在無事務上下文下調用 getCurrentSession(),Spring 未為當前線程綁定 Session。
@Transactional。@Transactional 放在 private/protected 方法上(代理不攔截)。@Transactional。@Transactional(propagation = Propagation.REQUIRES_NEW) 測試。logging.level.org.springframework.transaction=DEBUG。testService() 無業務邏輯,僅調用 DAO,驗證事務配置本身是否生效。<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
</bean>
@Transactional(readOnly = true) 標記查詢方法,優化性能。