请先看代码
第三种方式:使用拦截器
<!-- 定义事务管理器(声明式的事务) -->
<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注解)感觉没有解耦合不早了,该睡觉了。
晚安!
第三种方式:使用拦截器
<!-- 定义事务管理器(声明式的事务) -->
<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注解)感觉没有解耦合不早了,该睡觉了。
晚安!
解决方案 »
- strong标签在浏览器中为什么不起作用
- servlet路径
- java中,使用jacob操作word,我现在想用空格替换掉word中以“$”开始,并且以“$”结束的字符串,怎么做啊
- 如何在客户端ip发生变化时禁止客户端HttpInvoker(Spring)调用?请大虾指点!
- Struts1.3验证时资源文件如何取得
- 自学于教学的困惑!!!!
- j2ee各层架构技术使用比例表
- insert error! Hibernate: select max(id) from st是什么问题???
- 急,关于往SQL数据库的TEXT字段里写入值的问题
- 看看是什么错误!昨天刚看soap和tomcat出现下面的错误!
- 今天是平安夜,我男朋友却没有陪我过
- 200分:求spring 方法级(细粒度)事务--2个100分的帖子一起给
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>
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
在这个地方设置。
你的这个<prop key="*">PROPAGATION_REQUIRED</prop> 是可以写多个,不过,你最好全部用事务,对于查询的地方你可以用只读事务的形式。
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
<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>
先看看这个。能忙你点帮吧。
<!-- 事务配置 -->
<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>
<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>
下了个spring-reference.pdf英文版的,看的速度很慢,以前基本没看过英文的文档大家请继续!