void initializeConnectionFactory(ConnectionFactory connectionFactory) throws PersistenceException { this.connectionFactory = connectionFactory; tableManager = getTableManager(); tableManager.setCacheName(cacheName); tableManager.start(); }
@Override public boolean contains(Object key) { //we can do better if needed... return load(key) != null; }
@Override public void rollback(Transaction tx) { Connection connection; try { connection = getTxConnection(tx); connection.rollback(); } catch (SQLException e) { log.sqlFailureTxRollback(e); throw new PersistenceException(String.format("Error during rollback of JDBC transaction (%s)", tx), e); } finally { destroyTxConnection(tx); } }
private void write(MarshalledEntry entry, Connection connection, String keyStr, int segment) throws SQLException, InterruptedException { if (tableManager.isUpsertSupported()) { executeUpsert(connection, entry, keyStr, segment); } else { executeLegacyUpdate(connection, entry, keyStr, segment); } }
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(); }
private void assertRowCount(int rowCount) { ConnectionFactory connectionFactory = store.getConnectionFactory(); TableName tableName = store.getTableManager().getTableName(); int value = UnitTestDatabaseManager.rowCount(connectionFactory, tableName); assert value == rowCount : "Expected " + rowCount + " rows, actual value is " + value; }
@Override public void start() { if (configuration.manageConnectionFactory()) { ConnectionFactory factory = ConnectionFactory.getConnectionFactory(configuration.connectionFactory().connectionFactoryClass()); factory.start(configuration.connectionFactory(), factory.getClass().getClassLoader()); initializeConnectionFactory(factory); } try { Object mapper = Util.loadClassStrict(configuration.key2StringMapper(), globalConfiguration.classLoader()).newInstance(); if (mapper instanceof Key2StringMapper) key2StringMapper = (Key2StringMapper) mapper; } catch (Exception e) { log.errorf("Trying to instantiate %s, however it failed due to %s", configuration.key2StringMapper(), e.getClass().getName()); throw new IllegalStateException("This should not happen.", e); } if (trace) { log.tracef("Using key2StringMapper: %s", key2StringMapper.getClass().getName()); } if (configuration.preload()) { enforceTwoWayMapper("preload"); } if (isDistributedCache) { enforceTwoWayMapper("distribution/rehashing"); } }
@Override protected AdvancedLoadWriteStore createStore() throws Exception { ConfigurationBuilder builder = TestCacheManagerFactory.getDefaultCacheConfiguration(false); JdbcStringBasedStoreConfigurationBuilder storeBuilder = builder .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); storeBuilder.segmented(segmented); UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder); UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table()); JdbcStringBasedStore stringBasedCacheStore = new JdbcStringBasedStore(); stringBasedCacheStore.init(createContext(builder.build())); return stringBasedCacheStore; }
protected ConnectionFactory getConnection() { JdbcStringBasedStore store = (JdbcStringBasedStore) cacheStore; return store.getConnectionFactory(); }
@AfterMethod public void tearDown() throws PersistenceException { store.clear(); assertRowCount(0); TestingUtil.killCacheManagers(cacheManager); }
@BeforeClass public void createCacheStore() throws PersistenceException { ConfigurationBuilder builder = TestCacheManagerFactory.getDefaultCacheConfiguration(false); JdbcStringBasedStoreConfigurationBuilder storeBuilder = createJdbcConfig(builder); UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table()); UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder); cacheStore = new JdbcStringBasedStore(); marshaller = new TestObjectStreamMarshaller(); cacheStore.init(PersistenceMockUtil.createContext(getClass().getSimpleName(), builder.build(), marshaller)); cacheStore.start(); tableManager = (TableManager) ReflectionUtil.getValue(cacheStore, "tableManager"); }
public void testForJdbcStringBasedCacheStore() throws Exception { CacheContainer cm = null; try { cm = TestCacheManagerFactory.fromXml("configs/string-based.xml"); Cache<String, String> first = cm.getCache("first"); Cache<String, String> second = cm.getCache("second"); StoreConfiguration firstCacheLoaderConfig = first.getCacheConfiguration().persistence().stores().get(0); StoreConfiguration secondCacheLoaderConfig = second.getCacheConfiguration().persistence().stores().get(0); assertEquals(JdbcStringBasedStoreConfiguration.class, firstCacheLoaderConfig.getClass()); assertEquals(JdbcStringBasedStoreConfiguration.class, secondCacheLoaderConfig.getClass()); JdbcStringBasedStore firstCs = (JdbcStringBasedStore) TestingUtil.getFirstLoader(first); JdbcStringBasedStore secondCs = (JdbcStringBasedStore) TestingUtil.getFirstLoader(second); assertTableExistence(firstCs.getConnectionFactory().getConnection(), firstCs.getTableManager().getIdentifierQuoteString(), "second", "first", "ISPN_STRING_TABLE"); assertNoOverlapingState(first, second, firstCs, secondCs); } finally { TestingUtil.killCacheManagers(cm); } }
@Override protected AdvancedLoadWriteStore createStore() throws Exception { ConfigurationBuilder builder = TestCacheManagerFactory.getDefaultCacheConfiguration(false); JdbcStringBasedStoreConfigurationBuilder storeBuilder = builder .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); storeBuilder.dataSource() .jndiUrl(getDatasourceLocation()); UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table()); JdbcStringBasedStore stringBasedCacheStore = new JdbcStringBasedStore(); stringBasedCacheStore.init(createContext(builder.build())); return stringBasedCacheStore; }
@Override public void call() { Cache<String, String> first = cm.getCache("first"); Cache<String, String> second = cm.getCache("second"); StoreConfiguration firstCacheLoaderConfig = first.getCacheConfiguration().persistence().stores().get(0); assertNotNull(firstCacheLoaderConfig); assertTrue(firstCacheLoaderConfig instanceof JdbcStringBasedStoreConfiguration); StoreConfiguration secondCacheLoaderConfig = second.getCacheConfiguration().persistence().stores().get(0); assertNotNull(secondCacheLoaderConfig); assertTrue(secondCacheLoaderConfig instanceof JdbcStringBasedStoreConfiguration); JdbcStringBasedStore loader = (JdbcStringBasedStore) TestingUtil.getFirstLoader(first); assertTrue(loader.getConnectionFactory() instanceof ManagedConnectionFactory); } });
@Test public void testTxCommit() throws Exception { cache.put(KEY1, VAL1); TransactionManager tm = TestingUtil.getTransactionManager(cache); tm.begin(); cache.put(KEY2, VAL1); String oldValue = cache.put(KEY1, VAL2); assertEquals(oldValue, VAL1); tm.commit(); String cacheVal = cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD).get(KEY1); assertEquals(cacheVal, VAL2); // Ensure the values committed in the Tx were actually written to the store as well as to the cache assertRowCount(2); assertEquals(store.load(KEY1).getValue(), VAL2); assertEquals(store.load(KEY2).getValue(), VAL1); }
@Override public void commit(Transaction tx) { Connection connection; try { connection = getTxConnection(tx); connection.commit(); } catch (SQLException e) { log.sqlFailureTxCommit(e); throw new PersistenceException(String.format("Error during commit of JDBC transaction (%s)", tx), e); } finally { destroyTxConnection(tx); } }