`
Clayz
  • 浏览: 297804 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

Bean-Managed Transactions

    博客分类:
  • Java
阅读更多

To control transaction boundaries yourself, you must use the JTA interface javax.transaction.UserTransaction. The javax.transaction.UserTransaction interface enables you to programmatically control transactions. Here is what the javax.transaction.UserTransaction interface looks like:

public interface javax.transaction.UserTransaction
    public void begin();
    public void commit();
    public int getStatus();
    public void rollback();
    public void setRollbackOnly();
    public void setTransactionTimeout(int);
}

 

begin() : Begins a new transaction. This transaction becomes associated with the current thread.

commit() : Runs the two-phase commit protocol on an existing transaction associated with the current thread. Each resource manager will make its updates durable.

getStatus() : Retrieves the status of the transaction associated with this thread.

Rollback() : Forces a rollback of the transaction associated with the current thread.

setRollbackOnly() : Calls this to force the current transaction toroll back. This will eventually force the transaction to abort.

setTransactionTimeout(int) : The transaction timeout is the maximum amount of time that a transaction can run before it’s aborted. This is useful for avoiding deadlock situations, when precious resources are being held by a transaction that is currently running.

 

JTA also defines a number of constants that indicate the current status of a transaction. You will be returned one of these constants when you call the UserTransaction.getStatus() method:

public interface javax.transaction.Status {
    public static final int STATUS_ACTIVE;
    public static final int STATUS_NO_TRANSACTION;
    public static final int STATUS_MARKED_ROLLBACK;
    public static final int STATUS_PREPARING;
    public static final int STATUS_PREPARED;
    public static final int STATUS_COMMITTING;
    public static final int STATUS_COMMITTED;
    public static final int STATUS_ROLLING_BACK;
    public static final int STATUS_ROLLEDBACK;
    public static final int STATUS_UNKNOWN;
}

STATUS_ACTIVE : A transaction is currently active.

STATUS_NO_TRANSACTION : No transaction is active.

STATUS_MARKED_ROLLBACK : The current transaction will eventually abort because it’s been marked for rollback. This could be because some party called UserTransaction.setRollbackOnly().

STATUS_PREPARING : The current transaction is preparing to be committed (during Phase One of the two-phase commit protocol).

STATUS_PREPARED : The current transaction has been prepared to be committed (Phase One is complete).

STATUS_COMMITTING : The current transaction is in the process of being committed right now (during Phase Two).

STATUS_COMMITTED : The current transaction has been committed (Phase Two is complete).

STATUS_ROLLING_BACK : The current transaction is in the process of rolling back.

STATUS_ROLLEDBACK : The current transaction has been rolled back.

STATUS_UNKNOWN :  The status of the current transaction cannot be determined.

 

We now show you how to write an enterprise bean that uses bean-managed transactions. To do this, we’ll use teller bean. The transferFunds() method of the teller bean needs to use transactions to make sure that the updates to the bank account entities are done as part of the same transaction. Let us see how we do this by using javax.transaction.UserTransaction methods.

import javax.ejb.*;
import javax.annotation.Resource;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.transaction.UserTransaction;

@Stateless()
@TransactionManagement(javax.ejb.TransactionManagementType.BEAN)
public class TellerBean implements Teller {
    @PersistenceContext private EntityManager em;
    @Resource private javax.transaction.UserTransaction userTx;
    public void transferFunds(float amount, String fromAccount, String toAccount) {
        // Lookup for accts with the provided account Ids
        try {
             userTx.begin();
             BankAccount acct1 = em.find(BankAccount.class, fromAccount);
             BankAccount acct2 = em.find(BankAccount.class, toAccount);
             if (acct1.balance < amount) userTx.rollback();
             acct1.withdraw(amount);
             acct2.deposit(amount);
             em.persist(acct1);
             em.persist(acct2);
             userTx.commit();
        } catch (Exception e) {
             System.out.println(“Exception occurred during transfer of funds.” + e.getMessage());
        }
    }
}
分享到:
评论

相关推荐

    java面视大全(常见的所有ejb题型)

    - EJB的事务管理可以是Container-managed Transactions(CMT)或Bean-managed Transactions(BMT): - CMT由容器自动管理事务边界,开发者无需显式控制事务开始和结束。 - BMT则要求开发者自己编写事务管理代码...

    itfuture_sessionbean

    7. 容器管理事务(Container-Managed Transactions,CMT)和bean管理事务(Bean-Managed Transactions,BMT):理解这两种事务管理方式的差异和使用场景。 8. Session Bean的性能优化:如最小化内存占用、减少远程...

    EJB实体Bean与事物管理

    - **事务类型**: EJB支持两种事务管理模型:自动事务(Container-Managed Transactions,CMT)和声明式事务(Bean-Managed Transactions,BMT)。 - **CMT**: 容器根据Bean的方法声明自动开始、提交或回滚事务。...

    ejb2.0.rar_EJB2.0特性_ejb2.0

    8. **Performance Enhancements**:为了提高性能,EJB2.0对容器进行了优化,例如引入了bean-managed transactions和bean-managed persistence,让开发者有更多的控制权以优化性能。 9. **Web Services Support**:...

    一个EJB的小测试代码

    7. **事务管理**:EJB支持容器管理的事务(Container-Managed Transactions, CMT)和bean管理的事务(Bean-Managed Transactions, BMT)。根据业务需求,"CurrencyConverter"可能会配置为在特定方法上自动开启和提交...

    EntepriseJavaBean设计实务

    EJB支持多种事务管理级别,如自动事务(container-managed transactions)、编程事务(bean-managed transactions)和声明式事务。这使得开发者能够在处理复杂事务逻辑时保持代码的简洁性。 8. **安全性**: EJB...

    ejb3.0 分布式事务

    EJB3.0中,事务管理分为容器管理的事务(CMT, Container-Managed Transactions)和 bean 管理的事务(BMT, Bean-Managed Transactions)。CMT是默认模式,由容器自动管理事务的开始、提交、回滚,开发者只需关注...

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    Bean-Managed Transactions 524 Transaction Timeouts 525 Updating Multiple Databases 526 Transactions in Web Components 528 Further Information about Transactions 528 Chapter 28: Resource ...

    JPA教程

    2. **bean管理事务(Bean-managed Transactions, BMT)**: 开发者在代码中手动控制事务边界。 ### JPA的多对一、一对多、多对多关系映射 1. **多对一(ManyToOne)**: 一个实体可以关联多个其他实体,通常使用`@...

    Java_EE_6.0_API_DOC

    6. **Bean-Managed Transactions(BMT)**:允许开发者直接控制事务边界,增强了对事务管理的灵活性。 7. **统一的异步编程模型**:Java EE 6.0引入了Asynchronous Servlet API,使得Web应用程序可以处理非阻塞I/O...

    Apache Geronimo 2.1_ Quick Reference.pdf

    Bean-managed transactions 144 Support in Geronimo 145 Setting transaction timeout 145 Transaction isolation levels 145 Transactions in web applications 146 Summary 148 Chapter 6: Security 149 ...

    Advanced Programming for the Java 2 Platform.chm

    Data and Transaction Management &lt;br&gt;Bean-Managed Persistence and the JDBC Platform Managing Transactions Bean-Managed finder Methods Chapter 4: Distributed Computing &lt;br&gt;Lookup ...

    基于Java的实例源码-各种EJB之间的调用示例.zip

    - 应用程序管理的事务(Application-Managed Transactions, AMT):由bean自己管理事务,需要开发者编写事务控制代码。 5. EJB安全性: - 角色基的访问控制(Role-Based Access Control, RBAC):EJB可以通过定义...

    TransactionRewards

    5. **Bean-Managed Transactions (BMT)**: 相比CMT,开发者在BMT模式下需要手动管理事务的生命周期。这包括调用`UserTransaction`接口的`begin()`, `commit()`, 和 `rollback()`方法。 6. **Spring框架中的事务管理...

    J2EE程序设计复习题.doc

    - Session Bean和Message-Driven Bean的事务处理通常采用CMT(Container-Managed Transactions),而Entity Bean可以采用BMT或CMP。 7. 事务处理属性: - EJB的事务属性包括Supports、Should、NotSupported、...

    java源码:Java中的EJB编程实例代码.rar

    - **容器管理的事务(Container-Managed Transactions, CMT)**:容器自动处理事务边界,开发者无需编写事务控制代码。 - **安全性和角色绑定(Security and Role-Based Access Control, RBAC)**:EJB支持基于角色...

    基于Java的源码-EJB 模拟银行ATM流程及操作源代码.zip

    Java EE提供了一种称为容器管理的事务(Container-Managed Transactions,CMT),它允许开发者声明哪些方法需要在事务中执行,由容器自动处理事务的开始、提交或回滚。这对于处理银行业务这样需要确保数据一致性的...

    EJB面试题汇总

    - **BMT(Bean Managed Transactions)**:Bean管理事务,开发者显式地控制事务的开启与提交。 **5.3 申明式事务处理属性** - **Supports**:如果当前没有事务,则以非事务的方式执行。 - **NotSupported**:以非...

    EJB3-黎活明ppt

    容器管理的事务(Container-Managed Transactions, CMT):** EJB容器负责事务的开始、提交、回滚和传播,开发者无需手动处理事务边界,降低了事务管理的复杂性。 **6. 容器提供的服务:** EJB容器为Bean提供了...

    EJB 3 in Action

    EJB3还引入了容器管理的事务(Container-Managed Transactions, CMT),使得开发者无需手动处理事务管理,只需定义事务边界,即可确保数据的一致性。同时,安全性也是EJB3的重要组成部分,书中会讲解如何利用角色和...

Global site tag (gtag.js) - Google Analytics