文档章节

Multiple Transaction Managers with @Transactional

秋风醉了
 秋风醉了
发布于 2014/11/10 11:21
字数 300
阅读 155
收藏 0

http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/transaction.html

Multiple Transaction Managers with @Transactional

Multiple Transaction Managers with @Transactional

Most Spring applications only need a single transaction manager, but there may be situations where you want multiple independent transaction managers in a single application. The value attribute of the @Transactional annotation can be used to optionally specify the identity of the PlatformTransactionManager to be used. This can either be the bean name or the qualifier value of the transaction manager bean. For example, using the qualifier notation, the following Java code

public class TransactionalService {
    @Transactional("order")
    public void setSomething(String name) { ... }
    @Transactional("account")
    public void doSomething() { ... }
}

 

could be combined with the following transaction manager bean declarations in the application context.

<tx:annotation-driven />
<bean id="transactionManager1"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	...
	<qualifier value="order" />
</bean>
<bean id="transactionManager2"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	...
	<qualifier value="account" />
</bean>

In this case, the two methods on TransactionalService will run under separate transaction managers, differentiated by the "order" and "account" qualifiers. The default <tx:annotation-driven> target bean name transactionManager will still be used if no specifically qualified PlatformTransactionManager bean is found.

 

Custom shortcut annotations

If you find you are repeatedly using the same attributes with @Transactional on many different methods, then Spring’s meta-annotation support allows you to define custom shortcut annotations for your specific use cases. For example, defining the following annotations

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("order")
public @interface OrderTx {
}

 

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("account")
public @interface AccountTx {
}

allows us to write the example from the previous section as

public class TransactionalService {
    @OrderTx
    public void setSomething(String name) { ... }
    @AccountTx
    public void doSomething() { ... }
}

Here we have used the syntax to define the transaction manager qualifier, but could also have included propagation behavior, rollback rules, timeouts etc.

====================END====================

© 著作权归作者所有

共有 人打赏支持
秋风醉了
粉丝 241
博文 566
码字总数 417505
作品 0
朝阳
程序员
私信 提问
Spring with multiple transaction managers

Spring with multiple transaction managers Here is repost of http://stackoverflow.com/questions/4423125/spring-is-it-possible-to-use-multiple-transaction-managers-in-the-same-app......

我是小强
2013/12/10
0
0
使用@Transactional(SUPPORTS)和不加@Transactional 有什么区别?

Spring 的事务传播机制中 Propagation.SUPPORTS 级别的意义是,如果当前环境有事务,就加入到当前事务;如果没有事务,就以非事务的方式执行。从这个说明来看,使用这个级别和不加@Transact...

苗新国
2016/06/27
205
0
Spring+Mybatis 实现多数据源和多事物配置

没有相应的RPC接口,一个项目存在多个数据源,不想在代码端自己编写代码切换数据源? 你有可能需要下面的解决方案: 多数据源配置 多事物配置 配置事物拦截器 注解方式 在spring-mybatis配置文...

jeremyli90
2017/11/09
0
0
Storm Transaction 原理+实战

Storm guarantees data processing by providing an at least once processing guarantee. The most common question asked about Storm is "Given that tuples can be replayed, how do you......

泡海椒
2016/04/02
39
0
Java for Web学习笔记(一零四):Spring框架中使用JPA(4)Service中使用事务

Spring和JTA的@Transactional标记 总体而言,Spring的@Transactional比JTA的@Transactional要强大和更灵活。 @javax.transaction.Transactional,可以配置参数: dontRollbackOn:不触发回滚...

flowingflying
2017/12/30
0
0

没有更多内容

加载失败,请刷新页面

加载更多

查询改写参数配置

参数配置有哪些选项:

Java搬砖工程师
8分钟前
1
0
elasticsearch知识点

elasticsearch基础知识: 基础知识: 1 集群: 主节点不涉及文档级别的变更或搜索 只负责索引的变更 删除 和 集群节点的加入和删除 技术上来说,一个主分片最大能够存储 Integer.MAX_VALUE - 12...

geek土拨鼠
10分钟前
1
0
ECS(Entity-Component-System)

ECS是一种思想 TODO

梦想游戏人
11分钟前
1
0
Navicat使用教程:使用Navicat Premium 12自动执行数据库复制(一)

下载Navicat Premium最新版本 Navicat Premium是一个可连接多种数据库的管理工具,它可以让你以单一程序同时连接到MySQL、Oracle及PostgreSQL数据库,让管理不同类型的数据库更加的方便。 与...

电池盒
13分钟前
3
0
docker 新手向 - 防止容器自动退出的解决方案

本文主要简单介绍 docker 容器与前置进程的关系,以及如何编写 Dockerfile/docker-compose.yml 优雅的让容器可以常驻运行。 docker 容器的生命周期是同容器中的前置进程相关在一起的,这也是...

big_cat
16分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部