Example usage for java.sql Connection isClosed

List of usage examples for java.sql Connection isClosed

Introduction

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

Prototype

boolean isClosed() throws SQLException;

Source Link

Document

Retrieves whether this Connection object has been closed.

Usage

From source file:com.vertica.hadoop.AbstractVerticaOutputCommitter.java

/**
 * Rolls back and closes the connection if the connection is non-null and open.
 * @throws IOException if either isClosed or rollback fails.
 *///from  w ww . j  a v a  2  s  .  c  o m
protected final void sqlRollback(Connection connection) throws IOException {

    try {
        if (connection == null || connection.isClosed()) {
            throw new IOException("Trying to rollback a connection that is null or closed: " + connection);
        }
    } catch (SQLException e) {
        throw new IOException("Exception calling isClosed on connection", e);
    }

    try {
        connection.rollback();
    } catch (SQLException e) {
        throw new IOException("Exception rolling back connection on table", e);
    } finally {
        sqlClose(connection);
    }
}

From source file:org.mybatis.spring.SqlSessionTemplateTest.java

@Test
public void testGetConnectionInTx() throws java.sql.SQLException {
    TransactionStatus status = null;/* w  ww . j av a2 s  .c om*/

    try {
        status = txManager.getTransaction(new DefaultTransactionDefinition());

        java.sql.Connection con = sqlSessionTemplate.getConnection();

        assertFalse(con.isClosed());

    } finally {
        // rollback required to close connection
        txManager.rollback(status);
    }
}

From source file:org.apache.ddlutils.util.JdbcSupport.java

/**
 * Closes the given JDBC connection (returns it back to the pool if the datasource is poolable).
 * /*from  ww  w  .  j ava  2s  .  c o m*/
 * @param connection The connection
 */
public void returnConnection(Connection connection) {
    try {
        if ((connection != null) && !connection.isClosed()) {
            if (_log.isDebugEnabled()) {
                String connName = connection.toString();

                _openConnectionNames.remove(connName);

                StringBuffer logMsg = new StringBuffer();

                logMsg.append("Returning connection ");
                logMsg.append(connName);
                logMsg.append(" to data source.\nRemaining connections:");
                if (_openConnectionNames.isEmpty()) {
                    logMsg.append(" None");
                } else {
                    for (Iterator it = _openConnectionNames.iterator(); it.hasNext();) {
                        logMsg.append("\n    ");
                        logMsg.append(it.next().toString());
                    }
                }
                _log.debug(logMsg.toString());
            }
            connection.close();
        }
    } catch (Exception e) {
        _log.warn("Caught exception while returning connection to pool", e);
    }
}

From source file:kenh.xscript.database.elements.Rollback.java

public void process(@Attribute(ATTRIBUTE_REF) java.sql.Connection conn,
        @Attribute(ATTRIBUTE_SAVE_POINT) java.sql.Savepoint savepoint) throws UnsupportedScriptException {
    try {//from  w w  w  .j  av  a 2 s  .  c o  m
        if (!conn.isClosed()) {
            if (!conn.getAutoCommit()) {
                if (savepoint != null) {
                    conn.rollback(savepoint);
                } else {
                    conn.rollback();
                }
            }
        }
    } catch (Exception e) {
        throw new UnsupportedScriptException(this, e);
    }
}

From source file:org.darkphoenixs.pool.jdbc.JdbcConnectionFactory.java

@Override
public boolean validateObject(PooledObject<Connection> p) {

    Connection connection = p.getObject();

    if (connection != null)
        try {/*from  www.j a  v a 2s .com*/
            return ((!connection.isClosed()) && (connection.isValid(1)));
        } catch (SQLException e) {
            e.printStackTrace();
        }

    return false;
}

From source file:org.mule.tck.util.MuleDerbyTestDatabase.java

/**
 * Executes an SQL statement against the database.
 * For queries use execSqlQuery.// www.ja v  a2  s. c  o  m
 *
 * @param sql SQL query
 * @return
 * @throws Exception
 */
public int execSqlUpdate(String sql) throws Exception {
    Connection con = null;
    try {
        con = getConnection();
        return new QueryRunner().update(con, sql);
    } finally {
        if (con != null && !con.isClosed()) {
            con.close();
        }
    }
}

From source file:org.apache.hive.beeline.TestBeeLineWithArgs.java

/**
 * Create table for use by tests//  www .  j  ava  2s. c  o m
 * @throws ClassNotFoundException
 * @throws SQLException
 */
private static void createTable() throws ClassNotFoundException, SQLException {
    Class.forName(BeeLine.BEELINE_DEFAULT_JDBC_DRIVER);
    Connection con = DriverManager.getConnection(miniHS2.getBaseJdbcURL(), userName, "");

    assertNotNull("Connection is null", con);
    assertFalse("Connection should not be closed", con.isClosed());
    Statement stmt = con.createStatement();
    assertNotNull("Statement is null", stmt);

    stmt.execute("set hive.support.concurrency = false");

    HiveConf conf = new HiveConf();
    String dataFileDir = conf.get("test.data.files").replace('\\', '/').replace("c:", "");
    Path dataFilePath = new Path(dataFileDir, "kv1.txt");
    // drop table. ignore error.
    try {
        stmt.execute("drop table " + tableName);
    } catch (Exception ex) {
        fail(ex.toString() + " " + ExceptionUtils.getStackTrace(ex));
    }

    // create table
    stmt.execute("create table " + tableName
            + " (under_col int comment 'the under column', value string) comment '" + tableComment + "'");

    // load data
    stmt.execute("load data local inpath '" + dataFilePath.toString() + "' into table " + tableName);
}

From source file:org.freebxml.omar.server.persistence.rdb.ConnectionPool.java

private boolean isConnectionOK(Connection connection) {
    Statement testStmt = null;/* w w  w .  j ava2 s .c o  m*/

    try {
        if (!connection.isClosed()) {
            // Try to createStatement to see if it's really alive
            testStmt = connection.createStatement();
            testStmt.close();
        } else {
            return false;
        }
    } catch (SQLException e) {
        if (testStmt != null) {
            try {
                testStmt.close();
            } catch (SQLException se) {
            }
        }

        return false;
    }

    return true;
}

From source file:org.callimachusproject.sql.PoolableDriverConnectionFactory.java

public PoolableDriverConnection makeObject() throws Exception {
    Connection conn = factory.createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }/*from ww w  . j  a va2 s  .c om*/
    if (conn.isClosed()) {
        throw new SQLException("connection closed");
    }
    return new PoolableDriverConnection(conn, pool);
}

From source file:org.wso2.carbon.das.jobmanager.core.impl.RDBMSServiceImpl.java

/**
 * Close the database connection.// w  w  w.  j a v  a  2s . c  om
 *
 * @param connection The connection to be closed
 * @param task       The task which was running
 */
private void close(Connection connection, String task) {
    try {
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
    } catch (SQLException e) {
        log.error("Failed to close connection after " + task, e);
    }
}