org.openanzo.jdbc.utils.sqlcache
Class SQLCache

java.lang.Object
  extended by java.util.AbstractMap
      extended by org.apache.commons.collections.map.AbstractHashedMap
          extended by org.apache.commons.collections.map.AbstractLinkedMap
              extended by org.apache.commons.collections.map.LRUMap
                  extended by org.openanzo.jdbc.utils.sqlcache.SQLCache
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map, org.apache.commons.collections.BoundedMap, org.apache.commons.collections.IterableMap, org.apache.commons.collections.OrderedMap, PreparedStatementCache

public class SQLCache
extends org.apache.commons.collections.map.LRUMap
implements PreparedStatementCache

Provides a cache of JDBC statements. Statements can be accessed either as a JDBC PreparedStatement, in which case the statement is pooled, as a CallableStatement in which it may simple be desposed of after use or as a set of non-querying statements with no JDBC paramters that are executed together, such as DDL ('create table' calls, etc..). JDBC statements may be loaded from a java Properties file, a java Properties object or may be added using 'addPreparedStatement'. The key of each property is a string name for that prepared statement and the value is the SQL statement or, for non-querying statements with no JDBC paramters, a list of statements seperated ';;'. If a properties entry contains a list of statements as the value, only 'runSQLGroup' may be used to access these statements. Template variables may optionally be put in the JDBC statements using a '${varname}' syntax. These template variables should only be used to parameterize the tablenames or other values where it will be the same for a substantial number of calls. If the template variable is rarely the same value the cache will perform poorly, in which case it is better to use JDBC '?' parameters or dynamically generate the SQL and run it as a callable statement. Prepared statements should be pulled from the pool using 'getPreparedSQLStatement' and returned to the pool when they are no longer needed using 'returnPreparedSQLStatement', ideally the return call is put a finally clause to guarentee it is called.

Version:
$Revision: 154 $ on $Date: 2007-07-31 10:00:53 -0400 (Tue, 31 Jul 2007) $
Author:
Stephen Evanchik
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
SQLCache(java.sql.Connection connection)
          Create a new SQLCache for the given connection
SQLCache(java.sql.Connection connection, java.io.InputStream sqlFileInputStream)
          Create a new SQLCache for the given connection
SQLCache(java.sql.Connection connection, java.util.Properties defaults)
          Create a new SQLCache for the given connection
SQLCache(java.sql.Connection connection, java.lang.String sqlFilename)
          Create a new SQLCache for the given connection
SQLCache(java.sql.Connection connection, java.lang.String sqlFilename, java.util.Properties defaults)
          Create a new SQLCache for the given connection
 
Method Summary
 void addPreparedStatement(java.lang.String name, java.lang.String statement)
          Add a prepared statement with the given name, and text
 void clear()
           
static java.io.InputStream findSQLResource(java.lang.String sqlFilename)
          Load the contents of a file as a resource stream using SQLCache.class.getClassLoader().getClassLoader().getResourceAsStream()
 java.sql.Connection getJDBCConnection()
          Get the JDBC Connection object
 java.sql.PreparedStatement getPreparedSQLStatement(java.lang.String name)
          Retrieve a prepared statement with the given name
 java.sql.PreparedStatement getPreparedSQLStatement(java.lang.String name, java.lang.String[] templateParameters)
          Get a preparedStatement with the given name.
 ClosableIterator<java.lang.String> listPreloadData(java.io.InputStream inputStream)
          Retrieve data from file which will be used to preload some table
 ClosableIterator<java.lang.String> listPreloadData(java.lang.String sqlFilename)
          Retrieve data from file which will be used to preload some table
 void loadSQLFile(java.io.InputStream sqlFileInputStream)
          Load the contents of this file containing ddl and prepared statement text
 void loadSQLFile(java.lang.String sqlFilename)
          Load the contents of this file containing ddl and prepared statement text
 void loadSQLFile(java.lang.String sqlFilename, java.util.Properties defaults)
          Load the contents of this file containing ddl and prepared statement definitions
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
          Create a preparedStatement for the given SQL text
 java.sql.PreparedStatement prepareStatement(java.lang.String name, java.lang.String[] templateParameters)
          Create a preparedStatement with the given name
 java.lang.Object remove(java.lang.Object key)
           
 void returnPreparedSQLStatement(java.sql.PreparedStatement ps)
          Return the prepared statement to the cache
 void runSQLGroup(java.lang.String name, java.lang.String[] templateParameters)
          Execute a group of sql statements, usually used to initialize tables and indexes
 void setJDBCConnection(java.sql.Connection connection)
          Set the JDBC connection.
 void setResultSetParams(java.lang.Integer resultSetType, java.lang.Integer resultSetConcurrency, java.lang.Integer resultSetHoldability)
          Set the resultSet parameters for the connection
 
