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:server.LobbyHandler.java

private static Connection getConnection() {

    Connection con = null;
    try {/*from  w w  w. jav a  2s.c  o  m*/
        //Class.forName("com.mysql.jdbc.Driver").newInstance();
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/setgame", "root", "betamobile");
        if (con.isClosed()) {
            System.out.println("mySQL is closed");
        }
    } catch (Exception e) {
        System.out.println("could not connect to mySQL");
        System.err.println(e);

    }
    return con;
}

From source file:com.tesora.dve.common.DBHelper.java

@SuppressWarnings("resource")
public static Charset getConnectionCharset(final DBHelper dbHelper) throws SQLException {
    final Connection connection = dbHelper.getConnection();
    if ((connection != null) && !connection.isClosed()) {
        try (final ResultSet rs = connection.createStatement()
                .executeQuery("SHOW VARIABLES WHERE Variable_name = 'character_set_connection'")) {
            rs.last();//from   w  w  w.  j av a  2 s  . c  om
            final String connectionCharsetName = rs.getString(2);
            return Charset.forName(PEStringUtils.dequote(connectionCharsetName));
        }
    }

    throw new PECodingException("Database connection is not open.");
}

From source file:com.weibo.datasys.common.db.DBManager.java

/**
 * //from  ww w .j a  va2 s  . c o  m
 * ??
 * 
 * @param conn
 * @param ps
 */
public static void releaseConnection(Connection conn, PreparedStatement ps) {
    if (ps != null) {
        try {
            ps.close();
        } catch (Exception e) {
            logger.error("[ClosePSError] - ", e);
        }
    }
    if (conn != null) {
        try {
            if (!conn.isClosed())
                conn.close();
        } catch (Exception e) {
            logger.error("[CloseConnectionError] - ", e);
        }
    }
}

From source file:org.ralasafe.util.DBUtil.java

public static void close(Connection conn) {
    if (conn != null) {
        try {//from  w ww . j  a va  2 s .  c  om
            if (!conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            conn = null;
        }
    }
}

From source file:org.apache.synapse.commons.transaction.TranscationManger.java

public static Connection addConnection(final DataSource ds) throws Exception {
    long key = Thread.currentThread().getId();
    Connection conn = getConnection();
    if (conn != null) {
        log.debug(" Connection can get from map : " + key);
        return conn;
    }//w w w.j a v  a2 s . c om

    int count = 0;
    do {
        conn = ds.getConnection();
        Connection actual = ((javax.sql.PooledConnection) conn).getConnection();
        if (conn == null || actual == null) {
            continue;
        }
        if (!TranscationManger.checkConnectionAlreadyUse(conn) && !actual.isClosed()) {
            if (!connections.containsKey(key)) {
                connections.putIfAbsent(key, new ConnectionMapper(conn));
                log.debug(" Connection added to map in attempt : " + count + " Thread : " + key);
            }
            break;
        } else {
            conn.close();
            conn = null;
            Thread.sleep(500l);
            continue;
        }
    } while (++count < 5);

    if (conn == null && count >= 5) {
        throw new Exception(" Not enough Connections in the pool, Cache size : " + connections.size());
    }
    return conn;

}

From source file:com.tacitknowledge.util.migration.jdbc.util.SqlUtil.java

/**
 * Ensures the given connection, statement, and result are properly closed.
 *
 * @param conn the connection to close; may be <code>null</code>
 * @param stmt the statement to close; may be <code>null</code>
 * @param rs   the result set to close; may be <code>null</code>
 */// w  w w.  j av a 2  s. c o  m
public static void close(Connection conn, Statement stmt, ResultSet rs) {
    if (rs != null) {
        try {
            log.debug("Closing ResultSet: " + rs.toString());
            rs.close();
        } catch (SQLException e) {
            log.error("Error closing ResultSet", e);
        }
    }

    if (stmt != null) {
        try {
            log.debug("Closing Statement: " + stmt.toString());
            stmt.close();
        } catch (SQLException e) {
            log.error("Error closing Statement", e);
        }
    }

    if (conn != null) {
        try {
            if (!conn.isClosed()) {
                log.debug("Closing Connection " + conn.toString());
                conn.close();
            } else {
                log.debug("Connection (" + conn.toString() + ") already closed.");
            }
        } catch (SQLException e) {
            log.error("Error closing Connection", e);
        }
    }
}

From source file:org.apache.synapse.commons.transaction.TranscationManger.java

public static void bindConnection(final Connection conn) throws Exception {
    long key = Thread.currentThread().getId();
    try {//  ww  w .  java2 s .  c  o m
        if (conn instanceof XAConnection) {
            Transaction tx = transactions.get().get(key);
            XAResource xaRes = ((XAConnection) conn).getXAResource();

            if (!isXAResourceEnlisted(xaRes)) {
                tx.enlistResource(xaRes);
                addToEnlistedXADataSources(xaRes, key);
                log.debug(" DS enlisted in thread " + key + " XA Resource : " + xaRes.hashCode());
            }
        }

    } catch (Exception ex) {
        StringBuilder logMsg = new StringBuilder();
        Connection actual = ((javax.sql.PooledConnection) conn).getConnection();
        logMsg.append(" Thread Id : " + key)
                .append(" BIND ERROR , Transaction Manager status : " + txManagers.get().get(key).getStatus())
                .append("\n")
                .append(" BIND ERROR , Transaction status : " + transactions.get().get(key).getStatus())
                .append("\n").append(" JDBC Connection status : " + actual.isClosed()).append("\n")
                .append(" BIND ERROR  : " + ex);
        log.error(logMsg.toString());
        rollbackTransaction(true, key);
        throw ex;
    }
}

From source file:com.stratelia.webactiv.util.DBUtil.java

public static void rollback(Connection connection) {
    if (connection != null) {
        try {//from w w w.  j  av  a2s .c  o m
            if (!connection.getAutoCommit() && !connection.isClosed()) {
                connection.rollback();
            }
        } catch (SQLException e) {
            SilverTrace.error("util", "DBUtil.close", "util.CAN_T_ROLLBACK_CONNECTION", e);
        }
    }
}

From source file:wzw.util.DbUtils.java

/**
 * Connection ?/*from  w w w  . ja  v a 2s .c o  m*/
 * @param conn
 */
public static void rollbackQuietly(Connection conn) {
    try {
        if (conn != null && !conn.isClosed()) // ????
        {
            conn.rollback();
        }
    } catch (SQLException sqle) {
        log.info("?conn?" + sqle.getMessage(), sqle);
    }
}

From source file:wzw.util.DbUtils.java

/**
 * Connection ???//w w  w .  ja v  a2  s.c o m
 * @param conn
 */
public static void commitQuietly(Connection conn) {
    try {
        if (conn != null && !conn.isClosed()) // ????
        {
            conn.commit();
        }
    } catch (SQLException sqle) {
        log.info("?conn???" + sqle.getMessage(), sqle);
    }
}