Transactions are an essential part of programming. To standardize transaction development, Java has added transaction specifications, namely, JTA and JTs. You can search for JTA and JTs on the Internet, the discovery of valid content is much less than that of ejbs. This does not mean that they are less important than ejbs. Instead, it indicates that JTA and JTs are more transparent and most details are hidden. Generally, we only need to use a few simple APIs to solve the problem.
JTA and JTs JTA
JTA defines a set of interfaces, including several main roles: transactionmanager, usertransaction, transaction, and xaresource, and defines the rules to be observed between these roles, for example, delegate a transaction to transactionmanager.
JTs
JTs is also a set of specifications. As mentioned above, how should roles interact with each other? JTs defines the interaction details.
In general, JTA is more about agreeing on the Interface of program roles from the Framework perspective, while JTs is about agreeing on interfaces between program roles from the perspective of specific implementation.
Because JTA is relatively higher, we mainly focus on JTA.
XA
XA protocol, which specifies the Transaction Manager and resource manager interfaces and adopts the two-phase commit protocol.
Source code
The standard interface is located in javax. transaction:
Talk about transactions
Transactions are classified into local transactions and distributed transactions.
Local transaction
Local transactions are more common. You can understand the transactions derived from connection. because such transactions are bound to connections, they can only work for one connection and one Database. The following code:
Public void transferaccount () {connection conn = NULL; statement stmt = NULL; try {conn = getdatasource (). getconnection (); // set Automatic commit to false. // if it is set to true, the database identifies each data update as a transaction and automatically submits Conn. setautocommit (false); stmt = Conn. createstatement (); // reduce the amount in account A by 500 stmt.exe cute ("Update t_account set amount = amount-500 where account_id = 'A '"); // increase the amount in account B by 500 stmt.exe cute ("Update t_account set amount = Amount + 500 where account_id = 'B'"); // submit the transaction Conn. commit (); // transaction commit: the two-step transfer operation is successful at the same time} catch (sqlexception sqle) {try {// exception occurs, and the rollback operation in this transaction is Conn. rollback (); // transaction rollback: the two-step operation of the transfer completely revokes stmt. close (); Conn. close ();} sqle. printstacktrace ();}}
Distributed transactions
JTA and JTs can be used to provide distributed transaction services. Distributed Transaction includes the Transaction Manager and resource manager of Xa protocol ), the resource manager can be seen as any type of persistent data storage. The transaction manager is responsible for transaction coordination and control.
The following is an example of using JTA to process transactions (Conna and connb are connections from different databases:
Public void transferaccount () {usertransaction usertx = NULL; connection Conna = NULL; Statement statement TA = NULL; connection connb = NULL; statement stmtb = NULL; try {// get the transaction management object usertx = (usertransaction) getcontext (). lookup ("Java: COMP/usertransaction"); // obtain database connection Conna = getdatasourcea () from database (). getconnection (); // obtain the database connection connb = getdatasourceb () from database B (). getconnection (); // start the transaction usertx. begin (); // reduce the amount in account A by 500 stmta = Conna. createstatement (); ta.exe cute ("Update t_account set amount = amount-500 where account_id = 'A'"); // increase the amount in account B by 500 stmtb = connb. createstatement (); stmtb.exe cute ("Update t_account set amount = Amount + 500 where account_id = 'B'"); // submit the transaction usertx. commit (); // transaction commit: the two-step transfer operation is successful at the same time (data in database A and database B is updated at the same time)} catch (sqlexception sqle) {try {// an exception occurs, and the usertx is rolled back in this transaction. rollback (); // transaction rollback: the two-step transfer operation is completely canceled // (data updates in database A and database B are also revoked) stmt. close (); Conn. close (); sqle. printstacktrace ();} catch (exception ne) {e. printstacktrace ();}}}
The preceding figure shows a distributed transaction.
JTA transactions effectively shield underlying transaction resources. However, compared with local transactions, XA has a high system overhead. During system development, you should carefully consider whether distributed transactions are required. If distributed transactions are required to coordinate multiple transaction resources, implement and configure transaction resources that support the Xa protocol, such as JMS and JDBC database connection pools.
EJB transactions
JTA is used to provide transactions for ejbs. It is also intuitive to use annotations. You only need to provide @ transactionmanagement and @ transactionattribute, and configure the corresponding attributes, as shown in:
@Stateless@Remote({ ITemplateBean.class })@TransactionManagement(TransactionManagementType.CONTAINER)@TransactionAttribute(TransactionAttributeType.REQUIRED)public class TemplateBeanImpl implements ITemplateBean {//code...}
Summary Key Interfaces
First, we will introduce the following interfaces in the JTA standard interface:
- Usertransaction: programmer interface
- Transactionmanager: the transaction management interface left to the vendor.
- Transaction: transactions left to the vendor
- Xaresource: reserved for the vendor-implemented Interface related to persistent Resources
For the interface definition in the interface class, see the source code. The relationship between several interface classes is as follows:
After downloading JBoss's JTA implementation source code, it is difficult to go deep into the source code, and it takes a long time to learn it later. The JTA Implementation of JBoss is Narayana. The SVN address is https://github.com/jbos#/narayana. If you are interested, you can study it.
Summary
JTA has come to an end. For more information about transaction phase commit, see my blog "transaction Phase 1 commit protocol and phase 2 commit protocol".
Besides, JTA and JTs