Red Hat Training

A Red Hat training course is available for Red Hat Fuse

9.4. Demarcation by Declarative Transactions

Overview

You can also demarcate transactions by declaring transaction policies in your blueprint XML file. By applying the appropriate transaction policy to a bean or bean method (for example, the Required policy), you can ensure that a transaction is started whenever that particular bean or bean method is invoked. At the end of the bean method, the transaction will be committed. (This approach is analogous to the way that transactions are dealt with in Enterprise Java Beans).
OSGi declarative transactions enable you to define transaction policies at the following scopes in your blueprint file:

Bean-level declaration

To declare transaction policies at the bean level, insert a tx:transaction element as a child of the bean element, as follows:
<blueprint  xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
            xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.1.0">

    <bean id="accountFoo" class="org.fusesource.example.Account">
        <tx:transaction method="*" value="Required"/>
        <property name="accountName" value="Foo"/>
    </bean>

    <bean id="accountBar" class="org.fusesource.example.Account">
        <tx:transaction method="*" value="Required"/>
        <property name="accountName" value="Bar"/>
    </bean>

</blueprint>
In the preceding example, the Required transaction policy is applied to all methods of the accountFoo bean and the accountBar bean (where the method attribute specifies the wildcard, *, to match all bean methods).

Top-level declaration

To declare transaction policies at the top level, insert a tx:transaction element as a child of the blueprint element, as follows:
<blueprint  xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
            xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.1.0">

    <tx:transaction bean="account*" value="Required"/>

    <bean id="accountFoo" class="org.fusesource.example.Account">
        <property name="accountName" value="Foo"/>
    </bean>

    <bean id="accountBar" class="org.fusesource.example.Account">
        <property name="accountName" value="Bar"/>
    </bean>

</blueprint>
In the preceding example, the Required transaction policy is applied to all methods of every bean whose ID matches the pattern, account*.

tx:transaction attributes

The tx:transaction element supports the following attributes:
bean
(Top-level only) Specifies a list of bean IDs (comma or space separated) to which the transaction policy applies. For example:
<blueprint ...>
    <tx:transaction bean="accountFoo,accountBar" value="..."/>
</blueprint>
You can also use the wildcard character, *, which may appear at most once in each list entry. For example:
<blueprint ...>
    <tx:transaction bean="account*,jms*" value="..."/>
</blueprint>
If the bean attribute is omitted, it defaults to * (matching all non-synthetic beans in the blueprint file).
method
(Top-level and bean-level) Specifies a list of method names (comma or space separated) to which the transaction policy applies. For example:
<bean id="accountFoo" class="org.fusesource.example.Account">
    <tx:transaction method="debit,credit,transfer" value="Required"/>
    <property name="accountName" value="Foo"/>
</bean>
You can also use the wildcard character, *, which may appear at most once in each list entry.
If the method attribute is omitted, it defaults to * (matching all methods in the applicable beans).
value
(Top-level and bean-level) Specifies the transaction policy. The policy values have the same semantics as the policies defined in the EJB 3.0 specification, as follows:
  • Required—support a current transaction; create a new one if none exists.
  • Mandatory—support a current transaction; throw an exception if no current transaction exists.
  • RequiresNew—create a new transaction, suspending the current transaction if one exists.
  • Supports—support a current transaction; execute non-transactionally if none exists.
  • NotSupported—do not support a current transaction; rather always execute non-transactionally.
  • Never—do not support a current transaction; throw an exception if a current transaction exists.