Several Basic Concepts about J2EE tranaction

Source: Internet
Author: User
Transaction is a very important component in the J2EE and. Net fields. Although many concepts related to transaction are the same on two different platforms, they have many differences in the implementation of transaction. For more information about transaction under. net, see transaction in ado.net 2.0 by idior. In the following sections, I will introduce several concepts related to transaction in J2EE.
1. What is transaction? Transaction refers to a series of operations that can be performed to change the database. In this explanation, there are three keywords: A series, Not Allowed SplitAnd Change. There is no transaction to modify the database. Only a "series" of operations (a set of SQL statements) can constitute a transaction. "Not separated" means consistency and integrity, either all the operations commit or all rollback. If a series of operations only include enquiry operations, these operations are not transactions.
2. in J2EE, there are several main types of transaction? In J2EE, the main types of transaction are bean-managed transaction and container-managed transaction. The bean-managed transaction can be divided into JDBC transaction and JTA transaction.
3. What is JDBC transaction? What features does it have? JDBC transaction refers to the transaction managed by the database itself. The most important feature is that the transaction commit and rollback are completed by displaying the commit and rollback methods that call the connection interface. The transaction end boundary is the call of the commit or rollback method, and the START boundary is not so obvious. It starts when the first statement in all statement of the current transaction is executed. The Code is as follows:

Class creditdaoimpl implements creditdao {
Connection conn = getconnection ();
Public void transfer (currency amount, account fromaccount, account toaccount) throws creditexception {
Try {
Conn. setautocommit (false );
Deposittoaccount (Conn, toaccount, amount );
Withdrawfromaccount (Conn, fromaccount, amount );
Conn. Commit ();
} Catch (exception e ){
Try {
Conn. rollback ();
Throw new creditexception (E. getmessage ());
} Catch (sqlexception E1 ){
Throw new creditexception (E. getmessage ());
}
}
}
}

4. What is JTA transaction? What features does it have? JTA transaction is a transaction managed by J2EE transaction manager. The biggest feature is to call the begin, commit, and rollback methods of the usertransaction interface to define the transaction scope and commit and roll back the transaction. JTA transaction can implement the same transaction corresponding to different databases, but it still cannot implement the nesting of transactions. The specific code is as follows [1]: For more information about transaction attribute, see container-managed transaction in J2EE tutorial.

[1] The code is from Sun's J2EE tutorial about bean-managed transaction

Public void withdrawcash (double amount ){
Usertransaction ut = context. getusertransaction ();
Try {
Ut. Begin ();
Updatechecking (amount );
Machinebalance-= amount;
Insertmachine (machinebalance );
Ut. Commit ();
} Catch (exception ex ){
Try {
Ut. rollback ();
} Catch (systemexception syex ){
Throw new ejbexception
("Rollback failed:" + syex. getmessage ());
}
Throw new ejbexception
("Transaction failed:" + ex. getmessage ());
}
}

5. What is container-managed transaction? What features does it have? Container-managed transaction, as its name implies, is the transaction managed by the container. Of course, transaction appears in the EJB category. The biggest feature of container-managed transaction is that you do not need to explicitly define the transaction boundary or explicitly commit or roll back the transaction. All this is done by the container for us. What we need to do is to set which methods are related to transactions in a bean and set their transaction attribute.
The scope of transaction is very important, especially when calling another bean method in a bean method. To facilitate Problem description, we call the two methods methoda and methodb respectively. When methoda calls methodb, what are the restrictions on the caller's methoda at the transaction level (whether a transaction exists in methodb) and how methoda can call methodb at the transaction level (whether a new transaction needs to be created to process the call to methodb) must be set through transaction attribute. Specific Transaction attributes include required, requiresnew, mandatory, notsupported, supports, and never.

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.