@Override public void loadProperties(AppPropertySetter props) { Try.throwUnchecked(() -> { log.info("Load properties from db : {}", jdbcUrl); Class.forName(driverClassName); Statement stmt = null; ResultSet rs = null; Connection conn = null; try { log.debug("Obtain db connection..."); conn = DriverManager.getConnection(jdbcUrl, username, password); log.debug("Execute sql ->\n{}\n", sql); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int count = 0; while (rs.next()) { String key = rs.getString(1); String value = rs.getString(2); props.putProperty(this, key, value); count++; } log.info("Load {} properties from db!", count); } finally { JDBC.closeResultSetOnly(rs); JDBC.closeStatementOnly(stmt); JDBC.closeConnection(conn); } }); }
protected static String extractServerFromJdbcUrl(DatabaseMetaData md) { return Try.throwUncheckedWithResult(() -> { return JDBC.tryExtractServerString(md.getURL()); }); } }
public static void closeResultSetAndConnection(ResultSet rs) { if(null != rs){ Connection conn = null; Statement stmt = null; try{ stmt = rs.getStatement(); conn = stmt.getConnection(); }catch(Throwable e){ ; } try { rs.close(); } catch (Throwable e) { log.warn("Error closing result set : {} ",e.getMessage(),e); } finally { closeStatementOnly(stmt); closeConnection(conn); } } }
@Override public void postExecuteUpdate(Db db, Connection connection, PreparedStatement ps, int updatedResult) throws SQLException { PreparedStatement ps1 = null; ResultSet rs = null; try{ ps1 = connection.prepareStatement(db.getDialect().getSelectCurrentSequenceValueSql(sequenceName)); rs = ps1.executeQuery(); if(rs.next()){ generatedIdCallback.accept(db.getDialect().getColumnValue(rs, 1)); }else{ throw new IllegalStateException("No current value of sequence '" + sequenceName + "' returned"); } }finally{ JDBC.closeResultSetOnly(rs); JDBC.closeStatementOnly(ps1); try { if(this.autoCommit){ connection.setAutoCommit(true); } } catch (Exception e) { log.warn("Error restoring the auto comment property of connection : " + e.getMessage(),e); } } } }
protected void returnConnectionToDataSource(Connection connection){ JDBC.closeConnection(connection); }
@Override public void close() throws SQLException { JDBC.closeStatementOnly(stmt); super.close(); } }
@Override public void postExecuteUpdate(Db db, Connection connection, PreparedStatement ps, int updatedResult) throws SQLException { ResultSet rs = null; try{ rs = ps.getGeneratedKeys(); if(rs.next()){ generatedIdCallback.accept(db.getDialect().getColumnValue(rs, 1)); }else{ throw new IllegalStateException("No generated key returned after the execution of sql : " + sql); } }finally{ JDBC.closeResultSetOnly(rs); } } }
@Override public DbSchemaName[] readSchemaNames(Connection connection) { ResultSet rs = null; try{ rs = getSchemas(connection, connection.getMetaData()); List<DbSchemaName> schemas = new ArrayList<DbSchemaName>(); while(rs.next()){ String catalog = getSchemaCatalog(rs); String schema = getSchemaName(rs); if(!dialect.isSystemSchema(schema)){ schemas.add(new DbSchemaName(catalog, schema)); } } return schemas.toArray(new DbSchemaName[schemas.size()]); }catch(SQLException e){ throw new NestedSQLException("Error reading schema names : " + e.getMessage(),e); }finally{ JDBC.closeResultSetAndStatement(rs); } }
public static void closeStatementAndConnection(Statement statement){ if(null != statement){ Connection conn = null; try{ conn = statement.getConnection(); }catch(Throwable e) { ; } try { statement.close(); } catch (Throwable e) { log.warn("Error closing statement : {} ",e.getMessage(),e); }finally{ closeConnection(conn); } } }
public static void closeResultSetAndStatement(ResultSet rs) { Statement stmt = null; if(null != rs){ try { stmt = rs.getStatement(); } catch (Throwable e) { log.error(e); } try { rs.close(); } catch (Throwable e) { log.warn("Error closing result set : {} ",e.getMessage(),e); } finally { closeStatementOnly(stmt); } } }
throw new NestedSQLException(e, metadata.getProductName()); }finally{ JDBC.closeResultSetOnly(rs); JDBC.closeStatementOnly(ps);
protected List<DbTableBuilder> readAllTables(Connection connection,DatabaseMetaData dm,MetadataParameters params) throws SQLException { ResultSet rs = null; try{ List<DbTableBuilder> tables = new ArrayList<>(); rs = getTables(connection, dm, params); if(null != rs){ while ( rs.next() ) { String tableCatalog = getTableCatalog(params, rs); String tableSchema = getTableSchema(params, rs); String tableName = rs.getString(TABLE_NAME); if(Strings.isEmpty(params.schema) || params.schema.equalsIgnoreCase(tableSchema)){ DbTableBuilder table = new DbTableBuilder(tableCatalog,tableSchema,tableName); readTableProperties(table, rs); tables.add(table); } } } return tables; }finally{ JDBC.closeResultSetOnly(rs); } }
void closeReal() { if(null != conn) { JDBC.closeConnection(conn); conn = null; if(log.isDebugEnabled()) { String state = pool.getStateInfo(); log.debug("Underlying connection closed! {} : [{}]", pool.getName(), state); } } }
protected boolean testDriverSupportsGetParameterType() { return db.executeWithResult(connection -> { PreparedStatement ps = null; try { ps = connection.prepareStatement(getTestDriverSupportsGetParameterTypeSQL()); ps.getParameterMetaData().getParameterType(1); return true; } catch (SQLException e) { log.debug("JDBC 3.0 getParameterType call not supported, message : {}", e.getMessage()); return false; } finally { JDBC.closeStatementOnly(ps); } }); }
protected List<DbSequence> readAllSequences(Connection connection,DatabaseMetaData dm,MetadataParameters params) throws SQLException { ResultSet rs = null; try{ List<DbSequenceBuilder> sequences = new ArrayList<DbSequenceBuilder>(); rs = getSequences(connection, dm, params); if(rs != null){ while(rs.next()){ String sequenceCatalog = getSeqCatalog(params, rs); String sequenceSchema = getSeqSchema(params, rs); String sequenceName = rs.getString(SEQUENCE_NAME); if(Strings.isEmpty(params.schema) || params.schema.equalsIgnoreCase(sequenceSchema)){ DbSequenceBuilder sequence = new DbSequenceBuilder(sequenceName).setCatalog(sequenceCatalog).setSchema(sequenceSchema); if(readSequenceProperties(sequence, rs)){ sequences.add(sequence); } } } } return Builders.buildList(sequences); }finally{ JDBC.closeResultSetOnly(rs); } }
@Override public void execute(ConnectionCallback callback) throws NestedSQLException { try { DataSource contextDataSource = getContextDataSource(); if(null == contextDataSource && null != tp) { tp.execute(callback); }else{ Connection conn = null; try { conn = null != contextDataSource ? contextDataSource.getConnection() : dataSource.getConnection(); callback.execute(conn); }catch(SQLException e) { throw new NestedSQLException(e, metadata.getProductName()); }finally{ JDBC.closeConnection(conn); } } } finally { SqlExecutionContext.clean(); } }
throw new NestedSQLException("Error executing this execution : " + e.getMessage(), e); }finally{ JDBC.closeStatementOnly(stmt);
JDBC.closeResultSetOnly(rs); JDBC.closeResultSetOnly(rs);
throw new NestedSQLException("Error creating Db instance : " + e.getMessage(),e); }finally{ JDBC.closeConnection(connection);
throw new NestedSQLException(e, metadata.getProductName()); }finally{ JDBC.closeStatementOnly(ps);