搜索

此内容没有您所选择的语言版本。

9.4. Demarcation by Declarative Transactions

download PDF

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.
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.