1、编程式事务管理
spring的配置文件
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 事务回滚 -->
<bean id="defaultTransactionDefinition"
class="org.springframework.transaction.support.DefaultTransactionDefinition"></bean>
java代码:
@Resource
private DataSourceTransactionManager transactionManager;
@Resource
private DefaultTransactionDefinition defaultTransactionDefinition; public void delete(String id) throws Exception{
TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition);
try{
this.dao.delete(id);
transactionManager.commit(status);//没有发生异常提交事务
} catch(Exception e){
transactionManager.rollback(status);//发生了异常,回滚事务
log.warn(e.getMessage());
throw e;
}
}
2、基于注解的事务(注解只能作用于实现了接口的service或者dao),作用于类或者方法
spring的配置文件中加入:
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 开启注解的事务配置功能 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:annotation-driven/>
的属性
Attribute | Default | Description |
---|---|---|
transaction-manager |
transactionManager |
Name of transaction manager to use. Only required if the name of the transaction manager is not |
mode |
proxy |
The default mode "proxy" processes annotated beans to be proxied using Spring's AOP framework (following proxy semantics, as discussed above, applying to method calls coming in through the proxy only). The alternative mode "aspectj" instead weaves the affected classes with Spring's AspectJ transaction aspect, modifying the target class byte code to apply to any kind of method call. AspectJ weaving requires spring-aspects.jar in the classpath as well as load-time weaving (or compile-time weaving) enabled. (See Section 7.8.4.5, “Spring configuration” for details on how to set up load-time weaving.) |
proxy-target-class |
false |
Applies to proxy mode only. Controls what type of transactional proxies are created for classes annotated with the |
order |
Ordered.LOWEST_PRECEDENCE |
Defines the order of the transaction advice that is applied to beans annotated with |
代码中加入
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
@Transactional
注解的属性
属性 | 类型 | 描述 |
---|---|---|
传播性(propagation) |
枚举型:Propagation
|
可选的传播性设置 |
隔离性(isolation) |
枚举型:Isolation
|
可选的隔离性级别(默认值:ISOLATION_DEFAULT ) |
只读性(readOnly) |
布尔型 | 读写型事务 vs. 只读型事务 |
超时(timeout) |
int型(以秒为单位) | 事务超时 |
回滚异常类(rollbackFor) |
一组 Class 类的实例,必须是Throwable 的子类 |
一组异常类,遇到时 必须 进行回滚。默认情况下checked exceptions不进行回滚,仅unchecked exceptions(即RuntimeException 的子类)才进行事务回滚。 |
回滚异常类名(rollbackForClassname) |
一组 Class 类的名字,必须是Throwable 的子类 |
一组异常类名,遇到时 必须 进行回滚 |
不回滚异常类(noRollbackFor) |
一组 Class 类的实例,必须是Throwable 的子类 |
一组异常类,遇到时 必须不 回滚。 |
不回滚异常类名(noRollbackForClassname) |
一组 Class 类的名字,必须是Throwable 的子类 |
一组异常类,遇到时 必须不 回滚 |
3、声明性事务
见我的另外一篇博文:http://www.cnblogs.com/yangzhilong/archive/2013/02/04/2891819.html