SpringMVC4+Hibernate4运行报错Could not obtain transaction-synchronized Session for current thread
2015-09-14 15:21
513 查看
转自:/article/1571544.html
结合自己的配置问题,第二种解决了我的问题。
查了一下相关资料,整理如下:
原因:Hibernate4 No Session found for current thread原因
解决方法:
1. 在spring 配置文件中加入
![](http://www.yihaomen.com/images/code.gif)
程序代码
<tx:annotation-driven transaction-manager="transactionManager"/>
并且在处理业务逻辑的类上采用注解
![](http://www.yihaomen.com/images/code.gif)
程序代码
@Service
public class CustomerServiceImpl implements CustomerService {
@Transactional
public void saveCustomer(Customer customer) {
customerDaoImpl.saveCustomer(customer);
}
...
}
另外在 hibernate 的配置文件中,也可以增加这样的配置来避免这个错误:
![](http://www.yihaomen.com/images/code.gif)
程序代码
<property name="current_session_context_class">thread</property>
解决方法二:http://zhidao.baidu.com/link?url=BNyi8By29DMOXBCiNLeTa7WTTxlf4Q4IbQkrYO6lv6MFNMxv5zkBIx723t4JkezOaNcxc9ARhDIeXxtRZbID_0jYKonThVemDs1VkBUFmpK
以上亲测,都可以解决!
结合自己的配置问题,第二种解决了我的问题。
查了一下相关资料,整理如下:
原因:Hibernate4 No Session found for current thread原因
解决方法:
1. 在spring 配置文件中加入
![](http://www.yihaomen.com/images/code.gif)
程序代码
<tx:annotation-driven transaction-manager="transactionManager"/>
并且在处理业务逻辑的类上采用注解
![](http://www.yihaomen.com/images/code.gif)
程序代码
@Service
public class CustomerServiceImpl implements CustomerService {
@Transactional
public void saveCustomer(Customer customer) {
customerDaoImpl.saveCustomer(customer);
}
...
}
另外在 hibernate 的配置文件中,也可以增加这样的配置来避免这个错误:
![](http://www.yihaomen.com/images/code.gif)
程序代码
<property name="current_session_context_class">thread</property>
解决方法二:http://zhidao.baidu.com/link?url=BNyi8By29DMOXBCiNLeTa7WTTxlf4Q4IbQkrYO6lv6MFNMxv5zkBIx723t4JkezOaNcxc9ARhDIeXxtRZbID_0jYKonThVemDs1VkBUFmpK
我也出现了这个问题,但是我在web.xml中增加了filter就可以了。也许你的问题不是这个,但我的这个问题是这么解决的。 <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>
以上亲测,都可以解决!
相关文章推荐
- java序列化/反序列化之xml、protobuf、protostuff 的比较与使用例子
- Java序列化框架性能比较
- 使用Java进行双向认证的SSL链接以及使用OpenSSL生产证书链
- JAVA加密&解密——非对称加密算法RSA
- Java parseInt将其它进制整数转换为十进制
- 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本
- struts2文件下载 <result type="stream">
- 【java】0到n中生成m个等概率随机数
- 关于Java中equal函数和==的一些区别
- 关于Java中equal函数和==的一些区别做一个总结,以供自己随时查看复习。
- IBM 关于 Java 深入学习的技术资料
- java 多线程 ReentrantReadWriteLock 使用
- java设计模式学习 ----- 代理模式(Proxy)
- 基于Struts2框架实现登录案例
- Java压缩文件
- 十进制转换为任意进制 java实现
- Spring MVC form errors tag example
- URI in java
- spring中@Scope作用域的注解
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别