Weigh Apache Geronimo EJB transaction options, part 2nd: Bean Management Transactions

Source: Internet
Author: User

Now that you have learned all the details about the transactions managed by the Enterprise JavaBeans (EJB) container in part 1th, you may be curious about the transactions managed by the EJB bean and how you can implement it in the Apache Geronimo application server. In this installment of the three-part series (part 2nd), you can get all the details.

Brief introduction

This series, which is divided into three parts, explores how Geronimo and OPENEJB can help you, as well as the EJB transaction concepts that can now be implemented in EJB 2.1 (allowing you to successfully enter EJB 3.0). Part 2nd (that is, this article) describes the transaction of EJB Bean Management in detail, examining the use of two bean-managed transactions (Java Transaction API (JTA) and Java Database Connectivity (JDBC) transactions that can be generated 。 You will learn about the rollback of these transactions and how to use the bean-managed transactions in Geronimo.

Now briefly review what is covered in part 1th: Using EJB transactions, you can choose from the following two options:

You can delegate the transaction implementation to the EJB container. This is the container-managed transaction and the focus of the 1th part of this series. See part 1th for an introduction to transactions and container-managed transactions.

Allows the enterprise bean to be managed programmatically when the transaction is committed and rolled back. The application developer must explicitly incorporate transaction logic into the enterprise bean code to implement it. This is the bean-managed transaction that is the focus of the 2nd part of this series (that is, this article).

In part 3rd, you will learn about the challenges and additional features associated with container-managed transactions and bean-managed transactions.

Bean-Managed transactions

Container-managed transactions are simpler and less code compared to the transaction that is managed by the Bean. However, when you use a container-managed transaction, your enterprise bean method can either participate in the transaction or be not involved. If you need more rough logic and use rough logic to commit transactions or rollback transactions based on specific validation logic, you should use the transaction that the Bean manages. Bean-managed transactions allow you to have full control over transaction boundaries.

A session bean or message-driven bean (MDB) can use a transaction that is managed by the Bean. Entity beans cannot use transactions that are managed by the bean. This is because the EJB container controls the time that the data for the entity bean is loaded or stored.

In a bean-managed transaction, the container must allow the bean instance to perform several transactions consecutively in one method, but keep in mind that nested transactions cannot be performed. If you attempt to start a new transaction and the bean instance does not commit the previous transaction, the container throws an exception.

You can use transactions managed by two types of beans:

JTA Affairs

JDBC Transaction

We'll look at both of these transactions in the next section.

JTA Affairs

JTA is an interface specification between the transaction manager and other components involved in the Distributed transaction processing system. By using interfaces, you can separate transactions separately without using the unique APIs of the transaction manager.

All EJB containers must support the JTA API. For a developer, this allows you to communicate the transaction directives to the EJB container and provide the transaction directives in a common, simple way. This approach allows you to worry about the underlying work of the transaction service.

Javax.transaction.UserTransaction

When using bean-managed JTA transactions, you can use the Javax.transaction.UserTransaction interface to partition transactions. On this interface, there are three interesting ways to do this:

Begin ()-Creates a new transaction and associates the transaction with the current thread.

Commit ()--commits a transaction that is associated with the current thread.

Rollback ()--force rollback of transactions associated with the current thread.

All updates that occur between Begin () and commit () are done in one transaction.

Other methods on the UserTransaction interface include:

GetStatus ()--retrieves the status of the transaction related to the current thread. The return value is the constant defined on the Javax.transaction.Status interface.

Setrollbackonly ()--force the transaction to terminate.

Settransactiontimeout (int)--Sets the maximum number of times a transaction can run before it is aborted. This is useful when avoiding deadlocks.

See the Resources section of this article for links to UserTransaction and UserStatus interfaces in Sun's Javadocs.

How do you use the Bean method to get UserTransaction's original reference? Based on the type of the enterprise bean, you can obtain an interface from the bean context:

For session beans, you can call Getusertransaction () from Javax.ejb.EJBContext.

For an MDB, you can call Getusertransaction () from Messagedrivencontext.getusertransaction ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.