![]() |
![]() |
|
|
Using Transactions
This topic describes how to use transactions in CORBA C++, CORBA Java, and ActiveX client applications for the WebLogic Enterprise software. For a more in-depth discussion of using transactions in WebLogic Enterprise applications, see Using Transactions.
For an example of how transactions are implemented in working client applications, see the descrption of the Transactions sample application in the Guide to the University Sample Applications.
For an overview of the TransactionCurrent object, see Chapter 1, "Client Application Development Concepts."
Overview of WebLogic Enterprise Transactions
Client applications use transaction processing to ensure that data remains correct, consistent, and persistent. The transactions in the WebLogic Enterprise software allow client applications to begin and terminate transactions and to get the status of transactions. The WebLogic Enterprise software uses transactions as defined in the CORBAservices Object Transaction Service, with extensions for ease of use.
Transactions are defined on interfaces. The application designer decides which interfaces within an WebLogic Enterprise client/server application will handle transactions. Transaction policies are defined in the Implementation Configuration File (ICF) for C++ server applications, or in the Server Description file (XML) for Java server applications. Generally, the ICF file or the Server Description file for the available interfaces is provided to the client programmer by the application designer.
If you prefer, you can use the Transaction application programming interface (API) defined in the javax.transaction package that is shipped with the BEA WebLogic Enterprise Java version 2.2 software.
Summary of the Development Process for Transactions
The steps for adding transactions to a client application are as follows:
Step |
Description |
---|---|
1 |
Use the Bootstrap object to obtain a reference to the TransactionCurrent object in the specified WebLogic Enterprise domain. |
2 |
Use the methods of the TransactionCurrent object to include the interface of a CORBA object in a transaction operation. |
The following sections describe these steps and use portions of the client applications in the Transactions sample application to illustrate the steps. For information about the Transactions sample application, see the Guide to the University Sample Applications. The Transactions sample application is located in the following directory on the WebLogic Enterprise software kit:
drive:\WLEdir\samples\corba\university\transactions
Step 1: Using the Bootstrap Object to Obtain the TransactionCurrent Object
Use the Bootstrap object to obtain an object reference to the TransactionCurrent object for the specified WebLogic Enterprise domain. For a complete description of the TransactionCurrent object, see Using Transactions.
The following C++, Java, and Visual Basic examples illustrate how the Bootstrap object is used to return the TransactionCurrent object:
C++
CORBA::Object_var var_transaction_current_oref =
Bootstrap.resolve_initial_references("TransactionCurrent");
CosTransactions::Current_var transaction_current_oref=
CosTransactions::Current::_narrow(var_transaction_current_oref.in());
Java
org.omg.CORBA.Object transCurObj = gBootstrapObjRef.resolve_initial_references
("TransactionCurrent");
org.omg.CosTransactions.Current gTransCur=
org.omg.CosTransactions.CurrentHelper.narrow(transCurObj);
Visual Basic
Set objTransactionCurrent = objBootstrap.CreateObject("Tobj.TransactionCurrent")
Step 2: Using the TransactionCurrent Methods
The TransactionCurrent object has methods that allow a client application to manage transactions. These methods can be used to begin and end transactions and to obtain information about the current transaction. The TransactionCurrent object provides the following methods:
Creates a new transaction. Future operations take place within the scope of this transaction. When a client application begins a transaction, the default transaction timeout is 300 seconds. You can change this default, using the set_timeout method.
Ends the transaction successfully. Indicates that all operations on this client application have completed successfully.
Forces the transaction to roll back.
Marks the transaction so that the only possible action is to roll back. Generally, this method is used only in server applications.
Suspends participation in the current transaction. This method returns an object that identifies the transaction and allows the client application to resume the transaction later.
Resumes participation in the specified transaction.
Returns the status of a transaction with a client application.
Returns a printable string describing the transaction.
Modifies the timeout period associated with transactions. The default transaction timeout value is 300 seconds. If a transaction is automatically started instead of explicity started with the begin() method, the timeout value is determined by the value of the TRANTIME parameter in the UBBCONFIG file. For more information about setting the TRANTIME parameter, see Administration Guide available from the WebLogic Enterprise online information set.
Returns a control object that represents the transaction.
A basic transaction works in the following way:
The association between the transaction and the client application ends when the client application calls the Tobj::TransactionCurrent:commit()method or the Tobj::TransactionCurrent:rollback() method.The following C++, Java, and Visual Basic examples illustrate using a transaction to encapsulate the operation of a student registering for a class:
C++
//Begin the transaction
var_transaction_current_oref->begin();
try {
//Perform the operation inside the transaction
pointer_Registar_ref->register_for_courses(student_id, course_number_list);
...
//If operation executes with no errors, commit the transaction:
CORBA::Boolean report_heuristics = CORBA_TRUE;
var_transaction_current_ref->commit(report_heuristics);
}
catch (...) {
//If the operation has problems executing, rollback the
//transaction. Then throw the original exception again.
//If the rollback fails,ignore the exception and throw the
//original exception again.
try {
var_transaction_current_ref->rollback();
}
catch (...) {
TP::userlog("rollback failed");
}
throw;
}
Java
try{
gTransCur.begin();
//Perform the operation inside the transaction
not_registered =
gRegistrarObjRef.register_for_courses(student_id,selected_course_numbers);
if (not_registered != null)
//If operation executes with no errors, commit the transaction
boolean report_heuristics = true;
gTransCur.commit(report_heuristics);
} else gTransCur.rollback();
} catch(org.omg.CosTransactions.NoTransaction nte) {
System.err.println("NoTransaction: " + nte);
System.exit(1);
} catch(org.omg.CosTransactions.SubtransactionsUnavailable e) {
System.err.println("Subtransactions Unavailable: " + e);
System.exit(1);
} catch(org.omg.CosTransactions.HeuristicHazard e) {
System.err.println("HeuristicHazard: " + e);
System.exit(1);
} catch(org.omg.CosTransactions.HeuristicMixed e) {
System.err.println("HeuristicMixed: " + e);
System.exit(1);
}
Visual Basic
' Begin the transaction
'
objTransactionCurrent.begin
'
' Try to register for courses
'
NotRegisteredList = objRegistrar.register_for_courses(mStudentID,
CourseList, exception)
'
If exception.EX_majorCode = NO_EXCEPTION then
' Request succeeded, commit the transaction
'
Dim report_heuristics As Boolean
report_heuristics = True
objTransactionCurrent.commit report_heuristics
Else
' Request failed, Roll back the transaction
'
objTransactionCurrent.rollback
MsgBox "Transaction Rolled Back"
End If
![]() |
![]() |
![]() |
|
Copyright © 2000 BEA Systems, Inc. All rights reserved.
|