Sample demonstrating
usage of Connection Caching feature in Oracle9i
JDBC Drivers
Table Of Contents
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);
......................
......................
|
|
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.
|
- 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.
-
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.
|