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:wzw.util.DbUtils.java

/**
 * Execute an SQL INSERT, UPDATE, or DELETE query without replacement
 * parameters???,? doUpdate(List list).// w  w w  .j  ava2  s. c  o  m
 *
 * @param conn The connection to use to run the query
 * @param sql The SQL to execute
 * @return The number of rows updated
 * @throws SQLException ??
 */
public static int executeUpdate(String sql, Connection conn) throws SQLException {

    //?
    if (conn == null || conn.isClosed()) {
        throw new SQLException("Connection Object is null or is closed!");
    }

    PreparedStatement pstmt = null;
    int i_returns;
    try {
        /////conn = getConnection();  //?????? 
        pstmt = conn.prepareStatement(sql);
        i_returns = pstmt.executeUpdate();
        //log.debug("pstmt.executeUpdate success!");
        return i_returns;

    } catch (SQLException e) {
        log.fatal("Result in update Exception'SQL is:\n" + sql + ". Message:" + e.getMessage(), e);
        /// log.debug("debug", e);
        /// e.pri ntStackTrace();
        /// System.out.println("Result in update Exception'SQL is:\n"+sql );
        throw e;
        //this.rethrow(e, sql, list);

    } finally {
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {
            log.fatal("DBUtils.doUpdate() Exception?\n", e);
            ///log.debug("debug", e);
            /// e.pri ntStackTrace();
            //this.addErrors(new ActionError("error.database.deal"));
        }
    }
    //return i_returns;        
}

From source file:wzw.util.DbUtils.java

/**
 * Connection ?/*from w  w  w  .j  a va 2s  . co  m*/
 * @param conn
 */
public static void closeQuietly(Connection conn) {
    try {
        log.debug(conn != null ? "not null" : "is null"); //

        if (conn != null && !conn.isClosed()) // ????
        {
            log.debug("hashcode=" + conn.hashCode() + "" + "" + showTrace(8)); //
            //log.debug(conn.isClosed()?"---a---conn isClosed":"conn not closed");
            //log.debug(conn.getClass());
            conn.close();
            //log.debug(conn.isClosed()?"---b---conn isClosed":"conn not closed");

        }
    } catch (SQLException sqle) {
        log.info("?conn?" + sqle.getMessage(), sqle);
        /// log.debug("debug", sqle );
        /// sqle.pri ntStackTrace();
    }
}

From source file:wzw.util.DbUtils.java

/**
 * ??./*from w ww  . j  a va  2s . com*/
 * @param conn ?
 * @param allsql ?sql??
 * @throws ?
 * @return ?sql???
 */
public static int[] executeBatch(List<String> list, Connection conn) throws SQLException {

    //?
    if (conn == null || conn.isClosed()) {
        throw new SQLException("Connection Object is null or is closed!");
    }

    Statement stmt = null;
    int returns[];
    boolean isConnCreated = false;
    try {
        if (conn == null || conn.isClosed()) { // ????
            conn = getConnection(); //?????? 
            isConnCreated = true;
        }

        stmt = conn.createStatement();
        addBatch(stmt, list);
        returns = stmt.executeBatch();

        if (isConnCreated) {
            conn.commit(); //?????
        }
        return returns;
        //log.debug("doUpdate commit success!");

    } catch (SQLException e) {
        if (isConnCreated) {
            conn.rollback(); //?????
        }

        String info = "Result in doBatch Exception'SQLs is:";
        for (int i = 0; i < list.size(); i++) {
            info += list.get(i).toString() + ";";
        }
        log.fatal(DbUtils.class.getName() + " " + e.getMessage() + ". " + info);
        log.debug("debug", e);
        /// e.pri ntStackTrace();
        throw e;

    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            log.fatal("DBUtils.doBatch() ?\n", e);
            log.debug("debug", e);
            /// e.pri ntStackTrace();
        }

        try {
            if (isConnCreated && conn != null)
                conn.close(); //?????

        } catch (SQLException e) {
            log.fatal("DBUtils.doBatch() ?\n", e);
            log.debug("debug", e);
            /// e.pri ntStackTrace();
        }
    }
    // return null;
}

From source file:wzw.util.DbUtils.java

/**
 * Execute an SQL SELECT query without any replacement parameters.  The
 * caller is responsible for connection cleanup.
 *
 * @param conn The connection to execute the query in.
 * @param sql The query to execute.//from w w w  . jav  a  2 s  .  c  om
 * @param rsh The handler that converts the results into an object.
 * @return The object returned by the handler.
 * @throws SQLException
 */
