请先看代码
第三种方式:使用拦截器
    <!-- 定义事务管理器(声明式的事务) -->  
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean> 
   
    <bean id="transactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor">  
        <property name="transactionManager" ref="transactionManager" />  
        <!-- 配置事务属性 -->  
        <property name="transactionAttributes">  
            <props>  
                <prop key="*">PROPAGATION_REQUIRED</prop>  
            </props>  
        </property>  
    </bean>
      
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
        <property name="beanNames">  
            <list>  
                <value>*Service</value>
            </list>  
        </property>  
        <property name="interceptorNames">  
            <list>  
                <value>transactionInterceptor</value>  
            </list>  
        </property>  
    </bean>  请问如何只给特定的方法加上事务,比如userService中有addUser(),selectAll()等方法
只给addUser()加上事务,而selectAll(),不加事务,
请问怎么实现啊,最好用这种拦截器式的事务。

第四种方式:使用tx标签配置的拦截器
第五种方式:全注解(在Service上需加上@Transactional注解)感觉没有解耦合不早了,该睡觉了。
晚安!

解决方案 »

  1.   

    java技术交流群100756746,希望大家加入,我们共同讨论,共同进步!
      

  2.   

    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="nestedTransactionAllowed" value="false" />
    </bean> <aop:config proxy-target-class="true">
    <aop:advisor
    pointcut="execution(*Service.*(..))"
    advice-ref="txAdvice" />
    </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="addUser" rollback-for="Exception"
    propagation="REQUIRED" />
    <tx:method name="selectAll" rollback-for="Exception"
    propagation="REQUIRED" /> </tx:attributes>
    </tx:advice>
      

  3.   

    学习……spring 刚接触,不过帮你顶……
      

  4.   

     <property name="transactionAttributes">  
                <props>  
                    <prop key="*">PROPAGATION_REQUIRED</prop>  
                </props>  
            </property>  
    在这个地方设置。
    你的这个<prop key="*">PROPAGATION_REQUIRED</prop> 是可以写多个,不过,你最好全部用事务,对于查询的地方你可以用只读事务的形式。
      

  5.   

    6楼是正解~~

    <property name="transactionAttributes">
       <!--  下面定义事务传播属性-->
       <props>
         <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
         <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
       </props>
    </property>
      

  6.   

    不对啊
    <property name="beanNames">
    <list>
    <value>UserService</value>
    </list>
    </property>
    包含addUser()方法的service必须,加在<property name="beanNames">这里面被spring事务管理到
    这样一来,UserService里面的方法都加上了事务是不是可以这样理解:"
        1,这种方式,只能对整个Servcie加事务,不能对单个方法加事务
        2,但是可以对Service里面的方法价不同的方法
       比如
         <prop key="save*">PROPAGATION_REQUIRED</prop>
        <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
      

  7.   

    http://linshiquan.javaeye.com/blog/260549
    先看看这个。能忙你点帮吧。
      

  8.   

    楼主对着自己需要的改  需要事务的就配置  不需要的就不配置
    <!-- 事务配置 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- 查询方法只读 -->
    <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
    <!-- 其它方法使用普通事务 -->
    <tx:method name="insert*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    </tx:attributes>
    </tx:advice>

    <aop:config>
    <aop:pointcut expression="execution(* org.ssh.service.*.*(..))" id="interCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="interCut"/>
    </aop:config>
      

  9.   

    wnjok  凌晨还在学习  强
      

  10.   

    如果是操作的话用这个了PROPAGATION_REQUIRED,select就设置SUPPORTS,read-only=true
      

  11.   

    <aop:config proxy-target-class="true">
    <aop:pointcut id="outticketfailMethods" expression="execution(* com.umessage.ticket.task.outticketfail.service.*.*(..))" />
    <aop:advisor advice-ref="txOutticketfailMethods" pointcut-ref="outticketfailMethods" />
    </aop:config>

    <tx:advice id="txOutticketfailMethods" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true" />
    <tx:method name="query*" read-only="true" />
    <tx:method name="find*" read-only="true" />
    <tx:method name="*" />
    </tx:attributes>
    </tx:advice>
      

  12.   

    不好意思,这2天没回帖子,我在恶补spring-aop
    下了个spring-reference.pdf英文版的,看的速度很慢,以前基本没看过英文的文档大家请继续!
      

  13.   

    可以不对所有方法进行事务处理的,可以在spring配置文件里加上 <aop:pointcut id="allServiceManager" expression="execution(* ssh2.test.service.*.*(..))"/>,名字自己改