在用spring 4.1.2 + hibernate 4.2.0開發時,調用getSessionFactory().getCurrentSession()方法報異常:
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
異常原因:
解決辦法:
在Service層加上一個 @Transactional
的註解,聲明這個service所有方法需要事務管理,每一個業務方法開始時都會打開一個事務。