| 1 |
package org.apache.commons.jcs3.auxiliary.disk.jdbc.hsql; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.sql.Connection; |
| 23 |
import java.sql.DatabaseMetaData; |
| 24 |
import java.sql.ResultSet; |
| 25 |
import java.sql.SQLException; |
| 26 |
import java.sql.Statement; |
| 27 |
|
| 28 |
import javax.sql.DataSource; |
| 29 |
|
| 30 |
import org.apache.commons.jcs3.auxiliary.AuxiliaryCacheAttributes; |
| 31 |
import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCache; |
| 32 |
import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCacheAttributes; |
| 33 |
import org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCacheFactory; |
| 34 |
import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager; |
| 35 |
import org.apache.commons.jcs3.engine.behavior.IElementSerializer; |
| 36 |
import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger; |
| 37 |
import org.apache.commons.jcs3.log.Log; |
| 38 |
import org.apache.commons.jcs3.log.LogManager; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class HSQLDiskCacheFactory |
| 44 |
extends JDBCDiskCacheFactory |
| 45 |
{ |
| 46 |
|
| 47 |
private static final Log log = LogManager.getLog( HSQLDiskCacheFactory.class ); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@Override |
| 60 |
public <K, V> JDBCDiskCache<K, V> createCache( final AuxiliaryCacheAttributes rawAttr, |
| 61 |
final ICompositeCacheManager compositeCacheManager, |
| 62 |
final ICacheEventLogger cacheEventLogger, |
| 63 |
final IElementSerializer elementSerializer ) |
| 64 |
throws SQLException |
| 65 |
{ |
| 66 |
|
| 67 |
System.setProperty( "hsqldb.cache_scale", "8" ); |
| 68 |
|
| 69 |
final JDBCDiskCache<K, V> cache = super.createCache(rawAttr, compositeCacheManager, |
| 70 |
cacheEventLogger, elementSerializer); |
| 71 |
setupDatabase( cache.getDataSource(), (JDBCDiskCacheAttributes) rawAttr ); |
| 72 |
|
| 73 |
return cache; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected void setupDatabase( final DataSource ds, final JDBCDiskCacheAttributes attributes ) |
| 84 |
throws SQLException |
| 85 |
{ |
| 86 |
try (Connection cConn = ds.getConnection()) |
| 87 |
{ |
| 88 |
setupTable( cConn, attributes.getTableName() ); |
| 89 |
log.info( "Finished setting up table [{0}]", attributes.getTableName()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
protected synchronized void setupTable( final Connection cConn, final String tableName ) throws SQLException |
| 100 |
{ |
| 101 |
final DatabaseMetaData dmd = cConn.getMetaData(); |
| 102 |
final ResultSet result = dmd.getTables(null, null, tableName, null); |
| 103 |
|
| 104 |
if (!result.next()) |
| 105 |
{ |
| 106 |
|
| 107 |
final StringBuilder createSql = new StringBuilder(); |
| 108 |
createSql.append( "CREATE CACHED TABLE ").append( tableName ); |
| 109 |
createSql.append( "( " ); |
| 110 |
createSql.append( "CACHE_KEY VARCHAR(250) NOT NULL, " ); |
| 111 |
createSql.append( "REGION VARCHAR(250) NOT NULL, " ); |
| 112 |
createSql.append( "ELEMENT BINARY, " ); |
| 113 |
createSql.append( "CREATE_TIME TIMESTAMP, " ); |
| 114 |
createSql.append( "UPDATE_TIME_SECONDS BIGINT, " ); |
| 115 |
createSql.append( "MAX_LIFE_SECONDS BIGINT, " ); |
| 116 |
createSql.append( "SYSTEM_EXPIRE_TIME_SECONDS BIGINT, " ); |
| 117 |
createSql.append( "IS_ETERNAL CHAR(1), " ); |
| 118 |
createSql.append( "PRIMARY KEY (CACHE_KEY, REGION) " ); |
| 119 |
createSql.append( ");" ); |
| 120 |
|
| 121 |
try (Statement sStatement = cConn.createStatement()) |
| 122 |
{ |
| 123 |
sStatement.execute( createSql.toString() ); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |