Example usage for org.springframework.jdbc.support JdbcUtils closeConnection

List of usage examples for org.springframework.jdbc.support JdbcUtils closeConnection

Introduction

In this page you can find the example usage for org.springframework.jdbc.support JdbcUtils closeConnection.

Prototype

public static void closeConnection(@Nullable Connection con) 

Source Link

Document

Close the given JDBC Connection and ignore any thrown exception.

Usage

From source file:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findCoffee(int coffeeId, String[] coffeeDescription) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try {//from  ww  w  .j  a va2  s  . c o m
        connection = DriverManager.getConnection("jdbc:default:connection");
        String sql = "SELECT * FROM COFFEE_BEVERAGES WHERE ID = ? ";
        statement = connection.prepareStatement(sql);
        statement.setLong(1, coffeeId);

        ResultSet resultset = statement.executeQuery();
        resultset.next();
        coffeeDescription[0] = resultset.getString("COFFEE_DESCRIPTION");

    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeConnection(connection);
    }

}

From source file:de.visualdependencies.plugin.impl.DefaultConnectionProviderImpl.java

@Override
public void closeConnection(final Connection connection) {
    JdbcUtils.closeConnection(connection);
}

From source file:org.reusables.dbunit.handler.DataSourceOperationHandler.java

/**
 * @param task The task to handle.//from  w  ww  . j a  v a 2 s.  c o  m
 * @return False if the {@link DataSource} bean was not present.
 */
@Override
public boolean handleOperation(final JdbcTask task) {
    final DataSource dataSource = getDataSource();

    if (dataSource != null) {
        Connection connection = null;

        try {
            connection = dataSource.getConnection();
            task.execute(connection);
            connection.close();
            return true;
        } catch (final SQLException e) {
            throw new RuntimeException("Error executing JDBC task.", e);
        } finally {
            JdbcUtils.closeConnection(connection);
        }
    }
    return false;
}

From source file:com.emc.ecs.sync.service.RowIterator.java

@Override
public void close() {
    JdbcUtils.closeResultSet(rs);
    JdbcUtils.closeStatement(st);
    JdbcUtils.closeConnection(con);
}

From source file:org.paxml.table.jdbc.JdbcTable.java

public void close(boolean closeConnection) {
    JdbcUtils.closeResultSet(rs);// w w  w.j a v  a  2s  .co  m
    rs = null;
    JdbcUtils.closeStatement(ps);
    ps = null;
    if (closeConnection) {
        JdbcUtils.closeConnection(con);
    }
    con = null;
}

From source file:net.firejack.platform.model.upgrader.operator.AbstractOperator.java

/**
 * Executor method which execute sql commands
 *
 * @param type - operator class//from   ww w . j a v  a2  s . com
 */
public void execute(T type) {
    try {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(true);
            PreparedStatement statement = null;
            try {
                String[] sqlCommands = sqlCommands(type);
                for (String sqlCommand : sqlCommands) {
                    logger.info("Execute sql: \n" + sqlCommand);
                    statement = connection.prepareStatement(sqlCommand);
                    statement.executeUpdate();
                }
            } finally {
                JdbcUtils.closeStatement(statement);
            }
        } finally {
            connection.setAutoCommit(false);
            JdbcUtils.closeConnection(connection);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.thinkbiganalytics.server.KyloUpgradeDatabaseVersionChecker.java

/**
 * Query the Database for the Kylo Version
 *
 * @return the KyloVersion from the database, or null if not found or if an error occurs
 *///from   w  w  w  .  j a  v a  2 s  .c  o m
public KyloVersion getDatabaseVersion() {
    KyloVersion version = null;
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
        String user = environmentProperties.getPropertyValueAsString("spring.datasource.username");
        String password = environmentProperties.getPropertyValueAsString("spring.datasource.password");
        password = encryptionService.isEncrypted(password) ? encryptionService.decrypt(password) : password;
        String uri = environmentProperties.getPropertyValueAsString("spring.datasource.url");
        String driverClassName = environmentProperties
                .getPropertyValueAsString("spring.datasource.driverClassName");
        boolean testOnBorrow = BooleanUtils
                .toBoolean(environmentProperties.getPropertyValueAsString("spring.datasource.testOnBorrow"));
        String validationQuery = environmentProperties.getPropertyValueAsString("spring.data.validationQuery");

        PoolingDataSourceService.DataSourceProperties dataSourceProperties = new PoolingDataSourceService.DataSourceProperties(
                user, password, uri, driverClassName, testOnBorrow, validationQuery);

        DataSource dataSource = PoolingDataSourceService.getDataSource(dataSourceProperties);

        connection = dataSource.getConnection();
        String query = "SELECT MAJOR_VERSION,MINOR_VERSION FROM kylo.KYLO_VERSION ";
        statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            String majorVersion = rs.getString("MAJOR_VERSION");
            String minorVersion = rs.getString("MINOR_VERSION");
            version = new KyloVersionUtil.Version(majorVersion, minorVersion);
        }

    } catch (SQLException e) {
        // this is ok.. If an error happens assume the upgrade is needed.  The method will return a null value if errors occur and the upgrade app will start.
    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeResultSet(resultSet);
        JdbcUtils.closeConnection(connection);
    }
    return version;

}

