There are three types of Java transactions: JDBC transactions, JTA (Java transaction API) transactions, and container transactions.
1. JDBC transactions
JDBC transactions are controlled by connection objects. The JDBC connection interface (Java. SQL. Connection) provides two transaction modes: Automatic commit and manual commit. Java. SQL. Connection provides the following methods to control transactions:
Public void setautocommit (Boolean)
Public Boolean getautocommit ()
Public void commit ()
Public void rollback ()
When you use the JDBC transaction interface, you can combine multiple SQL statements into one transaction. One disadvantage of JDBC transactions is that the transaction scope is limited to a database connection. A jdbc transaction cannot span multiple databases.
2. JTA (Java transaction API) Transaction
JTA is a high-level implementation-independent and protocol-independent API. Applications and application servers can use JTA to access transactions.
JTA allows applications to execute Distributed Transaction Processing-data is accessed and updated on two or more network computer resources, which can be distributed across multiple databases. The JTA support of the JDBC driver greatly enhances data access capabilities.
If you plan to use JTA to define transactions, you need a JDBC driver that implements the javax. SQL. xadatasource, javax. SQL. xaconnection, and javax. SQL. xaresource interfaces. A driver that implements these interfaces can participate in JTA transactions. One xadatasource
The object is the factory of A xaconnection object. Xaconnection S is the JDBC connection that participates in the JTA transaction.
You will need to use the administration tool of the application server to set xadatasource. You can learn the relevant guidance from the application server and JDBC driver documentation.
J2EE applications use JNDI to query data sources. Once the application finds the data source object, it calls javax. SQL. datasource. getconnection () to obtain the connection to the database.
XA connections are different from non-Xa connections. Make sure that the Xa connection is involved in the JTA transaction. This means that the Xa connection does not support the JDBC automatic submission function. At the same time, applications must not call java. SQL. Connection. Commit () or Java. SQL. Connection. rollback () for XA connections (). Instead, applications should use
Usertransaction. Begin (), usertransaction. Commit (), and sertransaction. rollback ().
3. Container transactions
Container transactions are mainly provided by the J2EE application server, and most container transactions are completed based on JTA. This is a very complicated API implementation based on JNDI. To implement JTA transaction management through relative encoding, we can use the container Transaction Management Mechanism (CMT) provided by the EJB container to complete the same function, which is provided by the J2EE application server. This allows us to simply specify which method to add to the transaction. Once specified, the container will be responsible for the transaction management tasks. This is our solution for civil engineering, because in this way, we can exclude the transaction code from the logic code and hand over all the difficulties
Solve the problem by using the J2EE container. Another advantage of using ejb cmt is that programmers do not need to care about the jta api encoding. However, in theory, we must use EJB.
Iv. Three transaction differences
1. Restrictions of JDBC transaction control are in a database connection, but they are easy to use.
2. JTA transactions have powerful functions. transactions can span multiple databases or multiple Dao transactions, which are also complicated to use.
3. Container transactions mainly refer to the transaction management provided by the J2EE application server, which is limited to the use of EJB applications.
V. Summary
Transaction control is an indispensable part of J2EE application construction. It is vital to choose the transaction to be applied. In general, you can select JDBC transactions when connecting a single JDBC connection. When connecting multiple connections or databases, you need to use JTA transactions. If EJB is used, you can consider using EJB container transactions.