The Connection caching functions within the Order Entry application
are implemented within several files, which are available in the ConnectionCacheBean.java
file in the <Sample>\src\oracle\otnsamples\jdbc folder. The ProductInfoBean class is used by all the JSPs to perform database interactions.
This ProductInfoBean class uses JDBC to perform any DML/DDL operations. Since
JSPs execute in middle tier, getting an individual database connection everytime
for every user is an expensive operation, especially when the number of users
involved is large in number. With the usage of Connection Caching, the overhead
of instantiating a new physical database connection is easily overcome - the
OracleConnectionCacheManager APIs are used to create, manage, and maintain a
connection cache in an efficient way.
Connection Cache is initialized in the constructor of the
ConnectionCache Bean. Now we'll look at some code snippets from this bean, which
will explain how to create and configure connection cache. These code snippets
will also help put into perspective how OTN developers implemented each of these
functions in the Order Entry Sample Application, and also further your conceptual
understanding on the topic areas that we learnt in the Concepts section of this
tutorial.
The following configureDataSource
method shows the configuration of OracleDataSource that is associated with the
cache. It configures the Datasource with appropriate values of Host Name, User
Name, Password etc. Note that the configuration parameters are stored in the
Connection.properties file:
private void configDSConnection() {
/* Load the properties file to get the connection information * from the Connection.properties file */ Properties prop = this.loadParams("Connection");
ods.setServerName(prop.getProperty("HostName")); /* Set Host name */ ods.setServiceName(prop.getProperty("SID")); /* Set Database SID */ ods.setPortNumber(new Integer(prop.getProperty("Port")).intValue()); /* Set Port number */ ods.setDriverType("thin"); /* Set Driver type */ ods.setUser(prop.getProperty("UserName")); /* Set User name */ ods.setPassword(prop.getProperty("Password")); /* Set Password */
}
The method initializeConnectionCacheDataSrc
enables the connection caching and also sets the cache name which uniquely identifies
the connection cache. It initializes the variable 'ods' with value of valid
Connection Cache Data Source:
private void initializeConnectionCacheDataSrc()throws Exception {
ods = new OracleDataSource(); /* Initialize the Datasource */
/* Configure the Datasource with proper values of
* Host Name, Sid, Port, Driver type, User Name and Password
*/
this.configureDataSource(ods);
ods.setConnectionCachingEnabled(true); /* Enable caching */
ods.setConnectionCacheName(CACHE_NAME); /* Set the cache name */
}
The following code snippet demonstrates how a connection cache
is created using OracleDataSource.
The createCache method in the
OracleConnectionCacheManager
class creates the cache by taking the OracleDataSource
cache name and the Properties objects. It may also be noted that the properties
of the cache are set while creating the connection cache:
The Properties object is used to set the connection
caching properties like minimum cache limit, maximum cache limit etc.:
private void initializeConnectionCache() throws Exception {
..............
this.initializeConnectionCacheDataSrc();
/* Initialize the Connection Cache */
connMgr =
OracleConnectionCacheManager.getConnectionCacheManagerInstance();
/* This object holds the properties of a cache and is passed to the
* ConnectionCacheManager while creating the cache.
*/
Properties properties = new Properties();
properties.setProperty("MinLimit", "1"); /*Set Min Limit for the Cache*/
properties.setProperty("MaxLimit", "15"); /* Set Max Limit for the Cache */
properties.setProperty("InitialLimit", "10"); /* Set the Initial Limit */
/* Create the cache by passing the cache name, data source and the
* cache properties
*/
connMgr.createCache(CACHE_NAME, ods, properties);
}