public void executeUpdateSql(Connection conn, String sql) throws PersistenceException { Statement statement = null; try { statement = conn.createStatement(); statement.executeUpdate(sql); } catch (SQLException e) { log.errorCreatingTable(sql, e); throw new PersistenceException(e); } finally { JdbcUtil.safeClose(statement); } }
static boolean existsTable(Connection connection, TableName tableName) throws Exception { Statement st = connection.createStatement(); ResultSet rs = null; try { rs = st.executeQuery("select * from " + tableName); return true; } catch (SQLException e) { return false; } finally { JdbcUtil.safeClose(rs); JdbcUtil.safeClose(st); } } }
@Override public void releaseConnection(Connection conn) { logBefore(false); JdbcUtil.safeClose(conn); logAfter(conn, false); }
@Override protected boolean indexExists(String indexName, Connection conn) throws PersistenceException { ResultSet rs = null; try { DatabaseMetaData meta = conn.getMetaData(); rs = meta.getIndexInfo(null, null, getTableName().toString(), false, false); while (rs.next()) { String index = rs.getString("INDEX_NAME"); if (indexName.equalsIgnoreCase(index)) { return true; } } } catch (SQLException e) { throw new PersistenceException(e); } finally { JdbcUtil.safeClose(rs); } return false; }
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); } }
private void executeUpsert(Connection connection, MarshalledEntry entry, String keyStr, int segment) throws InterruptedException, SQLException { PreparedStatement ps = null; String sql = tableManager.getUpsertRowSql(); if (trace) { log.tracef("Running sql '%s'. Key string is '%s'", sql, keyStr); } try { ps = connection.prepareStatement(sql); prepareUpsertStatement(entry, keyStr, segment, ps); ps.executeUpdate(); } finally { JdbcUtil.safeClose(ps); } }
protected boolean indexExists(String indexName, Connection conn) throws PersistenceException { ResultSet rs = null; try { TableName table = getTableName(); DatabaseMetaData meta = conn.getMetaData(); rs = meta.getIndexInfo(null, table.getSchema(), table.getName(), false, false); while (rs.next()) { if (indexName.equalsIgnoreCase(rs.getString("INDEX_NAME"))) { return true; } } } catch (SQLException e) { throw new PersistenceException(e); } finally { JdbcUtil.safeClose(rs); } return false; }
private void executeLegacyUpdate(Connection connection, MarshalledEntry entry, String keyStr, int segment) throws InterruptedException, SQLException { String sql = tableManager.getSelectIdRowSql(); if (trace) { log.tracef("Running sql '%s'. Key string is '%s'", sql, keyStr); } PreparedStatement ps = null; try { ps = connection.prepareStatement(sql); ps.setString(1, keyStr); ResultSet rs = ps.executeQuery(); boolean update = rs.next(); if (update) { sql = tableManager.getUpdateRowSql(); } else { sql = tableManager.getInsertRowSql(); } JdbcUtil.safeClose(rs); JdbcUtil.safeClose(ps); if (trace) { log.tracef("Running sql '%s'. Key string is '%s'", sql, keyStr); } ps = connection.prepareStatement(sql); prepareStatement(entry, keyStr, segment, ps, !update); ps.executeUpdate(); } finally { JdbcUtil.safeClose(ps); } }
public boolean tableExists(Connection connection, TableName tableName) throws PersistenceException { Objects.requireNonNull(tableName, "table name is mandatory"); ResultSet rs = null; try { // we need to make sure, that (even if the user has extended permissions) only the tables in current schema are checked // explicit set of the schema to the current user one to make sure only tables of the current users are requested DatabaseMetaData metaData = connection.getMetaData(); String schemaPattern = tableName.getSchema(); rs = metaData.getTables(null, schemaPattern, tableName.getName(), new String[]{"TABLE"}); return rs.next(); } catch (SQLException e) { if (log.isTraceEnabled()) log.tracef(e, "SQLException occurs while checking the table %s", tableName); return false; } finally { JdbcUtil.safeClose(rs); } }
@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 tableExists(Connection connection, TableName tableName) throws PersistenceException { Objects.requireNonNull(tableName, "table name is mandatory"); ResultSet rs = null; try { DatabaseMetaData metaData = connection.getMetaData(); String schemaPattern = tableName.getSchema() == null ? metaData.getUserName() : tableName.getSchema(); rs = metaData.getTables(null, schemaPattern, tableName.getName(), new String[]{"TABLE"}); return rs.next(); } catch (SQLException e) { if (LOG.isTraceEnabled()) LOG.tracef(e, "SQLException occurs while checking the table %s", tableName); return false; } finally { JdbcUtil.safeClose(rs); } }
@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); } }
/** * 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 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); } }
@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); }
@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); } }
return function.apply(rs); }, kvp -> { JdbcUtil.safeClose(kvp.getValue()); connectionFactory.releaseConnection(kvp.getKey()); });