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());
}
}
}
分享到:
相关推荐
- EJB的事务管理可以是Container-managed Transactions(CMT)或Bean-managed Transactions(BMT): - CMT由容器自动管理事务边界,开发者无需显式控制事务开始和结束。 - BMT则要求开发者自己编写事务管理代码...
7. 容器管理事务(Container-Managed Transactions,CMT)和bean管理事务(Bean-Managed Transactions,BMT):理解这两种事务管理方式的差异和使用场景。 8. Session Bean的性能优化:如最小化内存占用、减少远程...
- **事务类型**: EJB支持两种事务管理模型:自动事务(Container-Managed Transactions,CMT)和声明式事务(Bean-Managed Transactions,BMT)。 - **CMT**: 容器根据Bean的方法声明自动开始、提交或回滚事务。...
8. **Performance Enhancements**:为了提高性能,EJB2.0对容器进行了优化,例如引入了bean-managed transactions和bean-managed persistence,让开发者有更多的控制权以优化性能。 9. **Web Services Support**:...
7. **事务管理**:EJB支持容器管理的事务(Container-Managed Transactions, CMT)和bean管理的事务(Bean-Managed Transactions, BMT)。根据业务需求,"CurrencyConverter"可能会配置为在特定方法上自动开启和提交...
EJB支持多种事务管理级别,如自动事务(container-managed transactions)、编程事务(bean-managed transactions)和声明式事务。这使得开发者能够在处理复杂事务逻辑时保持代码的简洁性。 8. **安全性**: EJB...
EJB3.0中,事务管理分为容器管理的事务(CMT, Container-Managed Transactions)和 bean 管理的事务(BMT, Bean-Managed Transactions)。CMT是默认模式,由容器自动管理事务的开始、提交、回滚,开发者只需关注...
Bean-Managed Transactions 524 Transaction Timeouts 525 Updating Multiple Databases 526 Transactions in Web Components 528 Further Information about Transactions 528 Chapter 28: Resource ...
2. **bean管理事务(Bean-managed Transactions, BMT)**: 开发者在代码中手动控制事务边界。 ### JPA的多对一、一对多、多对多关系映射 1. **多对一(ManyToOne)**: 一个实体可以关联多个其他实体,通常使用`@...
6. **Bean-Managed Transactions(BMT)**:允许开发者直接控制事务边界,增强了对事务管理的灵活性。 7. **统一的异步编程模型**:Java EE 6.0引入了Asynchronous Servlet API,使得Web应用程序可以处理非阻塞I/O...
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 ...
Data and Transaction Management <br>Bean-Managed Persistence and the JDBC Platform Managing Transactions Bean-Managed finder Methods Chapter 4: Distributed Computing <br>Lookup ...
- 应用程序管理的事务(Application-Managed Transactions, AMT):由bean自己管理事务,需要开发者编写事务控制代码。 5. EJB安全性: - 角色基的访问控制(Role-Based Access Control, RBAC):EJB可以通过定义...
5. **Bean-Managed Transactions (BMT)**: 相比CMT,开发者在BMT模式下需要手动管理事务的生命周期。这包括调用`UserTransaction`接口的`begin()`, `commit()`, 和 `rollback()`方法。 6. **Spring框架中的事务管理...
- Session Bean和Message-Driven Bean的事务处理通常采用CMT(Container-Managed Transactions),而Entity Bean可以采用BMT或CMP。 7. 事务处理属性: - EJB的事务属性包括Supports、Should、NotSupported、...
- **容器管理的事务(Container-Managed Transactions, CMT)**:容器自动处理事务边界,开发者无需编写事务控制代码。 - **安全性和角色绑定(Security and Role-Based Access Control, RBAC)**:EJB支持基于角色...
Java EE提供了一种称为容器管理的事务(Container-Managed Transactions,CMT),它允许开发者声明哪些方法需要在事务中执行,由容器自动处理事务的开始、提交或回滚。这对于处理银行业务这样需要确保数据一致性的...
- **BMT(Bean Managed Transactions)**:Bean管理事务,开发者显式地控制事务的开启与提交。 **5.3 申明式事务处理属性** - **Supports**:如果当前没有事务,则以非事务的方式执行。 - **NotSupported**:以非...
容器管理的事务(Container-Managed Transactions, CMT):** EJB容器负责事务的开始、提交、回滚和传播,开发者无需手动处理事务边界,降低了事务管理的复杂性。 **6. 容器提供的服务:** EJB容器为Bean提供了...
EJB3还引入了容器管理的事务(Container-Managed Transactions, CMT),使得开发者无需手动处理事务管理,只需定义事务边界,即可确保数据的一致性。同时,安全性也是EJB3的重要组成部分,书中会讲解如何利用角色和...