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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | JDBCStatementCache.BoundedLinkedHashMap
A |
| Modifier and Type | Field and Description |
|---|---|
| private final LinkedHashMap | statements
Structure holding the cached prepared statement objects. |
| Access | Constructor and Description |
|---|---|
| public | JDBCStatementCache(int
maximum number of statements in the cache maxSize)Creates a new, empty JDBC statement cache. |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized boolean | Returns: true if added to the cache, false if
not.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.key for the prepared statement to look up statementKey)Retrieves a cached prepared statement if one exists. |
| statements | back to summary |
|---|---|
| private final LinkedHashMap<StatementKey, PreparedStatement> statements Structure holding the cached prepared statement objects. | |
| JDBCStatementCache | back to summary |
|---|---|
| public JDBCStatementCache(int maxSize) Creates a new, empty JDBC statement cache.
| |
| cacheStatement | back to summary |
|---|---|
| public synchronized boolean cacheStatement(StatementKey statementKey, PreparedStatement ps) Cache the prepared statement if it does not already exist.
| |
| getCached | back to summary |
|---|---|
| public synchronized PreparedStatement getCached(StatementKey statementKey) Retrieves a cached prepared statement if one exists.
| |
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.
| Modifier and Type | Field and Description |
|---|---|
| private final int | maxSize
Maximum number of entries. |
| Access | Constructor and Description |
|---|---|
| public | BoundedLinkedHashMap(int
maximum size of the map maxCapacity)Creates a bounded |
| Modifier and Type | Method and Description |
|---|---|
| protected boolean | Returns: true if the element is to be removed,
false if not.the element picked out for removal eldestOverrides java. |
| maxSize | back to summary |
|---|---|
| private final int maxSize Maximum number of entries. | |
| BoundedLinkedHashMap | back to summary |
|---|---|
| public BoundedLinkedHashMap(int maxCapacity) Creates a bounded Iteration is by insertion-order.
| |
| removeEldestEntry | back to summary |
|---|---|
| protected boolean removeEldestEntry(Map. Overrides java. 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.
| |