JTA (XA) transactions and JDBC transactions

Source: Internet
Author: User

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
*

@author Javer qq:84831612
* @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.")
");}
Out.println ("catch:" + e.getclass () + ";" + e.getmessage () + "
");
}finally{
if (conn!=null)
Try{conn.close ();} catch (Exception E1) {out.println ("Finally: failed to close the database connection.
");}
}
   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

/**
*
*/
Package Com.sun;

Import Javax.naming.InitialContext;
Import javax.naming.NamingException;
Import Javax.sql.DataSource;
Import javax.transaction.SystemException;
Import javax.transaction.UserTransaction;

Import java.sql.Statement;
Import java.sql.Connection;

/**
* @author Administrator
*
*/
public class Jtatest {

/**
* @param args
*/
public static InitialContext GetContext () {
InitialContext IC = null;
try {
IC = new InitialContext ();
catch (Namingexception e) {
E.printstacktrace ();
}
return IC;

}

public static UserTransaction Getusertransaction () {
UserTransaction ut = null;
try {
UT = (usertransaction) getcontext (). Lookup ("UserTransaction");
catch (Namingexception e) {
E.printstacktrace ();
}
return UT;

}

public static void Main (string[] args) {
InitialContext IC = null;
UserTransaction ut = null;
DataSource ds = null;

try {
IC = GetContext ();
UT = getusertransaction ();
ds = (DataSource) ic.lookup ("Mysqlds");

Ut.begin ();

Connection C = ds.getconnection ();
Statement s = c.createstatement ();
S.execute ("INSERT into student values (2, ' hehe ')");

Ut.commit ();
SYSTEM.OUT.PRINTLN ("Successful transaction processing");

catch (Exception e) {
try {
Ut.rollback ();
catch (IllegalStateException E1) {
E1.printstacktrace ();
catch (SecurityException E1) {
E1.printstacktrace ();
catch (SystemException E1) {
E1.printstacktrace ();
}
E.printstacktrace ();
}

}

}

Related Article

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.

Tags Index: