Java EE Technical Specification (vii)--JTA (understanding JTA, writing simple transaction client program)

Source: Internet
Author: User
Tags rollback stmt

Before the content, wrote a few about the Java EE Standard Blog, and now continue to improve these content, this content mainly to add a blog.
WebLogic Server uses JTA1.0.1A to implement and manage transactions WebLogic Server provides the following support: creating a unique transaction marker (XID) supporting an optional transaction name tracking the database that the transaction is involved in notifying transactions use XA overwrite 2PC perform rollback perform automatic recovery process management timeout on failure

To process a transaction:

scope of the transaction: transactions can span: EJB Access JDBC Connection JMS connection

examples of EJBS and JDBC in a transaction How transactions can be mixed across EJB method calls and JDBC connections:

...
Context Tc=new InitialContext ();
UserTransaction ut= (usertransaction)
	ic.lookup ("javax.transaction.UserTransaction");
Myejbhome ejbhome= (myejbhome)
	ic.lookup ("Java:com/env/ejb/myejbhome");
MYEJB ejb=ejbhome.create ();
DataSource ds= (DataSource)
	ic.lookup ("Java:com.env/jdbc/mydb");
Connection con=ds.getconnection ();
Statement stmt=con.createstatement ();
Ut.begin ();
Ejb.methoda ();
Ejb.methodb ();
Stmt.executequery (...);
Stmt.executeupdate (...);

exceptions in a transaction Servlets must be aware of exceptions and their implementation in terms of transactions the EJBS involved in a transaction may throw the following exceptions: Application exceptions (Exception applied)--if the EJB marks the transaction as rollback, then the client can only roll back. If no rollback is marked, the client can commit or roll back. System or non-applied exception-The transaction is marked for rollback. The client can only roll back one, the JMS operations involved in the transaction may throw JMSException (1) The JDBC operations involved in the transaction may throw SqlException (2) in both cases, Client programs can be committed or rolled back.
examples of catching exceptions and rolling back (1) When a failure event is encountered, the transaction can perform the appropriate cleanup operation:

...
Context Ic=new InitialContext ();
UserTransaction ut= (usertransaction)
	ic.lookup ("javax.transaction.UserTransaction");
Ut.begin ();
Withdraw (fromaccount,amount);
Deposit (toaccount,amount);
Ut.commit ();
} catch (Customexception e) {
	System.out.println (e);
	try{
		ut.rollback ();
} catch (SystemException se) {
	System.out.println (e);
} catch (Exception e) {
	System.out.println (e);
	}
}

JTA the content of the first here, our next blog will continue to improve the Java EE specifications of other blogs.

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.

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.