JTA (Java Transaction API) transaction __java

Source: Internet
Author: User
Tags getmessage rollback jboss
Introduction to Transactions

   in general, the Java EE Application Server supports JDBC transactions, JTA (Transaction API) transactions (typically managed by a container). In general, it is best not to use the above three transaction types in your program, such as nested JDBC transactions in JTA transactions. Second, the transaction should be completed in as short a time as possible without the use of the transaction in different methods (the nesting of transactions requires a better design). JDBC Transaction

  How to combine multiple SQL statements into one transaction in JDBC. In JDBC, when you open a Connection object connection, the default is Auto-commit mode, and each SQL statement is treated as a transaction, that is, each execution of a statement automatically gets the transaction confirmation. In order to be able to combine multiple SQL statements into one transaction, the Auto-commit mode is shielded. After the Auto-commit mode is masked, the SQL statement does not get a transaction acknowledgement if the commit () method is not invoked. All SQL after the most recent commit () method call is acknowledged at the method commit () call. For example, the following code:
/**
* Test JDBC transactions in JBoss
* <p> @author Javer qq:84831612</p>
* @date 2005
*/
Jjava.sql.Connection conn = null;
try{
Javax.sql.DataSource ds = (javax.sql.DataSource) context.lookup ("Java:/oracleds");
conn = Ds.getconnection ();
Conn.setautocommit (FALSE);
Java.sql.Statement Statement = Conn.createstatement ();
/**
*
* Database Operations
*
*/
Conn.commit ();
catch (Exception e) {
if (conn!=null)
Try{conn.rollback ();} catch (Exception E1) {out.println ("Catch: Transaction rollback failed.") <br/> ");}
Out.println ("catch:" + e.getclass () + ";" + e.getmessage () + "<br/>");
}finally{
if (conn!=null)
Try{conn.close ();} catch (Exception E1) {out.println ("Finally: failed to close the database connection. <br/> ");}
}
  After all, JDBC transactions are likely to be used frequently by most programmers, and relatively simple, without too much description.JTA (XA) transaction

   the Java Transaction API (JTA) and its sibling Java transaction service (Java Transaction Service JTS) provide a distributed transaction service for the EE platform. A distributed transaction involves one transaction manager and one or more resource managers. A resource manager is any type of persistent data store. The transaction manager is responsible for coordinating communication between all transaction participants.
  The system overhead of the XA protocol is considerable compared to local transactions, and a careful consideration should be given to whether a distributed transaction is really required. Only resources that support the XA protocol can participate in distributed transactions. If a transaction is required to register more than one resource, you need to implement and configure the resource (adapter, JMS, or JDBC connection pool) involved to support XA. JTA Transaction Workflow

   Web servers (for example: WebLogic server) will return different kinds of wrappers based on the following criteria:

    1. Whether the JDBC driver class used supports XA
    2, from the DataSource or from the Txdatasource to get the connection
    3. Whether to run within a transaction when calling Getconnection ()
    4. Access to remote connections via RMI

  The algorithm that determines which wrapper to return works as follows:

JTA Instance Code

/**
* Test the JTA transaction in JBoss
* <p> @author Javer qq:84831612</p>
* @date 2005
*/
Javax.transaction.UserTransaction tx = NULL;
Java.sql.Connection conn = null;
try{
tx = (javax.transaction.UserTransaction) context.lookup ("Java:comp/usertransaction"); Obtain JTA transactions, in this case by the JBoss container management
Javax.sql.DataSource ds = (javax.sql.DataSource) context.lookup ("Java:/xaoracleds"); To get a database connection pool, you must have a database, driver that supports XA
Tx.begin ();
conn = Ds.getconnection ();
Conn.setautocommit (FALSE);
Java.sql.Statement Statement = Conn.createstatement ();
String sql = "INSERT into TestTable (CELL1,CELL2,CELL3,CELL4) VALUES (' +system.currenttimemillis () +" ', ', ', ', ') ";
int insert = statement.executeupdate (SQL);
Conn.commit (); Do not nest JDBC transactions in JTA transactions ... Important, remember, otherwise it will throw an exception ...
Out.println ("inserted + Insert +" row record.) <br/> ");
if (true) throw new Exception ("intentionally thrown exception.") ");
int num = statement.executeupdate ("delete testtable");
Out.println ("the + num +" row record was deleted.) <br/> ");
Tx.commit ();
catch (Exception e) {
if (tx!=null)
Try{tx.rollback ();} catch (Exception E1) {out.println ("Catch: Transaction rollback failed.") <br/> ");}
Out.println ("catch:" + e.getclass () + ";" + e.getmessage () + "<br/>");
}finally{
if (conn!=null)
Try{conn.close ();} catch (Exception E1) {out.println ("Finally: failed to close the database connection. <br/> ");}
}

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.