Top Description Inners Fields Constructors Methods
org.apache.derby.client.am.stmtcache

public final Class JDBCStatementCache

extends Object
Class Inheritance
Imports
java.sql.PreparedStatement, .SQLException, java.util.LinkedHashMap, .Map, org.apache.derby.shared.common.sanity.SanityManager

A cache for JDBC statement objects.

The entries in the cache contains objects implementing the java.sql.PreparedStatement interface, and they are inserted with a key object implementing the interface StatementKey. The cached objects can be either java.sql.PreparedStatement or java.sql.CallableStatement. These two should be separated by using different types of keys.

The cache only contains free statement objects, and on a successful request for a cached statement the statement is removed from the cache. The cache is not intended to hold duplicate statements. The physical prepared statement should be (re-)inserted into the cache when close is called on the logical prepared statement using it.

There is a maximum number of cached statements associated with the cache. If this number is exceeded, the oldest entry will be thrown out. One can always throw out an entry, because the fact that it is in the cache means it is free and not in use.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
JDBCStatementCache.BoundedLinkedHashMap

A LinkedHashMap with an upper bound on the number of entries.

Field Summary

Modifier and TypeField and Description
private final LinkedHashMap<StatementKey, PreparedStatement>
statements

Structure holding the cached prepared statement objects.

Constructor Summary

AccessConstructor and Description
public
JDBCStatementCache(int
maximum number of statements in the cache
maxSize
)

Creates a new, empty JDBC statement cache.

Method Summary

Modifier and TypeMethod and Description
public synchronized boolean

Returns:

true if added to the cache, false if not.
cacheStatement
(StatementKey
key to insert prepared statement with
statementKey
,
PreparedStatement
prepared statement to cache
ps
)

Cache the prepared statement if it does not already exist.

public synchronized PreparedStatement

Returns:

A cached statement if one exists, null otherwise.
getCached
(StatementKey
key for the prepared statement to look up
statementKey
)

Retrieves a cached prepared statement if one exists.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

statementsback to summary
private final LinkedHashMap<StatementKey, PreparedStatement> statements

Structure holding the cached prepared statement objects.

Constructor Detail

JDBCStatementCacheback to summary
public JDBCStatementCache(int maxSize)

Creates a new, empty JDBC statement cache.

Parameters
maxSize:int

maximum number of statements in the cache

Exceptions
IllegalArgumentException:
if maxSize is less than one

Method Detail

cacheStatementback to summary
public synchronized boolean cacheStatement(StatementKey statementKey, PreparedStatement ps)

Cache the prepared statement if it does not already exist.

Parameters
statementKey:StatementKey

key to insert prepared statement with

ps:PreparedStatement

prepared statement to cache

Returns:boolean

true if added to the cache, false if not.

getCachedback to summary
public synchronized PreparedStatement getCached(StatementKey statementKey)

Retrieves a cached prepared statement if one exists.

Parameters
statementKey:StatementKey

key for the prepared statement to look up

Returns:PreparedStatement

A cached statement if one exists, null otherwise.

org.apache.derby.client.am.stmtcache back to summary

private Class JDBCStatementCache.BoundedLinkedHashMap

extends LinkedHashMap<StatementKey, PreparedStatement>
Class Inheritance

A LinkedHashMap with an upper bound on the number of entries.

If the maximum size is exceeded, the oldest entry is automatically removed after the new entry has been inserted.

Field Summary

Modifier and TypeField and Description
private final int
maxSize

Maximum number of entries.

Constructor Summary

AccessConstructor and Description
public
BoundedLinkedHashMap(int
maximum size of the map
maxCapacity
)

Creates a bounded LinkedHashMap with the specified maximum size.

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

true if the element is to be removed, false if not.
removeEldestEntry
(Map.Entry<StatementKey, PreparedStatement>
the element picked out for removal
eldest
)

Overrides java.util.LinkedHashMap.removeEldestEntry.

Tells if an entry should be removed from the map.
Inherited from java.util.LinkedHashMap:
clearcontainsValueentrySetforEachgetgetOrDefaultkeySetnewLinkedHashMapputFirstputLastreplaceAllreversedsequencedEntrySetsequencedKeySetsequencedValuesvalues

Field Detail

maxSizeback to summary
private final int maxSize

Maximum number of entries.

Constructor Detail

BoundedLinkedHashMapback to summary
public BoundedLinkedHashMap(int maxCapacity)

Creates a bounded LinkedHashMap with the specified maximum size.

Iteration is by insertion-order.

Parameters
maxCapacity:int

maximum size of the map

Method Detail

removeEldestEntryback to summary
protected boolean removeEldestEntry(Map.Entry<StatementKey, PreparedStatement> eldest)

Overrides java.util.LinkedHashMap.removeEldestEntry.

Tells if an entry should be removed from the map.

If the cache has exceeded its maximum size, the oldest element will be marked for removal. The oldest element will be removed after the new element has been inserted.

Parameters
eldest:Map.Entry<StatementKey, PreparedStatement>

the element picked out for removal

Returns:boolean

true if the element is to be removed, false if not.