Package net.jextra.fauxjo
Class StatementCache
- java.lang.Object
-
- net.jextra.fauxjo.StatementCache
-
public class StatementCache extends Object
Caches PreparedStatements and CallableStatements per Connection, local to the Thread.If using a modern jdbc driver that caches Statements (PG, Oracle, or MySQL), consider disabling this via its Home object, drivers cache Statements better. Bottom-up, the containment hierarchy is as follows:
- A StmtCache (StatementCache.PerConnectionCache) is an LRU cache storing PreparedStatement or CallableStatements by their sql.
- A StmtCacheMap is created if not exists from calls to prepareStatement or prepareCall and holds StmtCaches by their Connection key.
- StmtCacheMap is a type of the static ThreadLocal created at startup so each thread gets its own StmtCacheMap.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classStatementCache.Configstatic interfaceStatementCache.ProxyCloserstatic classStatementCache.WrappedPreparedStatementBlock from actually doing close.static classStatementCache.WrappedResultSetOverride getStatement to return proxied Statement vs real Statement.
-
Constructor Summary
Constructors Constructor Description StatementCache()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(StatementCacheListener listener)booleanclear()Closes and removes all Listeners, PreparedStatements and PreparedCalls for the active Thread.booleanclear(Connection conn)StatementCache.ConfiggetConfig()static LonggetConnKey(Connection conn)Return the Long key to be used for caching the Stmt.voidgetDiagnosticCsv(Connection conn, StringBuilder sb)If connection is cached, append its contents to the string builder as csv rows.StatementCacheListener[]getListeners()longgetPerConCache_MaxEntries()Return max Statements (unique sql) allowed in the cache (default is 1000) before evicting the LRU.longgetPerConCacheMaxTtl()Return maximum time for a Statement to live in the Connection cache (default is 30 minutes).voidgetStats(StringBuilder strBldrToAppend, DateTimeFormatter optionalDtFormat)Append stats as json params to strBldrToAppend.CallableStatementprepareCall(Connection conn, String sql)CallableStatementprepareCall(Connection conn, String sql, int resultSetType, int resultSetConcurrenc)CallableStatementprepareCall(Connection conn, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)PreparedStatementprepareStatement(Connection conn, String sql, boolean supportsGeneratedKeys)PreparedStatementprepareStatement(Connection conn, String sql, int autoGeneratedKeys)PreparedStatementprepareStatement(Connection conn, String sql, int[] columnIndexes)PreparedStatementprepareStatement(Connection conn, String sql, int resultSetType, int resultSetConcurrency)PreparedStatementprepareStatement(Connection conn, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)PreparedStatementprepareStatement(Connection conn, String sql, String[] columnNames)voidremoveAllListeners()booleanremoveListener(StatementCacheListener listener)voidsetPerConCache_Maximums(Integer perConCacheMaxEntries, Long perConCacheMaxTtl)Set perConCache maximums.voidsetPerConCacheMaxEntries(Long perConCacheMaxEntries)voidsetPerConCacheMaxTtl(Long perConCacheMaxTtl)
-
-
-
Method Detail
-
getConfig
public StatementCache.Config getConfig()
-
getPerConCacheMaxTtl
public long getPerConCacheMaxTtl()
Return maximum time for a Statement to live in the Connection cache (default is 30 minutes). Guard against Statements that concatenate instead of use params.
-
setPerConCacheMaxTtl
public void setPerConCacheMaxTtl(Long perConCacheMaxTtl)
-
getPerConCache_MaxEntries
public long getPerConCache_MaxEntries()
Return max Statements (unique sql) allowed in the cache (default is 1000) before evicting the LRU. Guards against Statements concatenating instead of using params.
-
setPerConCacheMaxEntries
public void setPerConCacheMaxEntries(Long perConCacheMaxEntries)
-
setPerConCache_Maximums
public void setPerConCache_Maximums(Integer perConCacheMaxEntries, Long perConCacheMaxTtl)
Set perConCache maximums.- Parameters:
perConCacheMaxEntries- stmt max Statements before evict LRU (default is 1000)perConCacheMaxTtl- stmt max time to live before evict LRU (default is 30 minutes)
-
prepareStatement
public PreparedStatement prepareStatement(Connection conn, String sql, boolean supportsGeneratedKeys) throws SQLException
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(Connection conn, String sql, int resultSetType, int resultSetConcurrency) throws SQLException
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(Connection conn, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(Connection conn, String sql, int autoGeneratedKeys) throws SQLException
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(Connection conn, String sql, int[] columnIndexes) throws SQLException
- Throws:
SQLException
-
prepareStatement
public PreparedStatement prepareStatement(Connection conn, String sql, String[] columnNames) throws SQLException
- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(Connection conn, String sql) throws SQLException
- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(Connection conn, String sql, int resultSetType, int resultSetConcurrenc) throws SQLException
- Throws:
SQLException
-
prepareCall
public CallableStatement prepareCall(Connection conn, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException
- Throws:
SQLException
-
clear
public boolean clear(Connection conn) throws SQLException
- Throws:
SQLException
-
clear
public boolean clear() throws SQLExceptionCloses and removes all Listeners, PreparedStatements and PreparedCalls for the active Thread.- Returns:
- true if any actual work was done and false if there was nothing to remove (most likely already closed).
- Throws:
SQLException
-
addListener
public void addListener(StatementCacheListener listener)
-
removeListener
public boolean removeListener(StatementCacheListener listener)
-
removeAllListeners
public void removeAllListeners()
-
getListeners
public StatementCacheListener[] getListeners()
-
getStats
public void getStats(StringBuilder strBldrToAppend, DateTimeFormatter optionalDtFormat) throws SQLException
Append stats as json params to strBldrToAppend.- Throws:
SQLException
-
getDiagnosticCsv
public void getDiagnosticCsv(Connection conn, StringBuilder sb) throws Exception
If connection is cached, append its contents to the string builder as csv rows.- Parameters:
conn- represented by the cachesb- will have cache entries appended- Throws:
Exception
-
getConnKey
public static Long getConnKey(Connection conn) throws SQLException
Return the Long key to be used for caching the Stmt.conn.getMetaData().getConnection().hashCode() is used in case conn is a new pooled wrapper connection. Using an always-new wrapped connection from a pool like Hikari as a cache key will leak memory.
- Parameters:
conn- used to create its cache key- Throws:
SQLException
-
-