Methods inherited from class org.apache.commons.collections.map.LRUMap
clone, get, isFull, isScanUntilRemovable, maxSize
 
Methods inherited from class org.apache.commons.collections.map.AbstractLinkedMap
containsValue, firstKey, lastKey, mapIterator, nextKey, orderedMapIterator, previousKey
 
Methods inherited from class org.apache.commons.collections.map.AbstractHashedMap
containsKey, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, size, toString, values
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, size, values
 

Constructor Detail

SQLCache

public SQLCache(java.sql.Connection connection)
Create a new SQLCache for the given connection

Parameters:
connection - connection for which to cache statements

SQLCache

public SQLCache(java.sql.Connection connection,
                java.lang.String sqlFilename)
         throws RdbException
Create a new SQLCache for the given connection

Parameters:
connection - connection for which to cache statements
sqlFilename - name of file containing initialization ddl data, and prepared statement definitions
Throws:
RdbException - if there was an error loading the initialization file

SQLCache

public SQLCache(java.sql.Connection connection,
                java.io.InputStream sqlFileInputStream)
         throws RdbException
Create a new SQLCache for the given connection

Parameters:
connection - connection for which to cache statements
sqlFileInputStream - input stream containing initialization ddl data, and prepared statement definitions
Throws:
RdbException - if there was an error loading the initialization file

SQLCache

public SQLCache(java.sql.Connection connection,
                java.lang.String sqlFilename,
                java.util.Properties defaults)
         throws RdbException
Create a new SQLCache for the given connection

Parameters:
connection - connection for which to cache statements
sqlFilename - name of file containing initialization ddl data, and prepared statement definitions
defaults - default text for ddl operations and prepared statement definitions
Throws:
RdbException - if there was an error loading the initialization file

SQLCache

public SQLCache(java.sql.Connection connection,
                java.util.Properties defaults)
Create a new SQLCache for the given connection

Parameters:
connection - connection for which to cache statements
defaults - default text for ddl operations and prepared statement definitions
Method Detail

setResultSetParams

public void setResultSetParams(java.lang.Integer resultSetType,
                               java.lang.Integer resultSetConcurrency,
                               java.lang.Integer resultSetHoldability)
Set the resultSet parameters for the connection

Parameters:
resultSetType - resultSetType
resultSetConcurrency - type of concurrency to use
resultSetHoldability - result set holdability setting

loadSQLFile

public void loadSQLFile(java.lang.String sqlFilename)
                 throws RdbException
Load the contents of this file containing ddl and prepared statement text

Parameters:
sqlFilename - file containing ddl and prepared statement text
Throws:
RdbException

loadSQLFile

public void loadSQLFile(java.io.InputStream sqlFileInputStream)
                 throws RdbException
Load the contents of this file containing ddl and prepared statement text

Parameters:
sqlFileInputStream - input stream containing ddl and prepared statement text
Throws:
RdbException

loadSQLFile

public void loadSQLFile(java.lang.String sqlFilename,
                        java.util.Properties defaults)
                 throws RdbException
Load the contents of this file containing ddl and prepared statement definitions

