spring事务处理的几种方式
来源:互联网 发布:淘宝达人运营怎么做 编辑:程序博客网 时间:2024/05/16 07:57
一、声明式事务
1、使用配置文件
建立一个事务性的服务接口
package x.y.service;public interface FooService { Foo getFoo(String fooName); Foo getFoo(String fooName, String barName); void insertFoo(Foo foo); void updateFoo(Foo foo);}
实现上诉接口
package x.y.service;public class DefaultFooService implements FooService { public Foo getFoo(String fooName) { throw new UnsupportedOperationException(); } public Foo getFoo(String fooName, String barName) { throw new UnsupportedOperationException(); } public void insertFoo(Foo foo) { //嵌入数据库语句 throw new UnsupportedOperationException(); } public void updateFoo(Foo foo) { //嵌入数据库语句 throw new UnsupportedOperationException(); }}
添加配置文件代码
<!-- from the file 'context.xml' --><?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.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- this is the service object that we want to make transactional --> <bean id="fooService" class="x.y.service.DefaultFooService"/> <!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- the transactional semantics... --> <tx:attributes> <!-- all methods starting with 'get' are read-only --> <tx:method name="get*" read-only="true"/> <!-- other methods use the default transaction settings (see below) --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface --> <aop:config> <aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/> </aop:config> <!-- don't forget the DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </bean> <!-- similarly, don't forget the PlatformTransactionManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- other <bean/> definitions here --></beans>
上述文件具体解析,详见 Spring2.5-reference.chm手册
注意:在这里要阐明我说的问题,通常我们的service配置文件和dao的配置文件不在同一个目录中,如果,把下面的配置
<tx:advice id="txAdvice" transaction-manager="txManager"> <!-- the transactional semantics... --> <tx:attributes> <!-- all methods starting with 'get' are read-only --> <tx:method name="get*" read-only="true"/> <!-- other methods use the default transaction settings (see below) --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- ensure that the above transactional advice runs for any execution of an operation defined by the FooService interface --> <aop:config> <aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/> </aop:config>
放到dao的配置文件中,那么这个事务处理是不起作用的,需要将这个配置放到service的配置文件中去。
2、基于注解的事务管理
在需要事务的类或者方法上加入如下注解
@Transactional(propagation=Propagation.REQUIRED, rollbackFor=DAOException.class)
同时在配置文件中声明
<tx:annotation-driven transaction-manager="transactionManager" />
同样上面配置也要加在service所在的配置文件中。
- spring事务处理的几种方式
- Spring中的几种事务处理方式
- Spring应用的几种事务处理机制
- Spring事务处理的两种方式
- Spring事务处理的两种方式
- Spring事务处理的两种方式
- Spring事务处理的两种方式 .
- spring配置事务处理的五种方式
- Spring有几种事务处理方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式
- Spring事物配置的几种方式
- Spring获取Bean的几种方式
- spring事物配置的几种方式
- Spring事务的几种配置方式
- Spring 控制反转的几种方式
- Android四大组件
- 一些有关图像处理的代码片段(抓图、倒影、圆角)
- 全排列问题
- 内存Cookie 硬盘Cookie 写入,读取,删除
- 使用面向对象的编程思想写一个简单的控制台计算器程序,放松一下
- spring事务处理的几种方式
- 视图切换总结
- andorid g13 刷机
- Android的GridView控件(二)
- 数论中的若干定理
- 耶鲁大学的七大行为理念
- POJ 2376 Cleaning Shifts 贪心
- C++ 类访问控制public/private/protected
- 和菜鸟一起学linux总线驱动之初识USB系统结构