void initializeConnectionFactory(ConnectionFactory connectionFactory) throws PersistenceException { this.connectionFactory = connectionFactory; tableManager = getTableManager(); tableManager.setCacheName(cacheName); tableManager.start(); }
AbstractJdbcEntryIterator(ConnectionFactory connectionFactory, TableManager tableManager, StreamingMarshaller marshaller) { this.connectionFactory = connectionFactory; this.tableManager = tableManager; this.marshaller = marshaller; PreparedStatement countPs = null; ResultSet countRs = null; try { conn = connectionFactory.getConnection(); countPs = conn.prepareStatement(tableManager.getCountNonExpiredRowsSql()); countPs.setLong(1, System.currentTimeMillis()); countRs = countPs.executeQuery(); countRs.next(); numberOfRows = countRs.getInt(1); ps = conn.prepareStatement(tableManager.getLoadAllRowsSql(), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ps.setFetchSize(tableManager.getFetchSize()); rs = ps.executeQuery(); } catch (SQLException e) { throw new PersistenceException("SQL error while fetching all StoredEntries", e); } finally { JdbcUtil.safeClose(countPs); JdbcUtil.safeClose(countRs); } }
public void testTableQuoting() throws Exception { tableManager.setCacheName("my.cache"); assert !existsTable(connection, tableManager.getTableName()); tableManager.createTable(connection); assert existsTable(connection, tableManager.getTableName()); }
private String key2Str(Object key) throws PersistenceException { if (!key2StringMapper.isSupportedType(key.getClass())) { throw new UnsupportedKeyTypeException(key); } String keyStr = key2StringMapper.getStringMapping(key); return tableManager.isStringEncodingRequired() ? tableManager.encodeString(keyStr) : keyStr; }
public void testConnectionLeakGuessDialect() throws Exception { JdbcStringBasedStoreConfigurationBuilder storeBuilder = TestCacheManagerFactory .getDefaultCacheConfiguration(false) .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table()); PooledConnectionFactory connectionFactory = new PooledConnectionFactory(); ConnectionFactoryConfiguration config = UnitTestDatabaseManager .configureUniqueConnectionFactory(storeBuilder).create(); connectionFactory.start(config, Thread.currentThread().getContextClassLoader()); // JdbcStringBasedStoreConfiguration defaults to null dialect, so dialect and versions must be guessed TableManager tableManager = TableManagerFactory.getManager(connectionFactory, storeBuilder.create()); tableManager.setCacheName("GuessDialect"); tableManager.start(); UnitTestDatabaseManager.verifyConnectionLeaks(connectionFactory); tableManager.stop(); connectionFactory.stop(); }
String sql; if (segments != null) { sql = tableManager.getLoadNonExpiredRowsSqlForSegments(segments.size()); } else { sql = tableManager.getLoadNonExpiredAllRowsSql(); ps.setFetchSize(tableManager.getFetchSize()); ResultSet rs = ps.executeQuery(); return function.apply(rs);
private TableManager getTableManager(boolean binary) { TableManipulationConfiguration tableConfig = binary ? binaryConfig : stringConfig; TableManager tableManager = TableManagerFactory.getManager(metaData, connectionFactory, tableConfig); tableManager.setCacheName(props.cacheName()); return tableManager; }
@Test(dependsOnMethods = "testExists") public void testDrop() throws Exception { assert tableManager.tableExists(connection); byte[] data = new byte[64]; new Random().nextBytes(data); PreparedStatement ps = null; try { ps = connection.prepareStatement("INSERT INTO " + tableManager.getTableName() + "(ID_COLUMN, DATA_COLUMN, TIMESTAMP_COLUMN) values(?, ?, ?)"); ps.setString(1, System.currentTimeMillis() + ""); ps.setBlob(2, new ByteArrayInputStream(data)); ps.setLong(3, System.currentTimeMillis()); assert 1 == ps.executeUpdate(); } finally { JdbcUtil.safeClose(ps); } tableManager.dropTable(connection); assert !tableManager.tableExists(connection); }
public void testCreateTable() throws Exception { assert !existsTable(connection, tableManager.getTableName()); tableManager.createTable(connection); assert existsTable(connection, tableManager.getTableName()); }
@Override public int size() { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = connectionFactory.getConnection(); String sql = tableManager.getCountNonExpiredRowsSql(); ps = conn.prepareStatement(sql); ps.setLong(1, timeService.wallClockTime()); rs = ps.executeQuery(); rs.next(); return rs.getInt(1); } catch (SQLException e) { log.sqlFailureIntegratingState(e); throw new PersistenceException("SQL failure while integrating state into store", e); } finally { JdbcUtil.safeClose(rs); JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(conn); } }
@Override public void stop() { Throwable cause = null; try { tableManager.stop(); tableManager = null; } catch (Throwable t) { cause = t.getCause(); if (cause == null) cause = t; log.debug("Exception while stopping", t); } try { if (configuration.connectionFactory() instanceof ManagedConnectionFactory) { log.tracef("Stopping managed connection factory: %s", connectionFactory); connectionFactory.stop(); } } catch (Throwable t) { if (cause == null) { cause = t; } else { t.addSuppressed(cause); } log.debug("Exception while stopping", t); } if (cause != null) { throw new PersistenceException("Exceptions occurred while stopping store", cause); } }
@Override public int size(IntSet segments) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = connectionFactory.getConnection(); String sql = tableManager.getCountNonExpiredRowsSqlForSegments(segments.size()); ps = conn.prepareStatement(sql); int offset = 1; ps.setLong(offset, timeService.wallClockTime()); for (PrimitiveIterator.OfInt segIter = segments.iterator(); segIter.hasNext(); ) { ps.setInt(++offset, segIter.nextInt()); } rs = ps.executeQuery(); rs.next(); return rs.getInt(1); } catch (SQLException e) { log.sqlFailureIntegratingState(e); throw new PersistenceException("SQL failure while integrating state into store", e); } finally { JdbcUtil.safeClose(rs); JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(conn); } }
public void testNotCreateConnectionFactory() throws Exception { ConfigurationBuilder builder = TestCacheManagerFactory .getDefaultCacheConfiguration(false); JdbcStringBasedStoreConfigurationBuilder storeBuilder = builder .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class) .manageConnectionFactory(false) .dialect(DatabaseType.H2) .dbMajorVersion(1) .dbMinorVersion(4); storeBuilder.table().createOnStart(false); JdbcStringBasedStore stringBasedCacheStore = new JdbcStringBasedStore(); stringBasedCacheStore.init(createContext(builder.build())); stringBasedCacheStore.start(); assertNull(stringBasedCacheStore.getConnectionFactory()); // this will make sure that if a method like stop is called on the connection then it will barf an exception ConnectionFactory connectionFactory = mock(ConnectionFactory.class); TableManager tableManager = mock(TableManager.class); tableManager.start(); tableManager.setCacheName("otherName"); stringBasedCacheStore.initializeConnectionFactory(connectionFactory); //stop should be called even if this is an external reset(tableManager, connectionFactory); tableManager.stop(); stringBasedCacheStore.stop(); }
@BeforeClass public void createConnection() throws Exception { JdbcStringBasedStoreConfigurationBuilder storeBuilder = TestCacheManagerFactory .getDefaultCacheConfiguration(false) .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); UnitTestDatabaseManager.setDialect(storeBuilder); UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table()); ConnectionFactoryConfiguration factoryConfiguration = UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder).create(); if (factoryConfiguration instanceof SimpleConnectionFactoryConfiguration) { SimpleConnectionFactoryConfiguration simpleConfiguration = (SimpleConnectionFactoryConfiguration) factoryConfiguration; connectionFactory = ConnectionFactory.getConnectionFactory(SimpleConnectionFactory.class); connectionFactory.start(simpleConfiguration, connectionFactory.getClass().getClassLoader()); connection = connectionFactory.getConnection(); } else if (factoryConfiguration instanceof PooledConnectionFactoryConfiguration) { PooledConnectionFactoryConfiguration pooledConfiguration = (PooledConnectionFactoryConfiguration) factoryConfiguration; connectionFactory = ConnectionFactory.getConnectionFactory(PooledConnectionFactory.class); connectionFactory.start(pooledConfiguration, connectionFactory.getClass().getClassLoader()); connection = connectionFactory.getConnection(); } tableManager = TableManagerFactory.getManager(connectionFactory, storeBuilder.create()); tableManager.setCacheName("aName"); }