/*
Java Programming with Oracle JDBC
by Donald Bales
ISBN: 059600088X
Publisher: O'Reilly
*/
import oracle.jdbc.pool.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.ConnectionPoolDataSource;
public class OCCIConnectionServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("Oracle Cached Connection "
+ "Implementation Test Servlet ");
out.println("");
out.println("");
// let's turn on verbose output
OCCIConnection.setVerbose(true);
// now let's get a cached connection
Connection connection = OCCIConnection.checkOut();
Statement statement = null;
ResultSet resultSet = null;
String userName = null;
try {
// test the connection
statement = connection.createStatement();
resultSet = statement
.executeQuery("select initcap(user) from sys.dual");
if (resultSet.next())
userName = resultSet.getString(1);
} catch (SQLException e) {
out.println("DedicatedConnection.doGet() SQLException: "
+ e.getMessage() + "");
} finally {
if (resultSet != null)
try {
resultSet.close();
} catch (SQLException ignore) {
}
if (statement != null)
try {
statement.close();
} catch (SQLException ignore) {
}
}
// let's add a little delay so we can force
// multiple connections in the connection cache
for (int o = 0; o < 3; o++) {
for (int i = 0; i < 2147483647; i++) {
}
}
// let's return the conection
OCCIConnection.checkIn(connection);
out.println("Hello " + userName + "!
");
out.println("You're using an Oracle Cached "
+ "Connection Implementation connection!
");
out.println("");
out.println("");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}
class OCCIConnection {
private static boolean verbose = false;
private static int numberImplementations = 0;
private static Vector cachedImplementations = new Vector();
public static synchronized Connection checkOut() {
return checkOut("Database");
}
public static synchronized Connection checkOut(String baseName) {
boolean found = false;
OracleConnectionCacheImpl cached = null;
Connection connection = null;
if (verbose) {
System.out.println("There are "
+ Integer.toString(numberImplementations)
+ " connections in the cache");
System.out.println("Searching for a matching implementation...");
}
for (int i = 0; !found && i < numberImplementations; i++) {
if (verbose) {
System.out.println("Vector entry " + Integer.toString(i));
}
cached = (OracleConnectionCacheImpl) cachedImplementations.get(i);
if (cached.getDescription().equals(baseName)) {
if (verbose) {
System.out.println("found cached entry "
+ Integer.toString(i) + " for " + baseName);
}
found = true;
}
}
if (!found) {
if (verbose) {
System.out.println("Cached entry not found ");
System.out.println("Allocating new entry for " + baseName);
}
try {
cached = new OracleConnectionCacheImpl(
getConnectionPoolDataSource(baseName));
cached.setDescription(baseName);
cachedImplementations.add(cached);
numberImplementations++;
} catch (SQLException e) {
System.err.println(e.getMessage()
+ " creating a new implementation for " + baseName);
}
}
if (cached != null) {
try {
connection = cached.getConnection();
} catch (SQLException e) {
System.err.println(e.getMessage() + " getting connection for "
+ baseName);
}
}
return connection;
}
public static ConnectionPoolDataSource getConnectionPoolDataSource(
String baseName) {
Context context = null;
ConnectionPoolDataSource cpds = null;
try {
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
properties.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC");
context = new InitialContext(properties);
cpds = (ConnectionPoolDataSource) context.lookup(baseName);
} catch (NamingException e) {
System.err.println(e.getMessage() + " creating JNDI context for "
+ baseName);
}
return cpds;
}
protected static synchronized void checkIn(Connection c) {
try {
c.close();
} catch (SQLException e) {
System.err.println(e.getMessage() + " closing connection");
}
}
public static String[] getReport() {
int line = 0;
String[] lines = new String[numberImplementations * 7];
OracleConnectionCacheImpl cached = null;
for (int i = 0; i < numberImplementations; i++) {
cached = (OracleConnectionCacheImpl) cachedImplementations.get(i);
lines[line++] = cached.getDescription() + ":";
switch (cached.getCacheScheme()) {
case OracleConnectionCacheImpl.DYNAMIC_SCHEME:
lines[line++] = "Cache Scheme = DYNAMIC_SCHEME";
break;
case OracleConnectionCacheImpl.FIXED_RETURN_NULL_SCHEME:
lines[line++] = "Cache Scheme = FIXED_RETURN_NULL_SCHEME";
break;
case OracleConnectionCacheImpl.FIXED_WAIT_SCHEME:
lines[line++] = "Cache Scheme = FIXED_WAIT_SCHEME";
break;
}
lines[line++] = "Minimum Limit = "
+ Integer.toString(cached.getMinLimit());
lines[line++] = "Maximum Limit = "
+ Integer.toString(cached.getMaxLimit());
lines[line++] = "Cache Size = "
+ Integer.toString(cached.getCacheSize());
lines[line++] = "Active Size = "
+ Integer.toString(cached.getActiveSize());
lines[line++] = " ";
}
return lines;
}
public static void setVerbose(boolean v) {
verbose = v;
}
}