【发布时间】:2017-01-01 07:40:57
【问题描述】:
我知道这个问题已经被问过很多次了,但大多数人都无法解释我的问题:
@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的代码。
【问题讨论】:
标签: java hibernate transactions hibernate-session