JTA & Transaction Basic Information

xiaoxiao2021-03-06  41

http://www.onjava.com/pub/a/onjava/2001/05/23/j2ee.html

- Appserver use transaction manager to enalble and application component to perform transactional access across multiple EIS resource managers (distributed system) * Appserver provides transaction service * The transaction manager supports JTA XAResource * Every EIS resource must implement the JTA XAResource to be as a.. Interface in Order to Be Controlled by Transaction Manager.

- Supporting Access to a Single JDBC Database Withnin a Transaction (Multiple Connections to the Same Database Are ALOWED) IS Required by J2EE Platform.

- Supporting Transaction Across Multiple (Same / DiffERENT) Database IS NOT Required. 2PC is Maybe Not Provided by J2EE Produc.

- Three Pheromena About Database Access - DIRTY READS

1> COLUMNA = 1 2> T1 Begins 3> T2 Begins 4> T2 Change Columna To 2 5> T1 Read Column - 2 6> T2 Rollback 7> T1 Holds A DIRTY DATA - 2 8> T1 Does More .. 9> T1 commit

(In t1, t1 using a uncomited data) - nonrepeatable reads 1> Columna = 1 2> T1 Begins 3> T1 Read Columna - 1 4> T2 Begains 5> T2 Change Columna To 2 6> T2 Commit 7> T1 Re-Read Columna - 2 8> T1 Does More .. 9> T1 Commit

(In T1, THE Columna's Values ​​Read Twice Are Different) - Phantom READS

1> TABLEA HAS 50 Records 2> T1 Begins 3> T1 Query DB, 50 Records Returned Staisfies Conditon1 4> T2 Begin 5> T2 Insert 1 Record Satisfies The Condition1 6> T2 Commit 7> T1 Re-Search Base On The Same Condition, And 51 Records Returned 8> T1 Does More .. 9> T1 Commit

(In T1, THE RECORDS RETURNED of The Same 2 Query Are Different) - Isolation Levels

- Transaction_read_uncommitted Nothing Will BE Controlled

- Transaction_read_committed in a Transaction, Just Data Commit Can Be Read

- Transaction_repeAtable_read in a Transaction, Read Same Data Many Times, The Returned Result Will Be Same

- Transaction_Serializable The Transaction Has Exclusive Read and Update Privile to Data By Locking It. Other Transaction Cannot Access (Read / Write) The Data Util The Holder Transaction Releases Data's Lock.

Isolation level summary Isolation Level Dirty Read Non Repeatable Read Phantom Read TRANSACTION_READ_UNCOMMITTED YES YES YES TRANSACTION_READ_COMMITTED NO YES YES TRANSACTION_REPEATABLE_READ NO NO YES TRANSACTION_SERIALIZABLE NO NO NO

- All RDBMS or EIS used by a J2EE application should use the same isolation level for consistency reasons since the current J2EE specification does NOT define a standard way to set isolation levels when an EIS is accessed via JTA transactions If a J2EE product does not provide. A Way to configure teh isolation level

- Strongly recommend not to change the isolation level within a transaction, especially some work has already been done, some are to be done Some EIS will force commit transaction if you want to change isolation level in the middle of a transaction.

- There Are 2 Ways to Begin A JTA Transaction 1) Explicitly Using The JTA Javax.Transaction.UserTransaction Interface in Your Code 2) Implicitly Controlled by EJB Container if Using CMP

- Case 1 Stand-alone client -> EJB Container -> RDBMS / EIS Transaction support in applets and application clients is not required by the J2EE platform This is a added value of the J2EE product, you should make sure whether the appserver you are using can support it.- Case 2 Browser -> Web Container -> RDBMS / EIS It's important to keep in mind that a web component like a servlet may only start a transaction in its servic () method moreover, the transaction can not span across. Web Requests.

- Code Snippet Context myCntxt = new InitialContext (); UserTransaction ut = (UserTransaction) myCntxt.lookup ( "java: comp / UserTransaction"); ut.begin (); // perform transactional work here ut.commit (); - The following guidelines are recommended for handling interactions in web components between JTA transactions, threads, and JDBC connections -.. JTA transactions should start and complete only from the thread in which the service method is called Additional threads created in the servlet should not attempt to start any JTA transaction -. JDBC connections may be acquired and released by a thread other than the service method thread, but should not be shared between threads -. JDBC Connection objects should not be stored in static fields -. For web components implementing the SingleThreadModel, JDBC Connection Objects May Be Stored In Class Instance Fields. - for Web Components (servlets) Not Implementing The SingleThreadMode .

