@Override public void close() throws SQLException { stop(); } }
public static JdbcSharedStateManager usingConnectionUrl(String holderId, long locksExpirationMillis, String jdbcConnectionUrl, String jdbcDriverClass, SQLProvider provider) { return JdbcSharedStateManager.usingConnectionUrl(holderId, -1, null, locksExpirationMillis, jdbcConnectionUrl, jdbcDriverClass, provider); }
public static JdbcSharedStateManager usingConnectionUrl(String holderId, int networkTimeout, Executor networkTimeoutExecutor, long locksExpirationMillis, String jdbcConnectionUrl, String jdbcDriverClass, SQLProvider provider) { final JdbcSharedStateManager sharedStateManager = new JdbcSharedStateManager(holderId, locksExpirationMillis); sharedStateManager.setNetworkTimeout(networkTimeoutExecutor, networkTimeout); sharedStateManager.setJdbcConnectionUrl(jdbcConnectionUrl); sharedStateManager.setJdbcDriverClass(jdbcDriverClass); sharedStateManager.setSqlProvider(provider); try { sharedStateManager.start(); return sharedStateManager; } catch (SQLException e) { throw new IllegalStateException(e); } }
public static JdbcSharedStateManager usingDataSource(String holderId, int networkTimeout, Executor networkTimeoutExecutor, long locksExpirationMillis, DataSource dataSource, SQLProvider provider) { final JdbcSharedStateManager sharedStateManager = new JdbcSharedStateManager(holderId, locksExpirationMillis); sharedStateManager.setNetworkTimeout(networkTimeoutExecutor, networkTimeout); sharedStateManager.setDataSource(dataSource); sharedStateManager.setSqlProvider(provider); try { sharedStateManager.start(); return sharedStateManager; } catch (SQLException e) { throw new IllegalStateException(e); } }
@Override protected void prepareStatements() throws SQLException { this.liveLock = createLiveLock(this.holderId, this.connection, sqlProvider, lockExpirationMillis); this.backupLock = createBackupLock(this.holderId, this.connection, sqlProvider, lockExpirationMillis); this.readNodeId = connection.prepareStatement(sqlProvider.readNodeIdSQL()); this.writeNodeId = connection.prepareStatement(sqlProvider.writeNodeIdSQL()); this.initializeNodeId = connection.prepareStatement(sqlProvider.initializeNodeIdSQL()); this.writeState = connection.prepareStatement(sqlProvider.writeStateSQL()); this.readState = connection.prepareStatement(sqlProvider.readStateSQL()); }
private LeaseLock lock(long acquireMillis) { try { return JdbcSharedStateManager .createLiveLock( UUID.randomUUID().toString(), jdbcSharedStateManager.getConnection(), sqlProvider, acquireMillis); } catch (SQLException e) { throw new IllegalStateException(e); } }
@Override public void writeState(State state) { final String encodedState = encodeState(state); synchronized (connection) { try { connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); final boolean autoCommit = connection.getAutoCommit(); connection.setAutoCommit(false); try { final PreparedStatement preparedStatement = this.writeState; preparedStatement.setString(1, encodedState); if (preparedStatement.executeUpdate() != 1) { throw new IllegalStateException("can't write state to the JDBC Node Manager Store!"); } connection.commit(); } catch (SQLException ie) { connection.rollback(); connection.setAutoCommit(true); throw new IllegalStateException(ie); } finally { connection.setAutoCommit(autoCommit); } } catch (SQLException e) { throw new IllegalStateException(e); } } }
@Override public State readState() { synchronized (connection) { try { connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); final boolean autoCommit = connection.getAutoCommit(); connection.setAutoCommit(false); final State state; try { final PreparedStatement preparedStatement = this.readState; try (ResultSet resultSet = preparedStatement.executeQuery()) { if (!resultSet.next()) { state = State.FIRST_TIME_START; } else { state = decodeState(resultSet.getString(1)); } } connection.commit(); return state; } catch (SQLException ie) { connection.rollback(); throw new IllegalStateException(ie); } finally { connection.setAutoCommit(autoCommit); } } catch (SQLException e) { throw new IllegalStateException(e); } } }
@Override protected void createSchema() { try { createTable(sqlProvider.createNodeManagerStoreTableSQL(), sqlProvider.createNodeIdSQL(), sqlProvider.createStateSQL(), sqlProvider.createLiveLockSQL(), sqlProvider.createBackupLockSQL()); } catch (SQLException e) { //no op: if a table already exists is not a problem in this case, the prepareStatements() call will fail right after it if the table is not correctly initialized logger.debug("Error while creating the schema of the JDBC shared state manager", e); } }
@Test(timeout = 10000) public void shouldStartTwoIfTableNotExist() throws Exception { final JdbcSharedStateManager liveSharedStateManager = createSharedStateManager(); final JdbcSharedStateManager backupSharedStateManager = createSharedStateManager(); backupSharedStateManager.stop(); try { liveSharedStateManager.destroy(); } finally { liveSharedStateManager.stop(); } } }
private static JdbcNodeManager usingConnectionUrl(String brokerId, int networkTimeoutMillis, long lockExpirationMillis, long lockRenewPeriodMillis, long lockAcquisitionTimeoutMillis, String jdbcUrl, String driverClass, SQLProvider provider, ScheduledExecutorService scheduledExecutorService, ExecutorFactory executorFactory, IOCriticalErrorListener ioCriticalErrorListener) { return new JdbcNodeManager( () -> JdbcSharedStateManager.usingConnectionUrl(brokerId, networkTimeoutMillis, executorFactory == null ? null : executorFactory.getExecutor(), lockExpirationMillis, jdbcUrl, driverClass, provider), lockRenewPeriodMillis, lockAcquisitionTimeoutMillis, scheduledExecutorService, executorFactory, ioCriticalErrorListener); }
@Test(timeout = 10000) public void shouldStartIfTableExistInitialized() throws Exception { final TestJDBCDriver fakeDriver = createFakeDriver(true); fakeDriver.start(); final JdbcSharedStateManager sharedStateManager = createSharedStateManager(); sharedStateManager.stop(); try { fakeDriver.destroy(); } finally { fakeDriver.stop(); } }
private JdbcSharedStateManager createSharedStateManager() { return JdbcSharedStateManager.usingConnectionUrl( UUID.randomUUID().toString(), dbConf.getJdbcLockExpirationMillis(), dbConf.getJdbcConnectionUrl(), dbConf.getJdbcDriverClassName(), sqlProvider); }
@Test(timeout = 10000) public void shouldStartIfTableExistEmpty() throws Exception { final TestJDBCDriver fakeDriver = createFakeDriver(false); fakeDriver.start(); final JdbcSharedStateManager sharedStateManager = createSharedStateManager(); sharedStateManager.stop(); try { fakeDriver.destroy(); } finally { fakeDriver.stop(); } }
@Before public void createLockTable() throws Exception { dbConf = createDefaultDatabaseStorageConfiguration(); sqlProvider = JDBCUtils.getSQLProvider( dbConf.getJdbcDriverClassName(), dbConf.getNodeManagerStoreTableName(), SQLProvider.DatabaseStoreType.NODE_MANAGER); if (withExistingTable) { TestJDBCDriver testDriver = TestJDBCDriver .usingConnectionUrl( dbConf.getJdbcConnectionUrl(), dbConf.getJdbcDriverClassName(), sqlProvider); testDriver.start(); testDriver.stop(); } jdbcSharedStateManager = JdbcSharedStateManager .usingConnectionUrl( UUID.randomUUID().toString(), dbConf.getJdbcLockExpirationMillis(), dbConf.getJdbcConnectionUrl(), dbConf.getJdbcDriverClassName(), sqlProvider); }