当前位置:编程学习 > JAVA >>

刚用Spring mvc 碰上Spring管理hibernate事务的奇怪问题

最近刚学Spring3 mvc的开发 碰上一个非常古怪的问题 求大家解答

第一种情况
web上下文配置数据连接文件

<?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: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.xsd
http://www.springframework.org/schema/tx   
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
http://www.springframework.org/schema/aop   
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- Root Context: defines shared resources visible to all other web components -->
    <!-- 引入配置文件 -->  
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:jdbc.properties</value>  
            </list>  
        </property>  
    </bean> 
<!-- 数据源 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
       <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
       <property name="url" value="jdbc:mysql://localhost:3306/demo" />  
       <property name="username" value="root" />         
       <property name="password" value="root" />  
   </bean>  

    <!--hibernate工厂 -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="mappingResources">  
            <list>  
                <value>routeserver.hbm.xml</value>  
            </list>  
        </property>  
        <property name="hibernateProperties">   
            <props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
        </property>  
    </bean>
    
    <!-- 声明式事务 -->  
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
<aop:config >
<aop:advisor pointcut= "execution(* com.demo.services.*.*(..))" advice-ref="txAdvice"/>
</aop:config>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"
isolation="READ_COMMITTED" read-only="false"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
</beans>


DAO类

@Repository
public class RouteServerDao implements IRouteServerDao {
private SessionFactory sessionFactory;

@Autowired
public RouteServerDao(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
private Session currentSession(){
return sessionFactory.getCurrentSession();
}

@Override
public void insertRouteServer(RouteServer server){
Session session = currentSession();
session.save(server);
}
}



这种情况下 在Controller里调用 insertRouteServer 方法后 数据并没有持久化到数据库中 
改成下面这样才能提交事务到数据库

Session session = currentSession();
session.save(server);
session.flush()


后来我又把RouteServerDao配置到web上下文配置数据连接文件中

<bean id="RouteServerDAO" class="com.demo.services.RouteServerDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

DAO改为

@Repository
public class RouteServerDao implements IRouteServerDao {
private SessionFactory sessionFactory;

public RouteServerDao(){

}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

private Session currentSession(){
return sessionFactory.getCurrentSession();
}

@Override
public void insertRouteServer(RouteServer server){
Session session = currentSession();
session.save(server);
}


这样当controller调用insertRouteServer时方法结束时数据就自动提交到数据库中
现在非常困惑的是 这两种写法有什么区别,为什么第一种写法方法没有受到Spring事务的托管呢
@Autowired的意思不就是吧相同类型的类注入到类中么

注:@Autowired 引入的是import org.springframework.beans.factory.annotation.Autowired

谢谢大家解答了 Spring Hibernate 管理 事务 Spring MVC
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,