- Case 3 Browser -> Web Container -> EJB Container -> RDBMS / Eis - Code Snippet Used by bmp UserTransaction Ut = EjbContext.getusertransAction (); ut.begin (); // Transactional Work Is Done Here Ut.commit () ; - The following example illustrates a business method of a typical session bean that performs a bean managed transaction involving both a database connection and a JMS connection.public class MySessionEJB implements SessionBean {EJBContext ejbContext; public void someMethod (...) {javax. transaction.UserTransaction ut; javax.sql.DataSource ds; java.sql.Connection dcon; java.sql.Statement stmt; javax.jms.QueueConnectionFactory qcf; javax.jms.QueueConnection qcon; javax.jms.Queue q; javax.jms Javax.jms.queuesender Qsender; javax.jms.Message Message; InitialContext INITCTX = New InitialContext ();

// Obtain DB Conn Object and set it up for transactions DS = (javax.sql.datasource) INitctX.lookup ("java: comp / env / jdbc / database"); dcon = ds.getConnection (); STMT = DCON. CreateStatement ();

// obtain jms conn object and set up session for transactions qcf = (javax.jms.QueueConnectionFactory) initCtx.lookup ( "java: comp / env / jms / qConnFactory"); qcon = qcf.createQueueConnection (); qsession = qcon. CreatequeueESession (True, 0); q= (javax.jms.queue) INitctX.lookup ("java: comp / env / jms / jmsqueue); QSender = Qsession.createender (q); message = Qsession.createTextMessage (); Message.Settext ("Some Message"); //// Now do a transaction That Involves the Two Connections. // Ut = EjbContext.getusertransAction (); // Start the Transaction Ut.begin (); // DO Database Updates And send Message. THE Container // AutomaticLists DCON AND qsession with the // Transaction. Stmt.executeQuery (...); stmt.executeUpdate (...); QSender.sen D (Message); // Commitate; // Release Connections Stmt.close (); QSender.Close (); Qsession.Close (); dcon.close (); QCon.Close (); QCON.CLOSE (); } ...}

- The Following Example Illustrates a stateful session bean That Retains Transaction Context Across Three Client Calls, Invoked In The Order {Method1, Method2, And Method3}.

public class MySessionEJB implements SessionBean {EJBContext ejbContext; javax.sql.DataSource ds1; javax.sql.DataSource ds2; java.sql.Connection con1; java.sql.Connection con2; public void method1 (...) {java.sql. Statement stmt; InitialContext initCtx = new InitialContext (); // obtain user transaction interface ut = ejbContext.getUserTransaction (); // start a transaction ut.begin (); // make some updates on con1 ds1 = (javax.sql. DataSource) Initctx.lookup ("java: comp / env / jdbc / database1"); con1 = ds1.getConnection (); stmt = con1.createstatement (); stmt.executeUpdate (...); stmt.executeupdate. .); ////////////////////// ' tement stmt; InitialContext initCtx = new InitialContext (); // make some updates on con2 ds2 = (javax.sql.DataSource) initCtx.lookup ( "java: comp / env / jdbc / Database2"); con2 = ds2.getConnection ( ); Stmt = con2.createstatement (); stmt.executeUpdate (...); stmt.executeUpdate (...); // the contact with the transaction associated with the next client call (Which is Method3 (...)).} Public void method3 (...) {java.sql.Statement Stmt; // Obtain User Transaction Interface UT = EjbContext.getusertransAction ();

// make some more updates on con1 and con2 stmt = con1.createStatement (); stmt.executeUpdate (...); stmt = con2.createstatement (); stmt.executeUpdate (...); // Commit The Transaction Ut .commit (); // Release Connections Stmt.close (); con1.close (); con2.close ();} ...} - this is the transactions started in method1 and committed in method3. public class myssionejb imports sessionbean {EJBContext ejbContext; InitialContext initCtx; public void method1 (...) {java.sql.Statement stmt; // obtain user transaction interface ut = ejbContext.getUserTransaction (); // start a transaction ut.begin ();}

Public void method2 (...) {javax.sql.datasource ds; java.sql.connection con; java.sql.statement stmt; // open connection ds = (javax.sql.datasource) initX.lookup ("Java: Comp / env / jdbc / database "); con = ds.getConnection (); // make some updates oncon stmt = con.createstatement (); stmt.executeUpdate (...); stmt.executeUpdate (...) ; // close the connection stmt.close (); con.close ();} public void method3 (...) {// Obtain user Transaction interface ut = ejbcontext.getusrtransaction (); // commit the transaction ut.commit ();

转载请注明原文地址:https://www.9cbs.com/read-56788.html

New Post(0)