Example usage for java.sql Connection clearWarnings

List of usage examples for java.sql Connection clearWarnings

Introduction

In this page you can find the example usage for java.sql Connection clearWarnings.

Prototype

void clearWarnings() throws SQLException;

Source Link

Document

Clears all warnings reported for this Connection object.

Usage

From source file:org.artifactory.storage.db.spring.ArtifactoryPoolableConnectionFactory.java

@Override
public void passivateObject(Object obj) throws Exception {
    if (obj instanceof Connection) {
        Connection conn = (Connection) obj;
        if (!conn.getAutoCommit() && !conn.isReadOnly()) {
            conn.rollback();// w  w  w.  j av  a2  s.  co m
        }
        conn.clearWarnings();
        //if(!conn.getAutoCommit()) {
        //    conn.setAutoCommit(true);
        //}
    }
    if (obj instanceof DelegatingConnection) {
        Method passivateMethod = ReflectionUtils.findMethod(DelegatingConnection.class, "passivate");
        passivateMethod.setAccessible(true);
        ReflectionUtils.invokeMethod(passivateMethod, obj);
    }
}

From source file:org.pentaho.platform.engine.services.audit.AuditConnection.java

public Connection getAuditConnection() throws SQLException {
    SQLException sqlEx = null;/*from w  ww .  jav  a 2 s. co m*/
    int[] sleepTime = { 0, 200, 500, 2000 };
    for (int i = 0; i <= 3; i++) {
        waitFor(sleepTime[i]);
        try {
            Connection con = getConnection();
            try {
                con.clearWarnings();
            } catch (SQLException ex) {
                //ignored
            }
            return con;
        } catch (SQLException ex) {
            sqlEx = ex;
            AuditConnection.logger.warn(Messages.getInstance()
                    .getErrorString("AuditConnection.WARN_0001_CONNECTION_ATTEMPT_FAILED", "" //$NON-NLS-1$ //$NON-NLS-2$
                            + sleepTime[i]));
        }
    }
    throw new SQLException(Messages.getInstance().getErrorString("AUDSQLENT.ERROR_0001_INVALID_CONNECTION"), //$NON-NLS-1$
            sqlEx);
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

@Test
public void testConnectionPoolConnFunctions() throws SQLException {
    String tableName = RandomUtil.getIdentifier("table");
    tableName = DBTable.escapeIdentifier(tableName);

    String sql1 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n"
            + "drop table " + tableName + "\n" + "create table " + tableName + "\n" + "(\n"
            + "wibble_id int primary key not null,\n" + "counter int null\n" + ");";
    String sql2 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n"
            + "drop table " + tableName + "\n";

    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);/* w  w w  .j  ava2s  .  c  om*/

    PooledConnection pc = ds.getPooledConnection();
    Connection con = pc.getConnection();

    Statement statement = con.createStatement();
    statement.execute(sql1);
    statement.execute(sql2);
    con.clearWarnings();
    pc.close();
}

From source file:org.apache.airavata.registry.tool.DBMigrator.java

private static void executeSQL(String sql, Connection conn) throws Exception {
    if ("".equals(sql.trim())) {
        return;/*from   ww  w .j  a  v a 2  s .  c o  m*/
    }
    Statement statement = null;
    try {
        logger.debug("SQL : " + sql);

        boolean ret;
        int updateCount = 0, updateCountTotal = 0;
        statement = conn.createStatement();
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
            }
        } while (ret);

        logger.debug(sql + " : " + updateCountTotal + " rows affected");

        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            logger.warn(warning + " sql warning");
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("X0Y32")) {
            logger.info("Table Already Exists", e);
        } else {
            throw new Exception("Error occurred while executing : " + sql, e);
        }
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                logger.error("Error occurred while closing result set.", e);
            }
        }
    }
}

From source file:org.wso2.mobile.utils.persistence.EMMDBInitializer.java

/**
 * executes given sql//from   ww  w  . j  a  va  2  s.  c  om
 *
 * @param sql
 * @throws Exception
 */
private void executeSQL(String sql) throws Exception {
    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;
    }

    ResultSet resultSet = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }

        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        Connection conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            log.debug(warning + " sql warning");
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("X0Y32") || e.getSQLState().equals("42710")) {
            // eliminating the table already exception for the derby and DB2 database types
            if (log.isDebugEnabled()) {
                log.info("Table Already Exists. Hence, skipping table creation");
            }
        } else {
            throw new Exception("Error occurred while executing : " + sql, e);
        }
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing result set.", e);
            }
        }
    }
}

From source file:org.wso2.carbon.social.sql.SocialDBInitilizer.java

/**
 * executes given sql//w w  w  . j  a  v  a2s .c  o m
 *
 * @param sql
 * @throws Exception
 */
private void executeSQL(String sql) throws Exception {
    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;
    }

    ResultSet resultSet = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }

        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        Connection conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            log.debug(warning + " sql warning");
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("X0Y32") || e.getSQLState().equals("42710")) {
            // eliminating the table already exception for the derby and DB2 database types
            //if (log.isDebugEnabled()) {
            log.info("Table Already Exists. Hence, skipping table creation");
            //}
        } else {
            throw new Exception("Error occurred while executing : " + sql, e);
        }
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing result set.", e);
            }
        }
    }
}

From source file:org.apache.cayenne.datasource.UnmanagedPoolingDataSource.java

/**
 * Updates connection state to a default state.
 *///  w  ww .  j av a2  s  . c  o  m
Connection resetState(Connection c) throws SQLException {

    // TODO: tx isolation level?

    if (!c.getAutoCommit()) {

        try {
            c.setAutoCommit(true);
        } catch (SQLException e) {
            UnmanagedPoolingDataSource.sybaseAutoCommitPatch(c, e, true);
        }
    }

    c.clearWarnings();
    return c;
}

From source file:com.adaptris.jdbc.connection.FailoverDatasourceTest.java

@Test
public void testInfo() throws Exception {
    Connection conn = new MyProxy();

    try {/*  w  ww.j  av  a 2  s  .co  m*/
        try {
            conn.getMetaData();
        } catch (SQLException e) {

        }
        try {
            conn.setCatalog(conn.getCatalog());
        } catch (SQLException e) {

        }
        try {
            conn.setReadOnly(conn.isReadOnly());
        } catch (SQLException e) {

        }
        try {
            conn.setTransactionIsolation(conn.getTransactionIsolation());
        } catch (SQLException e) {

        }
        try {
            conn.setTransactionIsolation(conn.getTransactionIsolation());
        } catch (SQLException e) {

        }
        try {
            conn.getWarnings();
        } catch (SQLException e) {

        }
        try {
            conn.clearWarnings();
        } catch (SQLException e) {

        }
        try {
            conn.setHoldability(conn.getHoldability());
        } catch (SQLException e) {

        }

        try {
            conn.setSchema(conn.getSchema());
        } catch (SQLException e) {

        }

    } finally {
        JdbcUtil.closeQuietly(conn);
    }

}

From source file:org.bml.util.errorconsumer.ParseErrorWorkerThread.java

/**
 * TOOD: Add a temp ordered list to store ParseError objects as they are
 * taken from the queue and log if any rows are rejected by the DB server
 * TODO: abstract out the handleDBEntry base logic and use <T> for entry and
 * a static method for marshaling into a Prepared Statement (Consider adding
 * the marshal method to a TABLE definition object).
 *//*from   www  .j  a v  a 2s  . c  o m*/
