spring 编程控制事务
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action);
}
else {
TransactionStatus status = this.transactionManager.getTransaction(this);
T result;
try {
result = action.doInTransaction(status);
}
catch (RuntimeException ex) {
// Transactional code threw application exception -> rollback
rollbackOnException(status, ex);
throw ex;
}
catch (Error err) {
// Transactional code threw error -> rollback
rollbackOnException(status, err);
throw err;
}
catch (Exception ex) {
// Transactional code threw unexpected exception -> rollback
rollbackOnException(status, ex);
throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");
}
this.transactionManager.commit(status);
return result;
}
}
来看一下PlatformTransactionManager interface是怎么声明的
public interface PlatformTransactionManager {
/**
* Return a currently active transaction or create a new one, according to
* the specified propagation behavior.
* <p>Note that parameters like isolation level or timeout will only be applied
* to new transactions, and thus be ignored when participating in active ones.
* <p>Furthermore, not all transaction definition settings will be supported
* by every transaction manager: A proper transaction manager implementation
* should throw an exception when unsupported settings are encountered.
* <p>An exception to the above rule is the read-only flag, which should be
* ignored if no explicit read-only mode is supported. Essentially, the
* read-only flag is just a hint for potential optimization.
* @param definition TransactionDefinition instance (can be {@code null} for defaults),
* describing propagation behavior, isolation level, timeout etc.
* @return transaction status object representing the new or current transaction
* @throws TransactionException in case of lookup, creation, or system errors
* @throws IllegalTransactionStateException if the given transaction definition
* cannot be executed (for example, if a currently active transaction is in
* conflict with the specified propagation behavior)
* @see TransactionDefinition#getPropagationBehavior
* @see TransactionDefinition#getIsolationLevel
* @see TransactionDefinition#getTimeout
* @see TransactionDefinition#isReadOnly
*/
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
/**
...
*/
void commit(TransactionStatus status) throws TransactionException;
/**
...
*/
void rollback(TransactionStatus status) throws TransactionException;
}
要使用编程事务管理,我们要做的就是实现TransactionCallback接口,spring 中的很多接口都是遵循ISP原则的,用匿名类来实现这样接口是很方便的,例如spring-jdbc 中的很多接口
Rowmapper,ParameterMapper,PreparedStatementCallback......
public interface TransactionCallback<T> {
/**
...
*/
T doInTransaction(TransactionStatus status);
}
伪代码如下
new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
try{
operation1();
operation2();
operation3();
}catch(Exception e){
status.setRollbackOnly();
}
return null;
}
};
当然要使用spring的事务管理还需要实例化TransactioinTemplate,在application.xml文件中配置,不赘述。
分享到:
相关推荐
ByteTCC Transaction Manager旨在提供一个兼容JTA的基于TCC机制的分布式事务管理器。兼容JTA,可以很好的与EJB、Spring等容器(本文档下文说明中将以Spring容器为例)进行集成。
spring-transaction-managerspring事务管理以及mySQL事务隔离级别
Transaction Manager(TM) :控制全局事务的边界,负责开启一个全局事务,并最终发起全局提交或全局回滚的决议; Resource Manager(RM) :控制分支事务,负责分支注册,状态汇报,并接收事务协调器的指令,驱动分支...
37.6. Supporting an Alternative Embedded Transaction Manager 38. Hazelcast 39. Quartz Scheduler 40. Spring Integration 41. Spring Session 42. Monitoring and Management over JMX 43. Testing 43.1. Test ...
-- Transaction manager for a single JDBC DataSource 16 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 17 18 19 --> 20 21 <!-- 22 23 24 25...
TM (Transaction Manager) - 事务管理器:定义全局事务的范围:开始全局事务、提交或回滚全局事务。 RM (Resource Manager) -资源管理器:管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并...
51. <tx:annotation-driven transaction-manager="transactionManager" /> 52. <tx:annotation-driven mode="aspectj"/> 53. 54. <aop:aspectj-autoproxy/> 55.</beans> hibernate.properties数据库连接...
<tx:annotation-driven transaction-manager="txManager"/> <!-- 采用单数据源事务控制方式,通过注解来定义事务--> class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> ...
<tx:advice id="txAdvicePdm" transaction-manager="transactionManagerPdm"> *" propagation="REQUIRED"/> *" propagation="REQUIRED"/> *" propagation="REQUIRED"/> *" propagation="REQUIRED"/> ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
Transaction Manager(TM) :控制全局事务的边界,负责开启一个全局事务,并最终发起全局提交或全局回滚的决议; Resource Manager(RM) :控制分支事务,负责分支,状态汇报,并接收事务协调器的指令,驱动分支...
Seata的核心概念包括三个组件:事务协调器(Transaction Coordinator)、事务管理器(Transaction Manager)和资源管理器(Resource Manager)。事务协调器负责协调和管理全局事务,事务管理器负责管理分支事务,而...
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 保证POJO中标注@Required的属性被注入 --> <bean class="org.springframework.beans.factory.annotation....
JOTM (Java Open Transaction Manager)是由ObjectWeb协会开发的功能完整的且资源开放的独立的事务管理器。 它提供了 JAVA 应用程序的事务支持,而且与 JTA( JAVA 事务 API)兼容。您可以在JOTM home page 了解到更...
<tx:annotation-driven transaction-manager="txManager" /> <!-- <aop:config> expression="execution(* x.y.service.*Service.*(..))" /> id="noTxServiceOperation" expression="execution(* x.y....
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 保证POJO中标注@Required的属性被注入 --> <bean class="org.springframework.beans.factory.annotation....
<tx:annotation-driven transaction-manager="transactionManager"/> 如果将注解添加在Bean类上,则表示事务的设置对整个Bean类的所有方法都起作用;如果将注解添加在Bean类中的某个方法上,则表示事务的设置只对该...
<tx:advice id="txAdvice" transaction-manager="transactionManager"> *" read-only="true"/> *" propagation="REQUIRED"/> *" propagation="REQUIRED"/> *" propagation="REQUIRED"/> *" ...
<tx:advice id="txAdvice" transaction-manager="myJtaManager"> *" rollback-for="Exception"/> *" rollback-for="Exception"/> *" rollback-for="Exception"/> *" read-only="true" rollback-for=...