Sample Illustrating Statement Caching Support in Oracle9i JDBC Drivers.

Table Of Contents 

Overview of the Sample Application 

Back To Top

Statement caching prevents the overhead of repeated statement parsing and cursor creation by caching the statement state and meta data. There are two schemes available for statement caching

Implicit Statement Caching :

This scheme automatically caches all Prepared and Callable statements. Each time a statement is created, the cache is searched with the SQL string and statement type( Prepared or Callable), if any match is found the statement is returned from the cache, else a new statement is created. When the user calls the close() method on the statement, it is logically closed and put in the cache(for further use). The whole statement caching process is completely transparent to the developer.

Explicit Statement Caching :

In this scheme, a key(any Java String) is associated with each statement and this key is used to refer to the statement. When a Prepared or Callable statement is closed using stmt.closeWithKey(key), the statement is associated with this key and put in the cache. when the statement has to be created again, OracleConnection.getStatementWithKey(key) is used to retrieve the statement associated with key. Explicit caching has a performance edge over implicit caching since all the details of the statement are cached( statement data, state and meta data).

This sample illustrates the benefits and use of statement caching  with a "Employee Search Application". Consider an employee search application in  which a user can search for employees based on First Name, Department and Salary. Since the skeleton of the statement remains the same except the parameters, it would be ideal to use statement caching( instead of preparing the statement each time).

For more information refer to Oracle9i JDBC Developer's Guide and Reference.

Here is the code explaining Statement Caching. You can find more details of the code in StmtCacheSample.java file under src\oracle\otnsamples\oracle9ijdbc\stmtcache folder. Look into Description of Sample Files section for folder and file details.
Implicit Statement Caching
      // Set the Statement cache size to 5

      ((OracleConnection) conn).setStatementCacheSize(5);

      // Enable Implicit caching
      ((OracleConnection) conn).setImplicitCachingEnabled(true);

      // Set start time
      start = System.currentTimeMillis();

      // Loop, prepare and execute the query 10 times
      for (int i = 1; i <= 10; i++) {

        // The statement is searched in the cache, if a match is found, returns
        // the statement from cache else creates a new statement

        opstmt = (OraclePreparedStatement) conn.prepareStatement(mysql);

        opstmt.setString(1, empName);
        opstmt.setInt(2, Integer.parseInt(deptId[selectedDept]));
        opstmt.setInt(3, lowerLimit);
        opstmt.setInt(4, upperLimit);

        // Execute query
        rset = opstmt.executeQuery();

        // Do not process ResultSet

        // Close the ResultSet
        rset.close();


        // The statement is not closed physically, but put in the cache.
        opstmt.close();

      }
      .......
	  
Explicit Statement Caching
      // Set Statement Cache size to 5
      ((OracleConnection) conn).setStatementCacheSize(5);

      // Enable Explicit caching
      ((OracleConnection) conn).setExplicitCachingEnabled(true);


      // Set start time
      start = System.currentTimeMillis();

      // Loop, prepare and execute the query 10 times
      for (int i = 1; i <= 10; i++) {

        // Retrieve the statement from Explicit cache using key.
        opstmt = (OraclePreparedStatement)
            ((OracleConnection) conn).getStatementWithKey("searchEmployee");

        // If statement not in Explicit cache, create one.
        if (opstmt == null) {

          // This step is reached only when the function is called for the first time
          // from the next time the statement would be retrieved from cache.
          opstmt = (OraclePreparedStatement) conn.prepareStatement(mysql);

        }

        opstmt.setString(1, empName);
        opstmt.setInt(2, Integer.parseInt(deptId[selectedDept]));
        opstmt.setInt(3, lowerLimit);
        opstmt.setInt(4, upperLimit);

        // Execute query
        rset = opstmt.executeQuery();

        // Do not process results

        // Close the ResultSet
        rset.close();

        // Close the statement with a key(any Java String), this key would be used
        // to search for this statement in the explicit cache.

        opstmt.closeWithKey("searchEmployee");
        .....

 

Notations used

This following notations are used through out this document  
Notation
Description
<SAMPLE_HOME>
Folder where the StmtCacheSample 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.2.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. The JDBC driver is available at <ORACLE_HOME>/jdbc/lib or <OC4J_HOME>/jdbc/lib.

 

Application Set-up and Configuration

Back To Top
  • Unjar the provided StmtCacheSample.jar using the following command 
  • > jar xvf StmtCacheSample.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
    StmtCacheSample with all the source files.

  • Edit Connection.properties file in <SAMPLE_HOME>\StmtCacheSample directory
    Change the hostname, port number, and database name to those of the database instance you want to connect to.

    Note : The Employees table required by this sample is part of 'HR' schema.

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 StmtCacheSample.jws from the StmtCacheSample directory.
    • Set CLASSPATH to Oracle9i JDBC drivers. It can be done as follows:
      • Select StmtCacheSample.jpr and click Project/Project Settings, it will display the Project Settings window.
      • The options will be displayed in a tree format. Under Configurations, view the currently active configuration. Go to the Libraries option under the currently active configuration.
      • To add JDBC class libraries, click New button. It will display a window New Library
      • Give some name(Oracle JDBC jars) in the Library Name field shown.
      • Click the Edit button at Class Path field, this will display a new window.
      • Click Add Entry. This will display directory browsing window
      • Browse for <ORACLE_HOME>/jdbc/lib/classes12.jar and <ORACLE_HOME>/jdbc/lib/nls_charset12.jar file.
      • Click Select button
      • Click OK till you return to the Libraries option.

        Note: If JDK 1.4 is used, then ojdbc14.jar (<ORACLE_HOME>/jdbc/lib) must be used instead of classes12.jar
    • Next, select Project/Make StmtCacheSample.jpr from main menu.
    • Select the StmtCacheSample.java file and select Run/Run StmtCacheSample.java from JDeveloper main menu to run the application.

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

  • If JDK 1.4 is used, then ojdbc14.jar(downloadable from OTN) must be used. For all other JDK versions, classes12.jar must be used. .

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

  • Execute the script file in Windows as follows
    D:\StmtCacheSample\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: classes12.zip or classes12.jar or 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>\StmtCacheSample directory where Connection.properties exists and current directory to the CLASSPATH

  • Make sure that <JAVA_HOME>/bin is in the path.
    Note :If JDK 1.4 is used, then ojdbc14.jar(downloadable from OTN) must be used. For all other JDK versions, classes12.jar must be used.
  • From the directory <SAMPLE_HOME>\StmtCacheSample\src\oracle\otnsamples\oracle9ijdbc\stmtcache, compile all the java files using javac:
    Example:
     
       javac -d . *.java

  • Run the class file using java
    Example:

    java oracle.otnsamples.oracle9ijdbc.stmtcache.StmtCacheSample

Description of Sample Files 

Back To Top

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

Directory
Files
Description
StmtCacheSample StmtCacheSample.jws The Oracle9i JDeveloper workspace file
StmtCacheSample.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
StmtCacheSample\src\oracle\otnsamples\oracle9ijdbc\stmtcache StmtCacheSample.java The Source file for the Sample
StmtCacheFrame.java The Source file for the sample User Interface
BarChart.java Utility to draw horizontal Bar Graphs. 
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