public void handleDBEntry() {

    Connection myConnection = null;
    PreparedStatement myPreparedStatement = null;

    Connection myPageViewConnection = null;

    int batchExecutionResults[] = null;

    List<ParseError> theBatchTrackingList = new LinkedList<ParseError>();

    //DeviceType aDeviceType = null;
    //DeviceClass aDeviceClass = null;

    //Change to reusable map
    Map<String, String> tmpMap = null;

    //Change to StringBuilder 
    //String tmpString = null; 

    //theBatchTrackingList = new ArrayList<PageViewData>(dataQueue.size());
    boolean dbErrror = false;

    try {
        ParseError aParseError = null;
        try {
            aParseError = errorQueue.remove();
            theBatchTrackingList.add(aParseError);
        } catch (NoSuchElementException e) {
            LOG.info("There are no ParseError Objects to push into the DB");
            return;
        }
        StopWatch connectionAge = new StopWatch();
        connectionAge.start();
        setWorkerState(WORKER_STATE.ACQUIRING_CONNECTION);
        myConnection = DBUtil.getDefaultDataSource().getConnection();
        setWorkerState(WORKER_STATE.CONFIGURING_CONNECTION);
        myConnection.clearWarnings();
        myConnection.setAutoCommit(false);
        setWorkerState(WORKER_STATE.PREPARING_SQL);
        myPreparedStatement = myConnection.prepareStatement(ParseErrorTable.PREPARED_INSERT_SQL);
        setWorkerState(WORKER_STATE.BATCHING);

        while ((connectionAge.getTime() / 1000) <= 20) {
            ParseErrorTable.populatePreparedStatement(myPreparedStatement, aParseError.toParamMap(),
                    Boolean.FALSE);
            myPreparedStatement.addBatch();
            try {
                aParseError = errorQueue.remove();
                theBatchTrackingList.add(aParseError);
            } catch (NoSuchElementException e) {
                break;
            }
        }

        this.setWorkerState(WORKER_STATE.EXECUTING_BATCH);
        batchExecutionResults = myPreparedStatement.executeBatch();

        myConnection.commit();

        this.setWorkerState(WORKER_STATE.VERIFYING_BATCH);
        if (batchExecutionResults.length != theBatchTrackingList.size()) {

        }

    } catch (SQLException sqle) {
        if (LOG.isFatalEnabled()) {
            LOG.fatal(
                    "SQLException caught. The ErrorConsumer is unable to push data to a database. ParseErrors will be dumped to /tmp/error_consumer/",
                    sqle);
        }
    } catch (Exception e) {
        if (LOG.isFatalEnabled()) {
            LOG.fatal(
                    "Exception caught. The ErrorConsumer is unable to push data to a database. Errors will be dumped to /tmp/error_consumer/",
                    e);
        }

    } finally {
        DbUtils.closeQuietly(myPreparedStatement);
        DbUtils.closeQuietly(myConnection);
    }
}

From source file:org.wso2.carbon.identity.core.persistence.IdentityDBInitializer.java

/**
 * executes given sql/*from   ww  w  .  ja  v  a  2 s. c o m*/
 *
 * @param sql
 * @throws Exception
 */
private void executeSQL(String sql) {
    // Check and ignore empty statements
    if ("".equals(sql.trim())) {
        return;
    }

    ResultSet resultSet = null;
    Connection conn = null;

    try {
        if (log.isDebugEnabled()) {
            log.debug("SQL : " + sql);
        }

        boolean ret;
        int updateCount, updateCountTotal = 0;
        ret = statement.execute(sql);
        updateCount = statement.getUpdateCount();
        resultSet = statement.getResultSet();
        do {
            if (!ret) {
                if (updateCount != -1) {
                    updateCountTotal += updateCount;
                }
            }
            ret = statement.getMoreResults();
            if (ret) {
                updateCount = statement.getUpdateCount();
                resultSet = statement.getResultSet();
            }
        } while (ret);

        if (log.isDebugEnabled()) {
            log.debug(sql + " : " + updateCountTotal + " rows affected");
        }
        conn = dataSource.getConnection();
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            log.debug(warning + " sql warning");
            warning = warning.getNextWarning();
        }
        conn.clearWarnings();
    } catch (SQLException e) {
        if (e.getSQLState().equals("X0Y32") || e.getSQLState().equals("42710")) {
            // eliminating the table already exception for the derby and DB2 database types
            if (log.isDebugEnabled()) {
                log.info("Table Already Exists. Hence, skipping table creation");
            }
        } else {
            throw new IdentityRuntimeException("Error occurred while executing : " + sql, e);
        }
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing result set.", e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error("Error occurred while closing sql connection.", e);
            }
        }
    }
}