Sample demonstrating usage of Connection Caching feature in Oracle9i JDBC Drivers

Table Of Contents 

Overview of the Sample Application 

Back To Top

This sample illustrates the usage of Connection caching features in Oracle9i JDBC drivers. This sample uses the JDBC-Thin driver; the code is the same for JDBC-OCI, except for the database URL syntax.

What is Connection Caching  ?

            Connection caching, generally implemented in a middle tier, is a means of keeping and using caches of physical database connections. Oracle's implementation of Connection Caching uses the Connection Pooling Framework in much of its operations.

            Each connection cache is represented by an instance of a connection cache class and has an associated group of pooled connection instances. For a single connection cache instance, the associated pooled connection instances must all represent physical connections to the same database and schema.

            Pooled connection instances are created as needed, which is whenever a connection is requested and the connection cache does not have any free pooled connection instances.

            Oracle's connection caching framework supports three connection cache schemes.

  • Dynamic Scheme : In this default scheme, you can create new pooled connections above and beyond the maximum limit, but each one is automatically closed and freed as soon as the logical connection instance that it provided is no longer in use.
  • Fixed with no wait : In this scheme, the maximum limit cannot be exceeded. Requests for connections when the maximum has already been reached will return null.
  • Fixed wait : Same as the "fixed with no wait" scheme except that a request for a new connection will wait if the limit for the number of connections has been reached.


  • Users need to use these schemes in situations where
  • The application has requested a connection
  • All existing pooled connections are in use
  • The maximum number of pooled connections in the cache have been reached.

This Sample illustrates the DYNAMIC_SCHEME connection cache scheme.

Sample Application Scenario

             Consider that a fictious organization has internet enabled all its applications. Sales and marketing employees of this organization use web-based Order Entry application to monitor various Orders and order status for various products .

             This simplified Order Entry application connects to the database and fetches the product catalog. All the products are shown to the user. User can choose a particular product and view orders for the product.

              As the organization is huge, it is likely that lot of sales employees use the Order Entry Application at the same time.

              As this Order Entry application fetches the data from the database, the cost of using a dedicated physical database connection per user is quite high. To reduce this overhead, this application makes use of Connection Cache. This Sample shows how to set up Connection Cache, get a Connection from the cache and return the connection to the cache.

private void initializeConnectionCache() {
.......................

// Initialize Connection Cache
ocacheimpl = new OracleConnectionCacheImpl(cpds);
.......................

// Set Max Limit for the Cache
ocacheimpl.setMaxLimit(5);

// Set Min Limit for the Cache
ocacheimpl.setMinLimit(1);

// Set Caching Scheme as DYNAMIC_SCHEME
// This scheme means that after reaching the connection active size to 5,
// the next request for Connection will be served by creating a new pooled
// connection instance and closed automatically when no longer in use.
ocacheimpl.setCacheScheme(OracleConnectionCacheImpl.DYNAMIC_SCHEME);
......................
......................

 

Notations used

This following notations are used through out this document

Notation

Description

<OC4J_HOME>

Folder where OC4J is installed.

<SAMPLE_HOME>

Folder where the ConnCacheSample will be unzipped.

<JAVA_HOME>

Folder where JAVA is installed.

<ANT_HOME>

Folder where the ANT is installed.


Required Software

Back To Top

  • Oracle9i JDeveloper ( Note: Oracle9i JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here )
    or
    ANT tool for building the Enterprise Application aRchive (EAR) file( ANT Version 1.5 or above can be downloaded from here ) and JDK1.2.x or above(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 ConnCacheSample.jar using the following command 

  • > jar xvf ConnCacheSample.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 ConnCacheSample with all the source files.

  • Edit ConnCacheSample/config/Connection.properties file in your favorite editor. Change the HostName, Port, SID, UserName and Password to connect to your own database.

HostName

=

localhost

SID

=

ORCL

Port

=

1521

UserName

=

scott

Password

=

tiger

Deploying and Running the application using Oracle9i JDeveloper

Back To Top

This section describes the steps required in deploying and running this application inside embedded OC4J using Oracle9i JDeveloper.

  • Open Oracle9i JDeveloper and use File/Open option to select the ConnCacheSample.jws from the ConnCacheSample directory.
  • Next, select Project/Make ConnCacheSample.jpr from main menu.
  • Now, select Run/Run ConnCacheSample.jpr from main menu which opens up the browser and runs the ConnCacheSample.

Deploying and Running the application - Using ANT script

Back To Top

          This section describes the steps required in deploying this application to the Standalone OC4J using ANT Tool and running it using browser.

    • Make sure that the environment variables[<JAVA_HOME>, <ANT_HOME>/bin in the PATH;, <JAVA_HOME>/bin in the PATH] have been set before proceeding futher. For more information on how to setup these environment variables, please refer environment set up readme document.

    • Ensure that OC4J is up and running. To start the OC4J server, navigate to <OC4J_HOME>/j2ee/home and execute the following command
      java -jar oc4j.jar

    • Build and deploy the EAR file using ANT. From ConnCacheSample directory, execute the ant
      <SAMPLE_HOME>\ConnCacheSample\ant

      This will ask for various parameters at the start such as <OC4J_HOME>, host address of the machine where the OC4J server is running, port in which the OC4J listens for ormi requests[default value is 23791], admin user name[default value is admin ] and admin password. Upon successful execution, this script will create the ConnCacheSample.ear file and deploy it to the standalone OC4J server.
    • Open your favorite browser and access the sample, using the following URL.
          http://hostName:port/ConnCacheSample/Login.jsp

      where, <host_name> is the machine on which OC4J is running and <port> is Port in which the OC4J server listens to HTTP requests. By default OC4J listens for HTTP requests in port # 8888.

Description of Sample Files 

Back To Top

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


Directory
Files
Description

ConnCacheSample

ConnCacheSample.jws

The Oracle9i JDeveloper workspace file

ConnCacheSample.jpr

The Oracle9i JDeveloper project file

build.xml

Ant build script.

ConnCacheSample\src\oracle\otnsamples\oracle9ijdbc\conncachesample

ConnCacheBean.java

Java file implementing necessary business logic and using Connection Caching Feature.

ConnCacheSample\config

application.xml

This file is used to define the J2EE EAR file, which contains the location of EJB JAR file, Web application WAR file and is included in the EAR file

Connection.properties

This file has the details of the database connection parameters

ConnCacheSample\web\WEB-INF

web.xml

This file is used to define the Web deployment descriptors and is included in the WAR file



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