Could not obtain transaction-synchronized Session for current thread

简介: 报错信息: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.

介绍一个专注于Java的网站:Java之音

报错信息:

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)

这个错误的提示是无法获取当前transaction-synchronized线程的会话
原因是没有为用到了事务的方法定义事务


比如:在Action中定义了该方法,用于删除数据信息操作:


而在applicationContext.xml中是这样设置的:


这表示只有以do和find打头的方法才能事务,其他方法不走事务,删除自然不能成功,于是报上面的错误。


当然,也可能不是上面这么粗心的原因,如果使用了Hibernate4报错的话,可以在web.xml中加入如下配置,程序也可以正常运行了。

<filter>
	<filter-name>SpringOpenSessionInViewFilter</filter-name>
	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
  <filter-mapping>
    	<filter-name>SpringOpenSessionInViewFilter</filter-name>
   	<url-pattern>/*</url-pattern>
  </filter-mapping>

网上还看到一种解决方法,针对第二种情况的,测试也可行。
1. 在spring 配置文件中加入
2. 在处理业务逻辑的类上采用注解:<tx:annotation-driven transaction-manager="transactionManager"/>

如:

@Service
public class CustomerServiceImpl implements CustomerService {  
    @Transactional
    public void saveCustomer(Customer customer) {
        customerDaoImpl.saveCustomer(customer);
    }
    ...
}


目录
相关文章
|
17天前
|
NoSQL 编译器 API
关于thread使用的错误:pure virtual method called terminate called without an active exception
关于thread使用的错误:pure virtual method called terminate called without an active exception
11 1
|
8月前
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
Jetson Xavier NX 报错 RuntimeError: There is no current event loop in thread 'Thread-1'.异常错误,已解决
135 0
已解决 RuntimeError: There is no current event loop in thread ‘Thread-1‘.
|
4月前
报错modify sync object Modify sync object Failed!
报错modify sync object Modify sync object Failed!
21 1
Could not obtain transaction-synchronized Session for current thread原因及解决方案
Could not obtain transaction-synchronized Session for current thread原因及解决方案
204 1
Could not obtain transaction-synchronized Session for current thread原因及解决方案
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
292 0
Error:svn:E155037:Previous operation has not finished; run ‘cleanup‘ if it was interrupted(完美解决)
|
SQL 关系型数据库 MySQL
Deadlock found when trying to get lock; try restarting transaction
Deadlock found when trying to get lock; try restarting transaction
244 1
2015-03-18 current note update logic in my task
2015-03-18 current note update logic in my task
72 0
2015-03-18 current note update logic in my task
|
Linux
lvm[12446]: Another thread is handling an event. Waiting
在检查一Linux服务器时,发现日志里面有大量“lvm[12446]: Another thread is handling an event. Waiting...” Jul  4 00:01:42 localhost lvm[12446]: Another thread is handling an event.
1009 0