Client-managed transactions
EJB client applications can control transactions on the server
using the EJBTransaction object. This object has methods that enable
the client to begin, commit, or roll back a transaction. The client
can also get the status of a transaction, change its timeout value,
or modify the transaction so that it cannot be committed.
The EJBTransaction methods map directly to the methods of
the javax.transaction.UserTransaction interface,
which is documented in the JTA Specification on the Sun Java Web site
.
Beginning and ending transactions
Clients can obtain access to the methods of the EJBTransaction
class by calling the getEJBTransaction method
of the EJBConnection class:
1 |
ejbconnection conn<br>ejbtransaction trans<br>string properties[]<br> <br>conn = create ejbconnection<br>TRY<br>  conn.connectToServer(properties)<br>  trans = conn.getEJBTransaction()<br>CATCH (exception e)<br>  messagebox("exception", e.getmessage())<br>END TRY |
If an EJBTransaction instance is obtained successfully, you
use its begin method to start the transaction
and its commit or rollback methods
to end it:
1 |
TRY<br>  // Start the transaction<br>  trans.begin()<br>  // Create a component and call methods to be executed<br>  // within the transaction<br>  ...<br>  // Commit the transaction<br>  trans.commit();<br>CATCH (exception e)<br>  messagebox("exception", e1.getmessage())<br>  trans.rollback()<br>END TRY |
Getting information about the transaction
GetStatus returns an integer that indicates
whether the transaction is active, has been marked for rollback,
is in the prepare phase or commit phase,
or has been committed or rolled back.
Setting a timeout period for transactions
A calling thread can specify a timeout period after which
a transaction will be rolled back. This example sets the timeout
period to 3 minutes (180 seconds):
1 |
trans.SetTimeout(180)<br>trans.Begin() |