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:

ejbconnection conn
ejbtransaction trans
string properties[]

conn = create ejbconnection
TRY
  conn.connectToServer(properties)
  trans = conn.getEJBTransaction()
CATCH (exception e)
  messagebox("exception", e.getmessage())
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:

TRY
  // Start the transaction
  trans.begin()
  // Create a component and call methods to be executed
  // within the transaction
  ...
  // Commit the transaction
  trans.commit();
CATCH (exception e)
  messagebox("exception", e1.getmessage())
  trans.rollback()
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):

Example 6

trans.SetTimeout(180)
trans.Begin()