public static RowSet executeQuery(String sql, Connection conn) throws SQLException {

    log.debug(sql + "--1");
    //?
    if (conn == null || conn.isClosed()) {
        throw new SQLException("Connection Object is null or is closed!");
    }

    log.debug(sql + "--2");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    RowSet crs = null;

    try {
        log.debug(sql);

        /// conn = getConnection();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        //crs.populate( rs ) ;
        crs = resultSet2RowSet(((PlainConnection) conn).getDatabaseManager(), rs);

    } catch (SQLException e) {
        //this.rethrow(e, sql, params);
        log.debug("Result in Qurey Exception'SQL is:\n" + sql, e);
        /// e.pri ntStackTrace();
        throw e;

    } finally {

        DbUtils.closeQuietly(null, pstmt, rs);

    }

    return crs;
}

From source file:com.tera.common.database.dbcp.CPoolableConnectionFactory.java

@Override
public void validateConnection(Connection conn) throws SQLException {
    if (conn.isClosed())
        throw new SQLException("validateConnection: connection closed");
    if (validationTimeout >= 0 && !conn.isValid(validationTimeout))
        throw new SQLException("validateConnection: connection invalid");
}

From source file:com.aionemu.commons.database.PoolableConnectionFactoryAE.java

/**
 * Validate connection by checking if connection is not closed and is still valid. throws SQLException if connection
 * is invalid./*from ww  w . ja  v  a 2  s  . com*/
 */
@Override
public void validateConnection(Connection conn) throws SQLException {
    if (conn.isClosed())
        throw new SQLException("validateConnection: connection closed");
    if (validationTimeout >= 0 && !conn.isValid(validationTimeout))
        throw new SQLException("validateConnection: connection invalid");
}

From source file:org.castor.cpa.persistence.sql.keygen.AbstractKeyGenerator.java

/**
 * Close the JDBC Connection./*from  w  w w  .jav  a 2s .c o  m*/
 * 
 * @param conn A JDBC Connection.
 */

public final void closeSeparateConnection(final Connection conn) {
    try {
        if (!conn.isClosed()) {
            conn.close();
        }
    } catch (SQLException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:CachedConnectionServlet.java

synchronized public static void checkIn(Connection c) {
    boolean found = false;
    boolean closed = false;
    CachedConnection cached = null;//from   w w w.  j  av a2 s.  c o  m
    Connection conn = null;
    int i = 0;

    if (verbose) {
        System.out.println("Searching for connection to set not in use...");
    }
    for (i = 0; !found && i < numberConnections; i++) {
        if (verbose) {
            System.out.println("Vector entry " + Integer.toString(i));
        }
        cached = (CachedConnection) cachedConnections.get(i);
        conn = cached.getConnection();
        if (conn == c) {
            if (verbose) {
                System.out.println("found cached entry " + Integer.toString(i));
            }
            found = true;
        }
    }
    if (found) {
        try {
            closed = conn.isClosed();
        } catch (SQLException ignore) {
            closed = true;
        }
        if (!closed)
            cached.setInUse(false);
        else {
            cachedConnections.remove(i);
            numberConnections--;
        }
    } else if (verbose) {
        System.out.println("In use Connection not found!!!");
    }
}

From source file:hermes.store.jdbc.JDBCConnectionPool.java

public boolean beforeCheckin(Connection connection) {
    try {/*www. j  av  a 2s.c o  m*/
        if (connection.isClosed()) {
            return false;
        } else {
            if (!connection.getAutoCommit()) {
                connection.rollback();
            }
        }
    } catch (SQLException ex) {
        log.warn("beforeCheckin failed:" + ex.getMessage(), ex);
        return false;
    }

    return true;
}

From source file:org.apache.phoenix.hive.HivePhoenixOutputCommitter.java

public void commit(Connection connection) throws IOException {
    try {// w w w . ja  v  a2s .  c  om
        if ((connection == null) || (connection.isClosed()))
            throw new IOException("Trying to commit a connection that is null or closed: " + connection);
    } catch (SQLException e) {
        throw new IOException("Exception calling isClosed on connection", e);
    }
    try {
        this.LOG.info("Commit called on task completion");
        connection.commit();
    } catch (SQLException e) {
        throw new IOException("Exception while trying to commit a connection. ", e);
    } finally {
        try {
            this.LOG.info("Closing connection to database on task completion");
            connection.close();
        } catch (SQLException e) {
            this.LOG.warn("Exception while trying to close database connection", e);
        }
    }
}