我知道这个问题已经被问过很多次了,但大多数都无法解释我的问题:
@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
在活动事务上下文中被调用,为什么它仍然抛出错误???
参考这里的代码。