private void destroyTxConnection(Transaction tx) { Connection connection = transactionConnectionMap.remove(tx); if (connection != null) connectionFactory.releaseConnection(connection); }
@Override public boolean isAvailable() { if (tableManager == null || connectionFactory == null) return false; Connection connection = null; try { connection = connectionFactory.getConnection(); return connection != null && connection.isValid(10); } catch (SQLException e) { return false; } finally { connectionFactory.releaseConnection(connection); } }
@Override public void close() { JdbcUtil.safeClose(rs); JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(conn); } }
@Override public void stop() throws PersistenceException { if (config.dropOnExit()) { Connection conn = null; try { conn = connectionFactory.getConnection(); dropTable(conn); } finally { connectionFactory.releaseConnection(conn); } } }
@Override public void deleteBatch(Iterable<Object> keys) { Connection connection = null; try { connection = connectionFactory.getConnection(); try (PreparedStatement deleteBatch = connection.prepareStatement(tableManager.getDeleteRowSql())) { int batchSize = 0; for (Object key : keys) { String keyStr = key2Str(key); deleteBatch.setString(1, keyStr); deleteBatch.addBatch(); batchSize++; if (batchSize == configuration.maxBatchSize()) { batchSize = 0; deleteBatch.executeBatch(); deleteBatch.clearBatch(); } } if (batchSize != 0) deleteBatch.executeBatch(); } } catch (SQLException e) { throw log.sqlFailureDeletingBatch(keys, e); } finally { connectionFactory.releaseConnection(connection); } }
@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 boolean delete(Object key) { Connection connection = null; PreparedStatement ps = null; String keyStr = key2Str(key); try { String sql = tableManager.getDeleteRowSql(); if (trace) { log.tracef("Running sql '%s' on %s", sql, keyStr); } connection = connectionFactory.getConnection(); ps = connection.prepareStatement(sql); ps.setString(1, keyStr); return ps.executeUpdate() == 1; } catch (SQLException ex) { log.sqlFailureRemovingKeys(ex); throw new PersistenceException("Error while removing string keys from database", ex); } finally { JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(connection); } }
@Override public void write(MarshalledEntry entry) { Connection connection = null; String keyStr = key2Str(entry.getKey()); try { connection = connectionFactory.getConnection(); write(entry, connection, keyStr, getSegment(entry)); } catch (SQLException ex) { log.sqlFailureStoringKey(keyStr, ex); throw new PersistenceException(String.format("Error while storing string key to database; key: '%s'", keyStr), ex); } catch (InterruptedException e) { if (trace) { log.trace("Interrupted while marshalling to store"); } Thread.currentThread().interrupt(); } finally { connectionFactory.releaseConnection(connection); } }
@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); } }
JdbcUtil.safeClose(rs); JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(conn);
@Override public void clear() { Connection conn = null; Statement statement = null; try { String sql = tableManager.getDeleteAllRowsSql(); conn = connectionFactory.getConnection(); statement = conn.createStatement(); int result = statement.executeUpdate(sql); if (log.isTraceEnabled()) { log.tracef("Successfully removed %d rows.", result); } } catch (SQLException ex) { log.failedClearingJdbcCacheStore(ex); throw new PersistenceException("Failed clearing cache store", ex); } finally { JdbcUtil.safeClose(statement); connectionFactory.releaseConnection(conn); } }
throw log.sqlFailureWritingBatch(e); } finally { connectionFactory.releaseConnection(connection);
/** * Counts the number of rows in the given table. */ public static int rowCount(ConnectionFactory connectionFactory, TableName tableName) { Connection conn = null; PreparedStatement statement = null; ResultSet resultSet = null; try { conn = connectionFactory.getConnection(); String sql = "SELECT count(*) FROM " + tableName; statement = conn.prepareStatement(sql); resultSet = statement.executeQuery(); resultSet.next(); return resultSet.getInt(1); } catch (Exception ex) { throw new RuntimeException(ex); } finally { JdbcUtil.safeClose(resultSet); JdbcUtil.safeClose(statement); connectionFactory.releaseConnection(conn); } }
@Override public void clear(IntSet segments) { Connection conn = null; PreparedStatement ps = null; try { String sql = tableManager.getDeleteRowsSqlForSegments(segments.size()); conn = connectionFactory.getConnection(); ps = conn.prepareStatement(sql); int offset = 0; for (PrimitiveIterator.OfInt segIter = segments.iterator(); segIter.hasNext(); ) { ps.setInt(++offset, segIter.nextInt()); } int result = ps.executeUpdate(); if (log.isTraceEnabled()) { log.tracef("Successfully removed %d rows.", result); } } catch (SQLException ex) { log.failedClearingJdbcCacheStore(ex); throw new PersistenceException("Failed clearing cache store when using segments " + segments, ex); } finally { JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(conn); } }
}, kvp -> { JdbcUtil.safeClose(kvp.getValue()); connectionFactory.releaseConnection(kvp.getKey()); });
JdbcUtil.safeClose(rs); JdbcUtil.safeClose(ps); connectionFactory.releaseConnection(conn);
LOG.debug("Unable to retrieve DB Major and Minor versions from JDBC metadata.", e); } finally { connectionFactory.releaseConnection(connection); LOG.debug("Unable to guess dialect from JDBC metadata.", e); } finally { connectionFactory.releaseConnection(connection); LOG.debug("Unable to guess database dialect from JDBC driver name.", e); } finally { connectionFactory.releaseConnection(connection);
@Override public void start() throws PersistenceException { if (config.createOnStart()) { Connection conn = null; try { conn = connectionFactory.getConnection(); if (!tableExists(conn)) { createTable(conn); } createIndex(conn, timestampIndexExt, config.timestampColumnName()); if (!metaData.isSegmentedDisabled()) { createIndex(conn, segmentIndexExt, config.segmentColumnName()); } } finally { connectionFactory.releaseConnection(conn); } } }