4.3.3 Transaction Begin, Commit, and Rollback
In Jolt, a transaction is represented as an object of the class JoltTransaction. The transaction begins when the transaction object is instantiated. The transaction object is created with a timeout and JoltSession object parameter:
trans = new JoltTransaction(timeout, session)
Jolt uses an explicit transaction model for any services involved in a transaction. The transaction service invocation requires a JoltTransaction object as a parameter. Jolt also requires that the service and the transaction belong to the same session. Jolt does not allow you to use services and transactions that are not bound to the same session.
The sample code in the listing “Jolt Transfer of Funds Example (SimXfer.java)” describes how to use the Jolt Class Library and includes the use of the JoltSessionAttributes, JoltSession, JoltRemoteService, and JoltTransaction classes.
The same sample combines two user-defined Oracle Tuxedo services (WITHDRAWAL and DEPOSIT) to perform a simulated TRANSFER transaction. If the WITHDRAWAL operation fails, a rollback is performed. Otherwise, a DEPOSIT is performed and a commit completes the transaction.
The following programming steps describe the transaction process shown in the sample code listing “Jolt Transfer of Funds Example (SimXfer.java)”:
- Set the connection attributes like hostname and portnumber in the JoltSessionAttribute object.
Refer to this line in the following code listing:
sattr = new JoltSessionAttributes();
- The
sattr.checkAuthenticationLevel()
allows the application to determine the level of security required to log on to the server.Refer to this line in the following code listing:
switch (sattr.checkAuthenticationLevel())
- The logon is accomplished by instantiating a JoltSession object.
Refer to these lines in the following code listing:
session = new JoltSession (sattr, userName, userRole, userPassword, appPassword);
This example does not explicitly catch
SessionException
errors. - All JoltRemoteService calls require a service to be specified and the session key returned from
JoltSession()
. Refer to these lines in the following code listing:withdrawal = new JoltRemoteService(“WITHDRAWAL”, session);
deposit = new JoltRemoteService(“DEPOSIT”, session);
These calls bind the service definition of both the WITHDRAWAL and DEPOSIT services, which are stored in the Oracle Tuxedo Service Metadata Repository, to the withdrawal and deposit objects, respectively. The services WITHDRAWAL and DEPOSIT must be defined in the Metadata Repository; otherwise a ServiceException is thrown. This example does not explicitly catch ServiceException errors.
- Once the service definitions are returned, the application-specific fields such as account number ACCOUNT_ID and withdrawal amount SAMOUNT are automatically populated.
Refer to these lines in the following code listing:
withdrawal.addInt(“ACCOUNT_ID”, 100000);
withdrawal.addString(“SAMOUNT”, “100.00”);
The
add*()
methods can throwIllegalAccessError
orNoSuchFieldError
exceptions. - The JoltTransaction call allows a timeout to be specified if the transaction does not
complete within the specified time.
Refer to this line in the following code listing:
trans = new JoltTransaction(5,session);
- Once the withdrawal service definition is automatically populated, the withdrawal service
is invoked by calling the
withdrawal.call(trans)
method.Refer to this line in the following code listing:
withdrawal.call(trans);
- A failed WITHDRAWAL can be rolled back.
Refer to this line in the following code listing:
trans.rollback();
- Otherwise, once the DEPOSIT is performed, all the transactions are committed. Refer to these lines in the following code listing:
deposit.call(trans);
trans.commit();
The following listing shows an example of a simple application for the transfer of funds using the Jolt classes.
Listing Jolt Transfer of Funds Example (SimXfer.java)
/* Copyright 1999 Oracle Systems, Inc. All Rights Reserved */
import bea.jolt.*;
public class SimXfer
{
public static void main (String[] args)
{
JoltSession session;
JoltSessionAttributes sattr;
JoltRemoteService withdrawal;
JoltRemoteService deposit;
JoltTransaction trans;
String userName=null;
String userPassword=null;
String appPassword=null;
String userRole=”myapp”;
sattr = new JoltSessionAttributes();
sattr.setString(sattr.APPADDRESS, “//bluefish:8501”);
switch (sattr.checkAuthenticationLevel())
{
case JoltSessionAttributes.NOAUTH:
System.out.println(“NOAUTH\n”);
break;
case JoltSessionAttributes.APPASSWORD:
appPassword = “appPassword”;
break;
case JoltSessionAttributes.USRPASSWORD:
userName = “myname”;
userPassword = “mysecret”;
appPassword = “appPassword”;
break;
}
sattr.setInt(sattr.IDLETIMEOUT, 300);
session = new JoltSession(sattr, userName, userRole,
userPassword, appPassword);
// Simulate a transfer
withdrawal = new JoltRemoteService(“WITHDRAWAL”, session);
deposit = new JoltRemoteService(“DEPOSIT”, session);
withdrawal.addInt(“ACCOUNT_ID”, 100000);
withdrawal.addString(“SAMOUNT”,“100.00”);
// Begin the transaction w/ a 5 sec timeout
trans = new JoltTransaction(5, session);
try
{
withdrawal.call(trans);
}
catch (ApplicationException e)
{
e.printStackTrace();
// This service uses the STATLIN field to report errors
// back to the client application.
System.err.println(withdrawal.getStringDef(“STATLIN”,”NO
STATLIN”));
System.exit(1);
}
String wbal = withdrawal.getStringDef(“SBALANCE”, “$-1.0”);
// remove leading “$” before converting string to float
float w = Float.valueOf(wbal.substring(1)).floatValue();
if (w < 0.0)
{
System.err.println(“Insufficient funds”);
trans.rollback();
System.exit(1);
}
else // now attempt to deposit/transfer the funds
{
deposit.addInt(“ACCOUNT_ID”, 100001);
deposit.addString(“SAMOUNT”,“100.00”);
deposit.call(trans);
String dbal = deposit.getStringDef(“SBALANCE”, “-1.0”);
trans.commit();
System.out.println(“Successful withdrawal”);
System.out.println(“New balance is: “ + wbal);
System.out.println(“Successful deposit”);
System.out.println(“New balance is: “ + dbal);
}
session.endSession();
System.exit(0);
} // end main
} // end SimXfer
Parent topic: Jolt Class Library Walkthrough