spring整合hibernate事务编程中错误分析
2014-01-10 00:00
381 查看
摘要: HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation
在spring整合hibernate过程中,我们的配置文件:
利用Transactional 开启事务
调用代码
测试过程中总是报错:HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation
问题分析:
在spring中我们 通过配置文件,来获取Hibernate的 SessionFactory,并将这个sessionfactory放在txmanager这个bean中管理,
我们在addLog中没有开启事务,没有结束事务,这个就是spring通过AOP在我们的save逻辑之前和之后加了事务的逻辑方法。
我们知道AOP是通过动态代理(dynamc proxy)实现的,而dynamic prox又有两种实现方式:proxy,和cglib实现
proxy实现需要类实现接口,cglib实现是直接在修改二进制码
可以看出我这里是没有加cglib的jar包导致
在spring整合hibernate过程中,我们的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.spring"></context:component-scan> <!-- <context:property-placeholder location="classpath*:properties/jdbc.property" /> --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath*:properties/jdbc.properties"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" > <property name="url" value="${jdbc.url}"/> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="SessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>com.spring.model.User</value> <value>com.spring.model.Log</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key=" hibernate.format_sql">true</prop> </props> </property> </bean> <!--利用Hibernate的事物,将sessionfactory注入 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="SessionFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
利用Transactional 开启事务
@Transactional public void add(User user) { if (userDAO == null||logDAO==null) System.out.println("erro"); else { userDAO.add(user); logDAO.addLog(new Log()); } }
调用代码
public class LogDAOImpl implements LogDAO { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /* * 利用Spring——hibernate的事务bean管理事务, * 写了@Transaction相当于在方法之前加了beginTranscation * 后加了commit(),当然之前已经获取到session, * 不再需要获取session * @see com.spring.dao.LogDAO#addLog(com.spring.model.Log) */ @Override public void addLog(Log log) { // TODO Auto-generated method stub // System.out.println("user added successful"); Session session=sessionFactory.getCurrentSession(); log.setMes("save success by transaction too long may be cancel"); session.save(log); } }
测试过程中总是报错:HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation
问题分析:
在spring中我们 通过配置文件,来获取Hibernate的 SessionFactory,并将这个sessionfactory放在txmanager这个bean中管理,
我们在addLog中没有开启事务,没有结束事务,这个就是spring通过AOP在我们的save逻辑之前和之后加了事务的逻辑方法。
我们知道AOP是通过动态代理(dynamc proxy)实现的,而dynamic prox又有两种实现方式:proxy,和cglib实现
proxy实现需要类实现接口,cglib实现是直接在修改二进制码
可以看出我这里是没有加cglib的jar包导致
相关文章推荐
- 关于spring4和hibernate4整合,配置事务报“Cannot unwrap to requested type [javax.sql.DataSource]”错误的解决方法及心得
- hibernate+spring整合增删改事务错误
- struts,Hibernate,Spring三大框架整合错误分析
- 【spring 6】Spring和Hibernate的整合:编程式事务
- Hibernate整合进spring---使用自己的事务处理
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十:Spring mvc & hibernate 事务处理(回滚)
- Spring4.1.5 和 hibernate 4.3.7 整合出现MemorySafeUndeclaredThrowableStrategy,ClassNotFound错误
- SpringMVC+Mybatis+Spring整合中org.springframework.beans.factory.BeanCreationException错误分析
- 【spring 6】Spring和Hibernate的整合:编程式事务
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- 关于Spring3与Hibernate4整合错误:java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
- Spring整合Hibernate开发 - 使用事务管理器操作数据库 - 主配置文件
- hibernate整合进spring后的事务处理
- spring和hibernate整合,事务管理
- 关于flex和spring+hibernate整合web.xml的错误
- red5-hibernate-spring事务(aop)整合
- 解决在Spring整合Hibernate配置tx事务管理器出现错误的问题
- hibernate整合进spring后的事务处理