spring整合hibernate事务编程中错误分析

在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包导致

 

 

 

 

 

你可能感兴趣的:(spring整合hibernate事务编程中错误分析)