Parameters:
sqlFilename - name of file containing initialization ddl data, and prepared statement definitions
defaults - default text for ddl operations and prepared statement definitions
Throws:
RdbException

findSQLResource

public static java.io.InputStream findSQLResource(java.lang.String sqlFilename)
                                           throws RdbException
Load the contents of a file as a resource stream using SQLCache.class.getClassLoader().getClassLoader().getResourceAsStream()

Parameters:
sqlFilename - name of file containing initialization ddl data, and prepared statement definitions
Returns:
inputStream containing data from file
Throws:
RdbException

getJDBCConnection

public java.sql.Connection getJDBCConnection()
Get the JDBC Connection object

Returns:
The JDBC Connection object

setJDBCConnection

public void setJDBCConnection(java.sql.Connection connection)
Set the JDBC connection.

Parameters:
connection -

addPreparedStatement

public void addPreparedStatement(java.lang.String name,
                                 java.lang.String statement)
Add a prepared statement with the given name, and text

Parameters:
name - name of statement
statement - definition of prepared statement

runSQLGroup

public void runSQLGroup(java.lang.String name,
                        java.lang.String[] templateParameters)
                 throws java.sql.SQLException
Execute a group of sql statements, usually used to initialize tables and indexes

Parameters:
name - name of group
templateParameters - arguments to the statement templates
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
                                            throws java.sql.SQLException
Create a preparedStatement for the given SQL text

Specified by:
prepareStatement in interface PreparedStatementCache
Parameters:
sql - text of statement
Returns:
a preparedStatement for the given sql
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String name,
                                                   java.lang.String[] templateParameters)
                                            throws java.sql.SQLException
Create a preparedStatement with the given name

Parameters:
name - name of prepared statement
templateParameters - parameters to the statement template
Returns:
Prepared statement for the given name and parameters
Throws:
java.sql.SQLException

getPreparedSQLStatement

public java.sql.PreparedStatement getPreparedSQLStatement(java.lang.String name,
                                                          java.lang.String[] templateParameters)
                                                   throws java.sql.SQLException
Get a preparedStatement with the given name. Use cached version if available, or create a new version.

Specified by:
getPreparedSQLStatement in interface PreparedStatementCache
Parameters:
name - name of prepared statement
templateParameters - parameters to the statement template
Returns:
Prepared statement for the given name and parameters
Throws:
java.sql.SQLException

getPreparedSQLStatement

public java.sql.PreparedStatement getPreparedSQLStatement(java.lang.String name)
                                                   throws java.sql.SQLException
Description copied from interface: PreparedStatementCache
Retrieve a prepared statement with the given name

Specified by:
getPreparedSQLStatement in interface PreparedStatementCache
Parameters:
name - name of the prepared statement
Returns:
prepared statement with given name
Throws:
java.sql.SQLException

returnPreparedSQLStatement

public void returnPreparedSQLStatement(java.sql.PreparedStatement ps)
Description copied from interface: PreparedStatementCache
Return the prepared statement to the cache

Specified by:
returnPreparedSQLStatement in interface PreparedStatementCache
Parameters:
ps - prepared statement to return

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map
Overrides:
remove in class org.apache.commons.collections.map.AbstractHashedMap

listPreloadData

public ClosableIterator<java.lang.String> listPreloadData(java.lang.String sqlFilename)
                                                   throws RdbException
Retrieve data from file which will be used to preload some table

Parameters:
sqlFilename - name of file containing data
Returns:
Iterator over the lines of text in the file
Throws:
RdbException

listPreloadData

public ClosableIterator<java.lang.String> listPreloadData(java.io.InputStream inputStream)
Retrieve data from file which will be used to preload some table

Parameters:
inputStream - inputStream containing data
Returns:
Iterator over the lines of text in the file

clear

public void clear()
Specified by:
clear in interface java.util.Map
Overrides:
clear in class org.apache.commons.collections.map.AbstractLinkedMap


Copyright © 2007 Cambridge Semantics Inc.. All Rights Reserved.