Skip to content

Commit 1ddbfe0

Browse files
committed
SPR-6205: Added doc on annotation support for multiple transaction managers
1 parent 5d77255 commit 1ddbfe0

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

spring-framework-reference/src/transaction.xml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,17 @@ public class DefaultFooService implements FooService {
16481648
</thead>
16491649

16501650
<tbody>
1651+
<row>
1652+
<entry><literal><link
1653+
linkend="tx-multiple-tx-mgrs-with-attransactional">value</link></literal></entry>
1654+
1655+
<entry>String</entry>
1656+
1657+
<entry>
1658+
Optional qualifier specifying the transaction manager to be used.
1659+
</entry>
1660+
</row>
1661+
16511662
<row>
16521663
<entry><literal><link
16531664
linkend="tx-propagation">propagation</link></literal></entry>
@@ -1741,6 +1752,81 @@ public class DefaultFooService implements FooService {
17411752
the name of the transaction would be:
17421753
<literal>com.foo.BusinessService.handlePayment</literal>.</para>
17431754
</section>
1755+
1756+
<section id="tx-multiple-tx-mgrs-with-attransactional">
1757+
<title>Multiple Transaction Managers with <interfacename>@Transactional</interfacename></title>
1758+
<para>
1759+
Most Spring applications only need a single transaction manager, but there may be situations
1760+
where you want multiple independent transaction managers in a single application.
1761+
The value attribute of the <interfacename>@Transactional</interfacename> annotation can
1762+
be used to optionally specify the identity of the <classname>PlatformTransactionManager</classname>
1763+
to be used. This can either be the bean name or the qualifier value of the transaction manager bean.
1764+
For example, using the qualifier notation, the following Java code
1765+
<programlisting language="java">
1766+
public class TransactionalService {
1767+
1768+
@Transactional("order")
1769+
public void setSomething(String name) { ... }
1770+
1771+
@Transactional("account")
1772+
public void doSomething() { ... }
1773+
}
1774+
</programlisting>
1775+
could be combined with the following transaction manager bean declarations in the application context.
1776+
<programlisting language="xml"><![CDATA[
1777+
<tx:annotation-driven/>
1778+
1779+
<bean id="transactionManager1" class="org.springframework.jdbc.DataSourceTransactionManager">
1780+
...
1781+
<qualifier value="order"/>
1782+
</bean>
1783+
1784+
<bean id="transactionManager2" class="org.springframework.jdbc.DataSourceTransactionManager">
1785+
...
1786+
<qualifier value="account"/>
1787+
</bean>
1788+
]]>
1789+
</programlisting>
1790+
In this case, the two methods on <literal>TransactionalService</literal> will run under separate
1791+
transaction managers, differentiated by the "order" and "account" qualifiers.
1792+
The default <literal>&lt;tx:annotation-driven&gt;</literal> target bean name <literal>transactionManager</literal> will
1793+
still be used if no specifically qualified PlatformTransactionManager bean is found.
1794+
</para>
1795+
</section>
1796+
<section id="tx-custom-attributes">
1797+
<title>Custom shortcut annotations</title>
1798+
<para>
1799+
If you find you are repeatedly using the same attributes with <interfacename>@Transactional</interfacename>
1800+
on many different methods, then Spring's meta-annotation support allows you to define custom shortcut
1801+
annotations for your specific use cases. For example, defining the following annotations
1802+
<programlisting language="java">
1803+
@Target({ElementType.METHOD, ElementType.TYPE})
1804+
@Retention(RetentionPolicy.RUNTIME)
1805+
@Transactional("order")
1806+
public @interface OrderTx {
1807+
}
1808+
1809+
@Target({ElementType.METHOD, ElementType.TYPE})
1810+
@Retention(RetentionPolicy.RUNTIME)
1811+
@Transactional("account")
1812+
public @interface AccountTx {
1813+
}
1814+
</programlisting>
1815+
allows us to write the example from the previous section as
1816+
<programlisting language="java">
1817+
public class TransactionalService {
1818+
1819+
@OrderTx
1820+
public void setSomething(String name) { ... }
1821+
1822+
@AccountTx
1823+
public void doSomething() { ... }
1824+
}
1825+
</programlisting>
1826+
Here we have used the syntax to define the transaction manager qualifier, but could also have
1827+
included propagation behavior, rollback rules, timeouts etc.
1828+
</para>
1829+
</section>
17441830
</section>
17451831

17461832
<section id="tx-propagation">

0 commit comments

Comments
 (0)