From source file:com.thinkbiganalytics.server.upgrade.KyloUpgradeDatabaseVersionChecker.java

/**
 * Query the Database for the Kylo Version
 *
 * @return the KyloVersion from the database, or null if not found or if an error occurs
 *//*w  w w  .  j a  v  a  2 s .  co  m*/
public KyloVersion getDatabaseVersion() {
    KyloVersion version = null;
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
        String user = environmentProperties.getPropertyValueAsString("spring.datasource.username");
        String password = environmentProperties.getPropertyValueAsString("spring.datasource.password");
        password = encryptionService.isEncrypted(password) ? encryptionService.decrypt(password) : password;
        String uri = environmentProperties.getPropertyValueAsString("spring.datasource.url");
        String driverClassName = environmentProperties
                .getPropertyValueAsString("spring.datasource.driverClassName");
        boolean testOnBorrow = BooleanUtils
                .toBoolean(environmentProperties.getPropertyValueAsString("spring.datasource.testOnBorrow"));
        String validationQuery = environmentProperties
                .getPropertyValueAsString("spring.datasource.validationQuery");

        PoolingDataSourceService.DataSourceProperties dataSourceProperties = new PoolingDataSourceService.DataSourceProperties(
                user, password, uri, driverClassName, testOnBorrow, validationQuery);

        DataSource dataSource = PoolingDataSourceService.getDataSource(dataSourceProperties);

        connection = dataSource.getConnection();
        String query = "SELECT * FROM KYLO_VERSION ORDER BY MAJOR_VERSION DESC, MINOR_VERSION DESC, POINT_VERSION DESC, TAG DESC";
        statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        if (rs.next()) {
            String majorVersion = rs.getString("MAJOR_VERSION");
            String minorVersion = rs.getString("MINOR_VERSION");
            String pointVersion = rs.getString("POINT_VERSION");
            String tag = rs.getString("TAG");

            version = new KyloVersionUtil.Version(majorVersion, minorVersion, pointVersion, tag);
        }

    } catch (SQLException e) {
        // this is ok.. If an error happens assume the upgrade is needed.  The method will return a null value if errors occur and the upgrade app will start.
        e.printStackTrace();
    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeResultSet(resultSet);
        JdbcUtils.closeConnection(connection);
    }
    return version;

}

From source file:com.scistor.queryrouter.server.impl.JdbcHandlerImpl.java

@Override
public Map<String, String> queryForMeta(String tableName) {
    long begin = System.currentTimeMillis();
    Map<String, String> result = Maps.newConcurrentMap();
    Connection conn = null;/*w  w  w  .  ja  va 2s  .  c  om*/
    PreparedStatement pst = null;
    try {
        conn = this.getJdbcTemplate().getDataSource().getConnection();
        DatabaseMetaData dbMetaData = conn.getMetaData();
        if (StringUtils.isNotEmpty(tableName)) {
            pst = conn.prepareStatement(String.format("select * from %s where 1=2", tableName));
            ResultSetMetaData rsd = pst.executeQuery().getMetaData();
            for (int i = 0; i < rsd.getColumnCount(); i++) {
                result.put(rsd.getColumnName(i + 1), rsd.getColumnTypeName(i + 1));
            }
        }
    } catch (SQLException e1) {
        logger.error("queryId:{} select meta error:{}", 1, e1.getCause().getMessage());
    } finally {
        JdbcUtils.closeConnection(conn);
        JdbcUtils.closeStatement(pst);
        logger.info("queryId:{} select meta cost:{} ms resultsize:{}", 1, System.currentTimeMillis() - begin,
                result.size());
    }
    return result;
}

From source file:net.firejack.platform.core.config.upgrader.SchemaUpgrader.java

/**
 * This method returns current package version by package lookup
 *
 * @param packageLookup//from   ww w . ja v a  2 s  .  c  om
 * @return version
 */
private Version getDatabaseVersion(String packageLookup) {
    try {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(true);

            PreparedStatement statement = connection.prepareStatement("SELECT database_version "
                    + "FROM opf_registry_node " + "WHERE lookup = '" + packageLookup + "'");
            try {
                ResultSet resultSet = statement.executeQuery();
                try {
                    if (resultSet.next())
                        return new Version(resultSet.getInt(1));
                    else
                        throw new BusinessFunctionException("Can't find database version number.");
                } finally {
                    JdbcUtils.closeResultSet(resultSet);
                }
            } catch (SQLException e) {
                if (1054 == e.getErrorCode()) {
                    return new Version(1);
                }
                throw new RuntimeException(e);
            } finally {
                JdbcUtils.closeStatement(statement);
            }
        } finally {
            connection.setAutoCommit(false);
            JdbcUtils.closeConnection(connection);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}