Sample Illustrating Savepoint Support in JDBC

Table Of Contents 

Overview of the Sample Application 

Back To Top

This sample illustrates the usage of the JDBC 3.0 feature, Savepoint. Savepoints provide fine-grained control of transactions by marking intermediate points within a transaction. Once a savepoint has been set, the transaction can be rolled back to that savepoint without affecting preceding work. The DatabaseMetaData.supportsSavepoints method can be used to determine whether a JDBC API implementation supports savepoints.

The Savepoint interface allows you to partition a transaction into logical breakpoints, providing control over how much of the transaction gets rolled back. With the help of a small bank application which displays the details of all the accounts in the bank and how to manage it, this sample illustrates

  • How to create a savepoint in the transaction
  • How to rollback to a particular savepoint in the transaction
  • How to rollback the complete transaction
  • How transaction can be handled explicitly while transferring of accounts

Using this application, user can carry out following operations :

  • Update their account balance and thus create a transaction
  • As a banker he can save the changes he made to the accounts in the bank
  • Rollback the changes be made upto a point selected
  • Commit the changes he made to the accounts and store the changes in database when required
  • Transfer amount between two accounts

The account number, customer name and balance present in the account is stored in a table. As and when required the transaction can be started by updating an account and removed by committing or doing a complete rollback. The unchanged changes made at the time of exit are rolled back.

Here is the code explaining retrieving PL/SQL Index By table OUT parameter. You can find more details of the code in SavepointSample.java file under src\oracle\otnsamples\oracle9ijdbc\savepoint folder. Look into Description of Sample Files section for folder and file details.

Creating a savepoint
.....    
      // This method helps to creates an unnamed savepoint in the
      // current transaction and returns the new Savepoint object that
      // represents it. The syntax of this method is
      // Savepoint setSavepoint()
      // This method raises SQLException if the Connection is in auto commit mode.
      Savepoint savepoint = conn.setSavepoint();
......
Rollback from a savepoint
.....    
     // Get the savepoint id from it.
      int selectedSavepointId = Integer.parseInt(selectedLogDetails.get(0).toString());

      // Using the savepoint id selected above get the savepoint object
      // which was got when the Savepoint was created from the hash table.
      Savepoint savepoint = (Savepoint)savepointsCreated.get(new Integer(
                                                    selectedSavepointId));

      // This method undoes all changes after the given Savepoint object was set
      // in the current transaction and releases any database locks currently
      // held by this Connection object. It is similar to
      // the Rollback command in SQL. The syntax of this method is

      // void rollback(Savepoint savepointName)
      //          where savepointName specifies the point up to which the
      // transaction has to be rolled back.
      conn.rollback(savepoint);
....

 

Notations used

This following notations are used through out this document  
Notation
Description
<SAMPLE_HOME>
Folder where the SavepointSample will be unzipped. For example, C:\OTNSamples
<JAVA_HOME>
Folder where JAVA is installed. For example, C:\jdk1.2
<ORACLE_HOME>
Folder where Oracle Database or the Client is installed. For example, C:\ora9i
<JDBC_HOME>
Folder where the Oracle JDBC driver is installed. For example, C:\ora9i\jdbc\lib

 

Required Software

Back To Top

  • Oracle9i JDeveloper ( Note: Oracle9i JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here )
    or
    JDK1.4.x or above This can be downloaded from here .
  • Oracle9i Database or higher running SQL*Net TCP/IP listener. This can be downloaded from here .
  • Oracle9i JDBC Driver for use with JDK 1.4. Note that the ojdbc14.jar can be downloaded from here. The JDBC driver is also available at <ORACLE_HOME>/jdbc/lib.

 

Application Set-up and Configuration

