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());
}
}
}
分享到:
相关推荐
资源来自pypi官网。 资源全名:openmetadata-airflow-managed-apis-0.1.0.tar.gz
Ansible-ansible-managed-node.dockerfile.zip,使用运行openssh守护进程和python for ansible构建docker映像。,ansible是一个简单而强大的自动化引擎。它用于帮助配置管理、应用程序部署和任务自动化。
Connector/Net is a fully-managed ADO.NET driver for MySQL. MySql 的 ADO.NET 驱动 版本 6.3.5.0 适用于 .net 4.0
Color-Managed-Workflow
Sams - Managed DirectX 9 Kick Start
资源来自pypi官网。 资源全名:google-cloud-managed-identities-0.1.0.tar.gz
Bean-Managed Transactions 524 Transaction Timeouts 525 Updating Multiple Databases 526 Transactions in Web Components 528 Further Information about Transactions 528 Chapter 28: Resource ...
inside dispersion-managed fiber links. Using a Gaussian form for chirped optical pulses in combination with the variational analysis, we obtain an analytic expression for the timing jitter whose ...
Oracle9i User-Managed Backup and Recovery Guide Release 2 (9.2)
Kali Linux VMware(kali-linux-2022.2-vmware-i386.7z)文件分割成 3个 压缩包,必须集齐3个 文件后才能一起解压一起使用: Kali Linux VMware(kali-linux-2022.2-vmware-i386.part1.rar) ... ...
emWin 设计用于提供高效且独立于处理器和显示控制器的图形用户界面,用于任何使用图形显示进行 操作的应用。它与单任务和多任务环境、专用操作系统或具有任何商业RTOS 兼容,emWin 的发货 形式为C 语言源代码。...
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 ...
managed-cluster-capz-helm.gitcd azure-managed-cluster-capz-helmsource clusterctl.env 使用Helm部署集群helm install capz1 charts/azure-managed-cluster/ \--set subscriptionID=12c7e9d6-967e-40c8-8b3e-4659...
JSF 2.0: Managed Beans – Classes to Represent Form Info
dynamics 审批工作流
Data and Transaction Management <br>Bean-Managed Persistence and the JDBC Platform Managing Transactions Bean-Managed finder Methods Chapter 4: Distributed Computing <br>Lookup ...
Visual C++中非管理代码对管理代码的叫用方法实例
< artifactId>google-cloud-managed-identities < version>0.0.0 如果您使用的是不带BOM的Gradle,请将其添加到您的依赖项中 compile ' com.google.cloud:google-cloud-managed-identities:0.0.0 '
9 Seam-managed transactions and persistence 10 Rapid Seam development Part 4 – Sinking the business requirements 11 Securing Seam applications 12 Ajax and JavaScript remoting 13 File uploads, rich ...
WordPress与托管MySQL--Helm-Chart 这是用于WordPress的简单Helm Chart ,它连接...managed-mysql 显示值helm show values wordpress-managed-mysql 安装# Validate the yaml with dry-runhelm install < Release> \ -