Example usage for java.sql SQLException addSuppressed

List of usage examples for java.sql SQLException addSuppressed

Introduction

In this page you can find the example usage for java.sql SQLException addSuppressed.

Prototype

public final synchronized void addSuppressed(Throwable exception) 

Source Link

Document

Appends the specified exception to the exceptions that were suppressed in order to deliver this exception.

Usage

From source file:com.frameworkset.commons.dbcp2.PoolableConnectionFactory.java

protected void initializeConnection(Connection conn) throws SQLException {
    Collection<String> sqls = _connectionInitSqls;
    if (conn.isClosed()) {
        throw new SQLException("initializeConnection: connection closed");
    }/*from   w ww . ja v  a 2 s.  c o  m*/
    if (null != sqls) {
        Statement stmt = null;
        SQLException localThrowable2 = null;
        try {
            stmt = conn.createStatement();
            for (String sql : sqls) {
                if (sql == null) {
                    throw new NullPointerException("null connectionInitSqls element");
                }
                stmt.execute(sql);
            }
        } catch (SQLException localThrowable1) {
            localThrowable2 = localThrowable1;
            throw localThrowable1;
        } catch (Throwable localThrowable1) {

            SQLException localThrowable1_ = new SQLException(localThrowable1);
            localThrowable2 = localThrowable1_;
            throw localThrowable1_;
        } finally {
            if (stmt != null) {
                if (localThrowable2 != null) {
                    try {
                        stmt.close();
                    } catch (Throwable x2) {
                        localThrowable2.addSuppressed(x2);
                    }
                } else {
                    stmt.close();
                }
            }
        }
    }
}

From source file:org.protempa.backend.dsb.relationaldb.RelationalDbDataSourceBackend.java

@Override
public void deleteAllKeys() throws DataSourceWriteException {
    if (isInKeySetMode()) {
        try {/*from w  w w . jav  a  2  s .  c om*/
            ConnectionSpec connectionSpecInstance = getConnectionSpecInstance();
            try (Connection con = connectionSpecInstance.getOrCreate()) {
                try (Statement stmt = con.createStatement()) {
                    StringBuilder stmtBuilder = new StringBuilder();
                    stmtBuilder.append("DELETE FROM ");
                    if (getKeyLoaderKeyIdSchema() != null) {
                        stmtBuilder.append(getKeyLoaderKeyIdSchema());
                        stmtBuilder.append('.');
                    }
                    stmtBuilder.append(getKeyLoaderKeyIdTable());
                    stmt.execute(stmtBuilder.toString());
                    con.commit();
                } catch (SQLException sqlex) {
                    try {
                        con.rollback();
                    } catch (SQLException ignore) {
                        sqlex.addSuppressed(ignore);
                    }
                }
            }
        } catch (InvalidConnectionSpecArguments | SQLException ex) {
            throw new DataSourceWriteException(
                    "Could not delete all key ids in data source backend " + nameForErrors(), ex);
        }
    } else {
        SQLGenUtil.logger().log(Level.FINER,
                "Unable to delete all keys in keyIdSchema{0}, keyIdTable={1} and keyIdColumn={2}",
                new Object[] { this.keyLoaderKeyIdSchema, this.defaultKeyIdTable, this.defaultKeyIdColumn });
    }
}

From source file:org.protempa.backend.dsb.relationaldb.RelationalDbDataSourceBackend.java

@Override
public void writeKeys(Set<String> keyIds) throws DataSourceWriteException {
    if (isInKeySetMode()) {
        int batchSize = 1000;
        int commitSize = 10000;
        try {/*w ww .ja  v a  2 s . c o  m*/
            ConnectionSpec connectionSpecInstance = getConnectionSpecInstance();

            try (Connection con = connectionSpecInstance.getOrCreate()) {
                try {
                    int i = 0;
                    List<String> subKeyIds = new ArrayList<>(batchSize);
                    String stmt = buildWriteKeysInsertStmt(batchSize);
                    SQLGenUtil.logger().log(Level.FINER, "Statement for writing keys: {0}", stmt);
                    try (PreparedStatement prepareStatement = con.prepareStatement(stmt)) {
                        for (String keyId : keyIds) {
                            subKeyIds.add(keyId);
                            if (++i % batchSize == 0) {
                                for (int j = 0, n = subKeyIds.size(); j < n; j++) {
                                    prepareStatement.setObject(j + 1, subKeyIds.get(j));
                                }
                                prepareStatement.execute();
                            }
                            if (i >= commitSize) {
                                con.commit();
                                commitSize = 0;
                            }
                        }
                    }
                    if (!subKeyIds.isEmpty()) {
                        stmt = buildWriteKeysInsertStmt(subKeyIds.size());
                        SQLGenUtil.logger().log(Level.FINER, "Statement for writing keys: {0}", stmt);
                        i = 0;
                        try (PreparedStatement prepareStatement = con.prepareStatement(stmt)) {
                            for (String subKeyId : subKeyIds) {
                                prepareStatement.setObject(++i, subKeyId);
                            }
                            prepareStatement.execute();
                        }
                    }
                    if (i >= commitSize) {
                        con.commit();
                        commitSize = 0;
                    }
                } catch (SQLException ex) {
                    if (commitSize > 0) {
                        try {
                            con.rollback();
                        } catch (SQLException ignore) {
                            ex.addSuppressed(ignore);
                        }
                    }
                }
            }
        } catch (InvalidConnectionSpecArguments | SQLException ex) {
            throw new DataSourceWriteException(
                    "Could not write key ids in data source backend " + nameForErrors(), ex);
        }
    } else {
        SQLGenUtil.logger().log(Level.FINER,
                "Unable to write keys to keyIdSchema{0}, keyIdTable={1} and keyIdColumn={2}",
                new Object[] { this.keyLoaderKeyIdSchema, this.defaultKeyIdTable, this.defaultKeyIdColumn });
    }
}