Back To Top
  • Unjar the provided SavepointSample.jar using the following command 
  • > jar xvf SavepointSample.jar

    Note: You will find jar.exe in <JAVA_HOME>\bin. Ensure <JAVA_HOME>\bin is present in your system path.  For setting up environment variables in different platforms, please refer environment set up readme document

    This creates a folder
    SavepointSample with all the source files.

  • Edit Connection.properties file in <SAMPLE_HOME>\SavepointSample directory

    Change the following lines:

    # Your Database Connection details 
      HostName  = insn104a.idc.oracle.com
      Database  = ora9idb
      Port      = 1522
      UserName  = scott
      Password  = tiger

Database Setup

 

This sample application requires that the following table exists in the database.

Table Name : Account_Master

Column Name                   Column Type
-------------------------------------------------------
Account_Number            NUMBER (8)
Customer_Name            VARCHAR2(50)
Balance                            NUMBER
      This table holds the details of all the accounts present in the bank.

To create these, run SQL*Plus , connect to your test database and execute the PlsqlTablesSample.sql script as shown below.

SQL>@<SAMPLE_HOME>\SavepointSample\database\SavepointSample.sql

Specify the same username/password as connection params in the Connection.properties file.

 

Running the application using Oracle9i JDeveloper

Back To Top

This section describes the steps required to run this application using Oracle9i JDeveloper .

    • Open Oracle9i JDeveloper and use File/Open option to select the SavepointSample.jws from the SavepointSample directory.
    • Next, select Project/Make SavepointSample.jpr from main menu.
    • Select the SavepointSample.java file and select Run/Run SavepointSample.java from JDeveloper main menu to run the application.
      Note : JDK1.4 must be set as the JDK version before running the sample.

Running the application from command line

Back To Top

This section describes steps to run the application from console using JDK for Windows and Redhat Linux Advanced Server version 2.1. This application can be run either manually or using a script file.

Run the application using script file :

  • Make sure that the environment variables[<JAVA_HOME> and <JDBC_HOME>] have been set before proceeding futher. For more information on how to setup these environment variables in different platforms, please refer environment set up readme document


  • Now the sample application can be directly run by just executing the script file: run.bat/run.sh from the command prompt, from SavepointSample directory.

  • Execute the script file in Windows as follows
    D:\SavepointSample\run

    For Redhat Linux Advanced Server version 2.1 environment, execute the script file as follows:
    $sh run.sh

Running the application manually:

  • Set CLASSPATH to include Oracle9i JDBC Driver file: ojdbc14.jar
  • . For more information on how to setup environment variables in different platforms, please refer environment set up readme document

  • Also add <SAMPLE_HOME>\SavepointSample directory where Connection.properties exists and the <SAMPLE_HOME>\SavepointSample\src directory to the CLASSPATH

  • Make sure that <JAVA_HOME>/bin is in the path.

  • From the directory <SAMPLE_HOME>\SavepointSample\src\oracle\otnsamples\oracle9ijdbc\savepoint, compile all the java files using javac:
    Example:
     
       javac -d <SAMPLE_HOME>\SavepointSample\src *.java

  • Run the class file using java
    Example:

    java oracle.otnsamples.oracle9ijdbc.savepoint
    .SavepointSample

Description of Sample Files 

Back To Top

The directory structure of the deliverable SavepointSample.jar will be as shown below. SavepointSample is the top level directory

Directory
Files
Description
SavepointSample SavepointSample.jws The Oracle9i JDeveloper workspace file
SavepointSample.jpr The Oracle9i JDeveloper project file
Connection.properties This file has the details of the database connection parameters
run.bat The batch file to compile and run the sample in Windows environment
run.sh The shell script file to compile and run the sample in Linux environment
SavepointSample\database SavepointSample.sql This file creates the package, tables and sequence required by the sample .
SavepointSample\src\oracle\otnsamples\oracle9ijdbc\savepoint SavepointSample.java The Source file for the Sample
SavepointFrame.java The Source file for the sample User Interface
TransferAccountFrame.java This file contains the User Interface for transferring account
UpdateAccountFrame.java This file contains the User Interface for updating the account.
GenTableModel.java The source file for the GenTableModel class, which  handles the JTable data.

 


Please enter your comments about this sample in the OTN Sample Code Discussion Forum.
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy