配置文件里配置,程序里就不用处理事务了
例如
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置哪些方法需要哪些事务 -->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
     <tx:attributes>
     <tx:method name="add*" propagation="REQUIRED"/>
     <tx:method name="remove*" propagation="REQUIRED"/>
     <tx:method name="update*" propagation="REQUIRED"/>
     <tx:method name="*" read-only="true"/>
     </tx:attributes>
    </tx:advice>
    <aop:config>
     <aop:pointcut id="managerMethods" expression="execution (* com.spread.vshop.business.service.*.*(..))"/>
     <aop:advisor advice-ref="txadvice" pointcut-ref="managerMethods"/>
    </aop:config>  

解决方案 »

  1.   

    Spring 的事务控制一般放在业务层DAO层只继承一下HibernateDaoSupport支持
    还是需要注入sessionFactory的虽然DAO层用到了sessionFactory,甚至session,但都知道DAO里声明的都是些基本增删改查方法只有在业务层真正使用他们的时候,事务才被开启,这也比较符合LAZY特性~
      

  2.   

    //事务2.0配置<?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-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean> <aop:config>
    <aop:advisor
    pointcut="execution(* com.yyaccp.ssh.t9.biz.impl.*.*(..))"
    advice-ref="txAdvice" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="delete*" />
    <tx:method name="save*" />
    <tx:method name="update*" />
    <tx:method name="*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
    </tx:advice>
    </beans>
      

  3.   

    spring有声明式事务
    可以在里面设置自己的事务策略
      

  4.   

    单纯在dao里也可以实现事务管理的,主要是采用接口回调来暴露session实现的。
    getHibernateTemplate().execute(
        new HibernateCallback(){
            public Object doInHibernate(Session s){
                s.xxx();
            }
        }
    );
    就ok了。
    另外,象1楼说的直接在service层面上用声明式事务管理也是可以的!