📄 understandcacheattributes.java
字号:
/**
* @author Rajat Gupta
* @version 1.0
*
* Development Environment : Oracle JDeveloper 10g
* Name of the Application : UnderstandCacheAttributes.java
* Creation/Modification History :
*
* Rajat 25-Aug-2004 Created
*
* Overview of Application : This is the main class of the sample application.
* To understand all the various possibilities
* of Connection Attributes, run this class with
* appropriate parameters.
*
**/
package oracle.otnsamples.connectioncache.attributes;
// Import Packages
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;
public class UnderstandCacheAttributes {
/**
* Initialize Oracle DataSource
*/
OracleDataSource ods;
/**
* Empty Constructor
*/
public UnderstandCacheAttributes(){
}
/**
* This is the main method of the class. It invokes various
* methods within this class based on the passed arguments.
*
* @param args Passed Arguments
*/
public static void main(String[] args){
// Instantiate Class
UnderstandCacheAttributes cacheAttributes = new UnderstandCacheAttributes();
try{
// Instantiate Oracle DataSource
cacheAttributes.ods = new OracleDataSource();
if (args.length == 0)
{
// If no arguments are passed, then print the list of operations
System.out.println("\nPlease run the class with an argument. ConnectionCache"
+ " will be set for all operations with default attributes.");
System.out.println("\n1. To retrieve the connection with default "
+ "attributes, run with argument 'default'.");
System.out.println("\n2. To retrieve the connection with new attributes,"
+ " run with argument 'new'.");
System.out.println("\n3. To retrieve the connection with default"
+ " attributes and update it, run with argument 'update'.");
System.out.println("\n4. To retrieve the connection with matching "
+ "attributes, run with argument 'matching'.");
System.out.println("\n5. To clear the connection of all attributes, "
+ "run with argument 'clear'.");
}else{
// Read the parameter
String runMethod = args[0];
// Invoking various methods to demonstrate the features, functionality
// and usage of Oracle Connection Cache Attributes
if (runMethod.equalsIgnoreCase("default")){
// Initialize the Connection Cache and its attributes
cacheAttributes.setConnectionCacheAttributes();
System.out.println();
System.out.println("----> We will now retrieve this connection. The "
+ "following method does this");
cacheAttributes.getConnectionWithDefaultSetAttributes();
System.out.println();
System.out.println("----> CONCLUSION : We have retrieved the same connection");
}else if (runMethod.equalsIgnoreCase("new")){
// Initialize the Connection Cache and its attributes
cacheAttributes.setConnectionCacheAttributes();
System.out.println();
System.out.println("----> Lets try to retrieve the connection with a "
+ "new set of Attributes");
cacheAttributes.getConnectionWithNewAttributes();
System.out.println();
System.out.println("----> CONCLUSION : There is not matching connection "
+ "in the cache. So, we have got a new connection.");
}else if (runMethod.equalsIgnoreCase("update")){
// Initialize the Connection Cache and its attributes
cacheAttributes.setConnectionCacheAttributes();
System.out.println();
System.out.println("----> Let us now update the connection attributes "
+ "and use it");
cacheAttributes.getConnectionAndUpdateAttributes();
}else if (runMethod.equalsIgnoreCase("matching")){
// Initialize the Connection Cache and its attributes
cacheAttributes.setConnectionCacheAttributes();
System.out.println();
System.out.println("----> The following method tries to retrieve the "
+ "connection with a matching set of attributes");
cacheAttributes.getConnectionWithMatchingAttributes();
System.out.println();
System.out.println("----> CONCLUSION : As there is no connection with "
+ "exact set of attributes in the cache, we have retrieved a matching "
+ "connection which in this case happens to be OTN_CONNECTION. If "
+ "there was a connection in the cache with an exact match, then "
+ "that connection would be retrieved.");
}else if (runMethod.equalsIgnoreCase("clear")){
// Initialize the Connection Cache and its attributes
cacheAttributes.setConnectionCacheAttributes();
System.out.println();
System.out.println("----> The following method clears the connection "
+ "of all its attributes");
cacheAttributes.clearConnectionAttributes();
System.out.println();
System.out.println("----> Lets see if the Connection Attributes have "
+ "been cleared");
cacheAttributes.getConnectionWithDefaultSetAttributes();
System.out.println();
System.out.println("----> CONCLUSION : There is not matching connection in the cache");
}else{
}
}
}catch(Exception e){
// Catch and print in case of an exception
e.printStackTrace();
}
}
/**
* This method initializes the Oracle DataSource and Connection Cache.
* The default connection attributes are also set within this method.
*
* @exception Exception In Case of an Exception
*/
private void setConnectionCacheAttributes() throws Exception{
System.out.println("\n------------- Invoking method "
+ "setConnectionCacheAttributes() ----------------");
System.out.println("-- Set up a new Connection");
// Set DataSource properties
ods.setUser(ConnectionParams.userName);
ods.setPassword(ConnectionParams.password);
ods.setServerName(ConnectionParams.serverName);
ods.setDatabaseName(ConnectionParams.sid);
ods.setPortNumber(ConnectionParams.portNo);
ods.setDriverType(ConnectionParams.driverType);
// Enable Connection Cache
ods.setConnectionCachingEnabled(true);
// Enter a name for Connection Cache
ods.setConnectionCacheName("MyCache");
// This is a connection cache parameter. This specifies that if there
// is not an exact match of the connection attributes, even then retrieve
// a matching connection. This value, by default, is false.
// For more information on this feature and attribute, please read the
// accompanied Readme file with this sample application.
Properties cacheProps = new Properties();
cacheProps.put("ClosestConnectionMatch", "true");
ods.setConnectionCacheProperties(cacheProps);
// Get a Connection from the Oracle DataSource
OracleConnection conn = (OracleConnection)ods.getConnection();
System.out.println("-- Connection Retrieved. Executing Sample Query in "
+ "the database");
// Execute a Sample Query in the database to demonstrate that the
// connection is established
this.executeQuery(conn);
System.out.println();
System.out.println("-- Initializing Connection Attributes. This set includes");
System.out.println("-- CONNECTION_NAME = OTN_CONNECTION");
System.out.println("-- NLS_LANG = AMERICAN_AMERICA.WE8ISO8859P1");
System.out.println();
System.out.println("----> We will call it Default Set Attributes");
// Initialize default Connection Attributes
java.util.Properties connAttr = new Properties();
connAttr.setProperty("CONNECTION_NAME", "OTN_CONNECTION");
connAttr.setProperty("NLS_LANG", "AMERICAN_AMERICA.WE8ISO8859P1");
System.out.println();
System.out.println("-- Setting these Attributes And Closing Connection");
System.out.println();
// Apply the attributes to the connection and release the connection back
// to the MyCache
conn.close(connAttr);
}
/**
* This method retrieves the connection after passing default
* attributes. The attributes are then listed on the console.
*
* @exception Exception In Case of an Exception
*/
private void getConnectionWithDefaultSetAttributes() throws Exception{
System.out.println("\n----- Invoking method "
+ "getConnectionWithDefaultSetAttributes() ---------------");
System.out.println("-- Initializing Default Set Properties");
// Initialize default Connection Attributes
java.util.Properties connAttr = new Properties();
connAttr.setProperty("CONNECTION_NAME", "OTN_CONNECTION");
connAttr.setProperty("NLS_LANG", "AMERICAN_AMERICA.WE8ISO8859P1");
System.out.println("-- Retrieving Connection with Default Set Attributes");
// Get a Connection from the Oracle DataSource
OracleConnection conn = (OracleConnection)ods.getConnection(connAttr);
System.out.println();
System.out.println("-- Connection Retrieved. Executing Sample Query in "
+ "the database");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -