`
huangcongweng
  • 浏览: 3425 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

spring transaction manager

阅读更多
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机制的分布式事务管理器

    ByteTCC Transaction Manager旨在提供一个兼容JTA的基于TCC机制的分布式事务管理器。兼容JTA,可以很好的与EJB、Spring等容器(本文档下文说明中将以Spring容器为例)进行集成。

    spring-transaction-manager:Spring事务管理实现原理及MySQL InnoBD引擎行锁概述---配套代码

    spring-transaction-managerspring事务管理以及mySQL事务隔离级别

    SpringCloud Alibaba使用(四) - seata分布式事务.zip

    Transaction Manager(TM) :控制全局事务的边界,负责开启一个全局事务,并最终发起全局提交或全局回滚的决议; Resource Manager(RM) :控制分支事务,负责分支注册,状态汇报,并接收事务协调器的指令,驱动分支...

    spring-boot-reference.pdf

    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 ...

    Spring MVC 入门实例

    -- Transaction manager for a single JDBC DataSource 16 &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; 17 18 19 --&gt; 20 21 &lt;!-- 22 23 24 25...

    分布式事务、spring cloud \微服务

    TM (Transaction Manager) - 事务管理器:定义全局事务的范围:开始全局事务、提交或回滚全局事务。 RM (Resource Manager) -资源管理器:管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并...

    spring_MVC源码

    51. &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; 52. &lt;tx:annotation-driven mode="aspectj"/&gt; 53. 54. &lt;aop:aspectj-autoproxy/&gt; 55.&lt;/beans&gt; hibernate.properties数据库连接...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;tx:annotation-driven transaction-manager="txManager"/&gt; &lt;!-- 采用单数据源事务控制方式,通过注解来定义事务--&gt; class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; ...

    spring applicationContext 配置文件

    &lt;tx:advice id="txAdvicePdm" transaction-manager="transactionManagerPdm"&gt; *" propagation="REQUIRED"/&gt; *" propagation="REQUIRED"/&gt; *" propagation="REQUIRED"/&gt; *" propagation="REQUIRED"/&gt; ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    修改Seata是一款开源的分布式事务解决方案

    Transaction Manager(TM) :控制全局事务的边界,负责开启一个全局事务,并最终发起全局提交或全局回滚的决议; Resource Manager(RM) :控制分支事务,负责分支,状态汇报,并接收事务协调器的指令,驱动分支...

    分布式事务seata安装

    Seata的核心概念包括三个组件:事务协调器(Transaction Coordinator)、事务管理器(Transaction Manager)和资源管理器(Resource Manager)。事务协调器负责协调和管理全局事务,事务管理器负责管理分支事务,而...

    SSH整合&&SSH集成开发

    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;!-- 保证POJO中标注@Required的属性被注入 --&gt; &lt;bean class="org.springframework.beans.factory.annotation....

    spring+jotm 多数据源事务管理(一)jdbc

    JOTM (Java Open Transaction Manager)是由ObjectWeb协会开发的功能完整的且资源开放的独立的事务管理器。 它提供了 JAVA 应用程序的事务支持,而且与 JTA( JAVA 事务 API)兼容。您可以在JOTM home page 了解到更...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- &lt;aop:config&gt; expression="execution(* x.y.service.*Service.*(..))" /&gt; id="noTxServiceOperation" expression="execution(* x.y....

    spring3.2+strut2+hibernate4

    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;!-- 保证POJO中标注@Required的属性被注入 --&gt; &lt;bean class="org.springframework.beans.factory.annotation....

    基于java的企业级应用开发:声明式事务管理.ppt

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; 如果将注解添加在Bean类上,则表示事务的设置对整个Bean类的所有方法都起作用;如果将注解添加在Bean类中的某个方法上,则表示事务的设置只对该...

    springmvc-ibatis

    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt; *" read-only="true"/&gt; *" propagation="REQUIRED"/&gt; *" propagation="REQUIRED"/&gt; *" propagation="REQUIRED"/&gt; *" ...

    JTA事务源码示例

    &lt;tx:advice id="txAdvice" transaction-manager="myJtaManager"&gt; *" rollback-for="Exception"/&gt; *" rollback-for="Exception"/&gt; *" rollback-for="Exception"/&gt; *" read-only="true" rollback-for=...

Global site tag (gtag.js) - Google Analytics