@Override public void close() { DbUtils.closeQuietly(pstmt); pstmt = null; }
@Override public void close() { closed = true; DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); }
public void readLineHashesStream(DbSession dbSession, String fileUuid, Consumer<Reader> consumer) { Connection connection = dbSession.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; Reader reader = null; try { pstmt = connection.prepareStatement("SELECT line_hashes FROM file_sources WHERE file_uuid=? AND data_type='SOURCE'"); pstmt.setString(1, fileUuid); rs = pstmt.executeQuery(); if (rs.next()) { reader = rs.getCharacterStream(1); if (reader != null) { consumer.accept(reader); } } } catch (SQLException e) { throw new IllegalStateException("Fail to read FILE_SOURCES.LINE_HASHES of file " + fileUuid, e); } finally { IOUtils.closeQuietly(reader); DbUtils.closeQuietly(connection, pstmt, rs); } }
private void checkConnection() { Connection connection = null; try { connection = datasource.getConnection(); dialect.init(connection.getMetaData()); } catch (SQLException e) { throw new IllegalStateException("Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').", e); } finally { DbUtils.closeQuietly(connection); } }
@CheckForNull public List<String> selectLineHashes(DbSession dbSession, String fileUuid) { Connection connection = dbSession.getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = connection.prepareStatement("SELECT line_hashes FROM file_sources WHERE file_uuid=? AND data_type='SOURCE'"); pstmt.setString(1, fileUuid); rs = pstmt.executeQuery(); if (rs.next()) { String string = rs.getString(1); if (string == null) { return Collections.emptyList(); } return END_OF_LINE_SPLITTER.splitToList(string); } return null; } catch (SQLException e) { throw new IllegalStateException("Fail to read FILE_SOURCES.LINE_HASHES of file " + fileUuid, e); } finally { DbUtils.closeQuietly(connection, pstmt, rs); } }
@Override public <T> List<T> list(Select.RowReader<T> reader) throws SQLException { ResultSet rs = pstmt.executeQuery(); Select.Row row = new Select.Row(rs); try { List<T> rows = new ArrayList<>(); while (rs.next()) { rows.add(reader.read(row)); } return rows; } catch (Exception e) { throw newExceptionWithRowDetails(row, e); } finally { DbUtils.closeQuietly(rs); close(); } }
protected boolean shouldTruncate(Connection connection, String table) throws SQLException { Statement stmt = connection.createStatement(); ResultSet rs = null; try { rs = stmt.executeQuery("select count(1) from " + table); if (rs.next()) { return rs.getInt(1) > 0; } } catch (SQLException ignored) { // probably because table does not exist. That's the case with H2 tests. } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); } return false; }
@Override public void scroll(Select.RowHandler handler) throws SQLException { ResultSet rs = pstmt.executeQuery(); Select.Row row = new Select.Row(rs); try { while (rs.next()) { handler.handle(row); } } catch (Exception e) { throw newExceptionWithRowDetails(row, e); } finally { DbUtils.closeQuietly(rs); close(); } }
@Override public <T> T get(Select.RowReader<T> reader) throws SQLException { ResultSet rs = pstmt.executeQuery(); Select.Row row = new Select.Row(rs); try { if (rs.next()) { return reader.read(row); } return null; } catch (Exception e) { throw newExceptionWithRowDetails(row, e); } finally { DbUtils.closeQuietly(rs); close(); } }
/** * Provide a way to allow users define custom SQL operations without relying on fixed SQL * interface. The common use case is to group a sequence of SQL operations without commit every * time. * * @param operations A sequence of DB operations * @param <T> The type of object that the operations returns. Note that T could be null * @return T The object returned by the SQL statement, expected by the caller */ public <T> T transaction(final SQLTransaction<T> operations) throws SQLException { Connection conn = null; try { conn = this.queryRunner.getDataSource().getConnection(); conn.setAutoCommit(false); final DatabaseTransOperator transOperator = new DatabaseTransOperator(this.queryRunner, conn); final T res = operations.execute(transOperator); conn.commit(); return res; } catch (final SQLException ex) { // todo kunkun-tang: Retry logics should be implemented here. logger.error("transaction failed", ex); if (this.dbMetrics != null) { this.dbMetrics.markDBFailTransaction(); } throw ex; } finally { DbUtils.closeQuietly(conn); } }
@Test public void testGetNewConnectionAfterClose() throws Exception { this.connection.setAutoCommit(false); /** * See {@link org.apache.commons.dbcp2.PoolableConnectionFactory#passivateObject}. * If the connection disables auto commit, when we close it, connection will be reset enabling auto commit, * and returned to connection pool. */ DbUtils.closeQuietly(this.connection); final Connection newConnection = this.h2DataSource.getConnection(); Assert.assertEquals(newConnection.getAutoCommit(), true); DbUtils.closeQuietly(newConnection); }
public void truncateDatabase(DataSource dataSource) throws SQLException { Connection connection = dataSource.getConnection(); Statement statement = null; try { connection.setAutoCommit(false); statement = connection.createStatement(); for (String table : SqTables.TABLES) { try { if (shouldTruncate(connection, table)) { statement.executeUpdate(truncateSql(table)); connection.commit(); } } catch (Exception e) { connection.rollback(); throw new IllegalStateException("Fail to truncate table " + table, e); } } } finally { DbUtils.closeQuietly(connection); DbUtils.closeQuietly(statement); } }
private void createSchema() { Connection connection = null; try { connection = datasource.getConnection(); DdlUtils.createSchema(connection, "h2", true); } catch (SQLException e) { throw new IllegalStateException("Fail to create schema", e); } finally { DbUtils.closeQuietly(connection); } }
@Test public void testConnectionDisableAutoCommit() throws Exception { this.connection.setAutoCommit(false); Assert.assertEquals(this.connection.getAutoCommit(), false); DbUtils.closeQuietly(this.connection); }
public void executeScript(String classloaderPath) { Connection connection = null; try { connection = datasource.getConnection(); DdlUtils.executeScript(connection, classloaderPath); } catch (SQLException e) { throw new IllegalStateException("Fail to execute script: " + classloaderPath, e); } finally { DbUtils.closeQuietly(connection); } }
@VisibleForTesting void installH2() { Connection connection = null; try (DbSession session = dbClient.openSession(false)) { connection = session.getConnection(); createH2Schema(connection, dbClient.getDatabase().getDialect().getId()); } finally { DbUtils.closeQuietly(connection); } }
public void resetPrimaryKeys(DataSource dataSource) throws SQLException { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { connection = dataSource.getConnection(); connection.setAutoCommit(false); statement = connection.createStatement(); for (String table : SqTables.TABLES) { try { resultSet = statement.executeQuery("SELECT CASE WHEN MAX(ID) IS NULL THEN 1 ELSE MAX(ID)+1 END FROM " + table); resultSet.next(); int maxId = resultSet.getInt(1); resultSet.close(); for (String resetCommand : resetSequenceSql(table, maxId)) { statement.executeUpdate(resetCommand); } connection.commit(); } catch (Exception e) { connection.rollback(); // this table has no primary key } } } finally { DbUtils.closeQuietly(connection, statement, resultSet); } } }
@Test public void testConnectionDefaultAutoCommit() throws Exception { Assert.assertEquals(this.connection.getAutoCommit(), true); DbUtils.closeQuietly(this.connection); }
@Test public void testGetNewConnectionBeforeClose() throws Exception { this.connection.setAutoCommit(false); /** * {@link AzkabanDataSource#getConnection} fetches a new connection object other than one in the above, if we don't close. */ Assert.assertEquals(this.h2DataSource.getConnection().getAutoCommit(), true); DbUtils.closeQuietly(this.connection); }
@Test public void closeQuietlyConnectionResultSetThrowingExceptionStatement() throws Exception { Connection mockConnection = mock(Connection.class); ResultSet mockResultSet = mock(ResultSet.class); doThrow(SQLException.class).when(mockResultSet).close(); Statement mockStatement = mock(Statement.class); DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet); verify(mockConnection).close(); verify(mockResultSet).close(); verify(mockStatement).close(); }