The EJB transaction class enables PowerBuilder clients to control a transaction on an EJB server. EJBTransaction maps closely to the javax.transaction.UserTransaction interface.
EJBTransaction has six member functions:
Creates a new transaction and associates it with the current thread.
ejbtrans.Begin ( )
Argument |
Description |
---|---|
ejbtrans |
The name of an EJBTransaction object |
None
The following example shows the use of begin to create a transaction from a client:
EJBTransaction trans EJBConnection conn string properties[ ] // set properties ..... conn = create ejbconnection TRY conn.connectToServer(properties) trans = conn.GetEjbTransaction trans.begin() CATCH (exception e) messagebox("exception", e.getmessage()) END TRY
GetEJBTransaction (EJBConnection class)
Declares that the transaction associated with the calling thread should be committed.
ejbtrans.Commit ( )
Argument |
Description |
---|---|
ejbtrans |
The name of an EJBTransaction object |
None
In this example, the client calls the dopayroll method on the CmpnyAcct EJB component, which processes a company payroll. If the company has sufficient funds to meet the payroll, the client commits the transaction. Otherwise, an exception is thrown and the client rolls back the transaction:
// Instance variables: // EJBTransaction trans // EJBConnection conn // CmpnyAcctHome AcctHome // CmpnyAcct Acct TRY trans.begin() AcctHome = conn.lookup("CmpnyAcctHome", "Sample/CmpnyAcct", "sample.CmpnyAcctHome") Acct = AcctHome.create() Acct.dopayroll() trans.commit() CATCH (remoteexception re) messagebox("remoteexception", re.GetMessage()) CATCH (createexception ce) messagebox("createexception", ce.GetMessage()) CATCH (exception e1) MessageBox ("exception", e1.getmessage() ) TRY trans.rollback(); CATCH (exception e2) MessageBox ("exception", e2.getmessage() ) END TRY END TRY
The Commit method completes the transaction associated with the calling thread. The transaction is not completed if any other participants in the transaction vote to roll back the transaction.
GetEJBTransaction (EJBConnection class)
Returns the status of the EJB transaction associated with the client.
ejbtrans.GetStatus ( )
Argument |
Description |
---|---|
ejbtrans |
The name of an EJBTransaction object |
A long value representing the transaction status
Possible values are:
This example shows the use of GetStatus to obtain the state of the current transaction:
// Instance variables: // EJBConnection myconnect EJBTransaction mytrans
long ll_status mytrans = myconnect.GetEJBTransaction() ll_status = mytrans.GetStatus()
The GetStatus method can be used to determine the current status of a transaction by the client that initiated the transaction using the Begin method.
GetEJBTransaction (EJBConnection class)
Rolls back the transaction associated with the calling thread.
ejbtrans.Rollback ( )
Argument |
Description |
---|---|
ejbtrans |
The name of an EJBTransaction object |
None
This example shows the use of Rollback to roll back a transaction when an update does not succeed:
// Instance variables: // EJBTransaction trans // TRY trans.begin() Acct.updateChecking(amount) trans.commit() CATCH (exception e1) TRY trans.rollback() CATCH (exception e2) MessageBox("Rollback failed", e2.getMessage()) END TRY MessageBox("Transaction failed", e1.getMessage()) END TRY
GetEJBTransaction (EJBConnection class)
Modifies a transaction associated with a calling thread so that the only possible outcome is to roll back the transaction.
ejbtrans.SetRollbackOnly ( )
Argument |
Description |
---|---|
ejbtrans |
The name of an EJBTransaction object |
None
In this example, a participant in a transaction has determined that it should be rolled back. The participant gets a reference to the current transaction and votes to roll back the transaction:
// Instance variables: // EJBConnection conn // EJBTransaction trans trans = conn.GetEJBTransaction() trans.SetRollbackOnly()
Rollback is typically called by the originator of the transaction, but another participant in a transaction can call SetRollbackOnly to vote that the transaction should be rolled back.
GetEJBTransaction (EJBConnection class)
Sets the timeout value for subsequent transactions. The transaction is rolled back if it does not complete before the timeout expires.
ejbtrans.SetTransactionTimeout (long seconds )
Argument |
Description |
---|---|
ejbtrans |
The name of an EJBTransaction object |
seconds |
A long that specifies the number of seconds that elapse before a transaction is rolled back |
None
This example shows the use of SetTransactionTimeout to set the timeout period to five minutes:
// Instance variables: // EJBConnection conn // EJBTransaction trans TRY trans.SetTransactionTimeout(300) trans.begin() CATCH (exception e) MessageBox("Exception", e.getMessage()) END TRY
The SetTransactionTimeout method specifies the number of seconds that can elapse before a transaction is rolled back. The timeout period applies to transactions created by subsequent invocations of Begin. If seconds is 0, no timeout period is in effect.
GetEJBTransaction (EJBConnection class)