我理解這個問題已經被問了很多時間,但最不能解釋我的問題:休眠 - 無法獲得交易同步會話當前線程的
@RequestMapping("/testing")
public String testing(HttpServletRequest request, final ModelMap model)
{
Transaction ut = session.openSession().beginTransaction();
session.getCurrentSession(); // error here
ut.commit();
return "testing";
}
爲什麼我收到錯誤
Could not obtain transaction-synchronized Session for current thread.
如果我用@Transactional
註解方法,它工作得很好。因爲我在我的春天環境中有@EnableTransactionManagement
。
我想嘗試一下來了解getCurrentSession
和openSession
之間的區別,所以我在上面創建了測試用例。
getCurrentSession
在活動事務上下文中調用,爲什麼它仍然拋出錯誤???
請參閱this中的代碼。