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:org.mule.tck.util.MuleDerbyTestDatabase.java

/**
 * Executes a query against the database.
 *
 * @param sql SQL query//from w  ww.  ja  v a2  s .  c om
 * @return
 * @throws Exception
 */
public List execSqlQuery(String sql) throws Exception {
    Connection con = null;
    try {
        con = getConnection();
        return (List) new QueryRunner().query(con, sql, new ArrayListHandler());
    } finally {
        if (con != null && !con.isClosed()) {
            con.close();
        }
    }
}

From source file:org.verdictdb.jdbc41.VerdictConnectionTest.java

@After
public void tearDown() throws SQLException {
    Connection conn = connectionPair.getLeft();
    Connection vc = connectionPair.getRight();
    if (!conn.isClosed())
        conn.close();//w  w w .j a  v a  2s  .  co  m
    if (!vc.isClosed())
        vc.close();
}

From source file:it.cnr.icar.eric.server.persistence.rdb.ConnectionPool.java

private boolean isConnectionOK(Connection connection) {
    Statement testStmt = null;/*from   w w  w .j a v a 2s . 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) {
                testStmt = null;
            }
        }

        return false;
    }

    return true;
}

From source file:com.jedi.oracle.OracleCall.java

@Override
public void execute(DataSource dataSource) throws Exception {
    Connection connection = dataSource.getConnection();
    try {/*from   www .  j a va  2s .co m*/
        this.execute(connection);
    } finally {
        if (!connection.isClosed()) {
            connection.close();
        }
    }
}

From source file:pivotal.au.se.gemfirexdweb.controller.HomeController.java

@RequestMapping(value = "/home", method = RequestMethod.GET)
public String login(Model model, HttpServletResponse response, HttpServletRequest request, HttpSession session)
        throws Exception {
    logger.debug("Received request to show home page");

    if (session.getAttribute("user_key") == null) {
        logger.debug("user_key is null new Login required");
        response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
        return null;
    } else {/*  ww  w.  j  a  va  2 s .co m*/
        Connection conn = AdminUtil.getConnection((String) session.getAttribute("user_key"));
        if (conn == null) {
            response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
            return null;
        } else {
            if (conn.isClosed()) {
                response.sendRedirect(request.getContextPath() + "/GemFireXD-Web/login");
                return null;
            }
        }

    }

    // This will resolve to /WEB-INF/jsp/main.jsp
    return "main";
}

From source file:uk.co.modularaudio.util.pooling.sql.RemovalArbiter.java

/**
 * <P>/*from   w  ww .  j  av  a  2 s.c o m*/
 * This arbiter is called before a resource is destroyed in the pool.
 * </P>
 * <P>
 * It closes the java.sql.Connection.
 * </P>
 */
@Override
public int arbitrateOnResource(Pool pool, PoolStructure data, Resource res) {
    // Log.debug(className, "** Closing connection. **");
    DatabaseConnectionResource dbc = (DatabaseConnectionResource) res;
    Connection con = dbc.getConnection();
    try {
        // Log.debug(className, "Attempting to get lock");
        synchronized (con) {
            // Log.debug(className, "Got lock, attempting to close.");
            con.close();
            if (con.isClosed()) {
                // Log.debug(className, "Successful in closing connection");
            } else {
                log.error("Unable to close connection for some reason.");
            }
        }
    } catch (SQLException sqle) {
        log.error("Unable to closed DB connection", sqle);
    }
    return (Arbiter.CONTINUE);
}

From source file:org.azkfw.business.test.AbstractDatabaseTestCase.java

/**
 * ???/*from  w  w  w . j a v a 2 s . c  om*/
 * 
 * @param connection ?
 */
protected final void releaseConnection(final Connection connection) {
    if (null != connection) {
        if (connections.contains(connection)) {
            try {
                if (connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
                fail("Connection release error.");
            }
            connections.remove(connection);
        }
    }
}

From source file:com.zimbra.cs.db.DbPool.java

public static DbConnection getConnection(Mailbox mbox) throws ServiceException {
    if (!isInitialized()) {
        throw ServiceException.FAILURE("Database connection pool not initialized.", null);
    }//from w ww  .j  a v a 2s . co m
    Integer mboxId = mbox != null ? mbox.getId() : -1; //-1 == zimbra db and/or initialization where mbox isn't known yet
    try {
        Db.getInstance().preOpen(mboxId);
        long start = ZimbraPerf.STOPWATCH_DB_CONN.start();

        // If the connection pool is overutilized, warn about potential leaks
        PoolingDataSource pool = getPool();
        checkPoolUsage();

        Connection dbconn = null;
        DbConnection conn = null;
        try {
            dbconn = pool.getConnection();

            if (dbconn.getAutoCommit() != false)
                dbconn.setAutoCommit(false);

            // We want READ COMMITTED transaction isolation level for duplicate
            // handling code in BucketBlobStore.newBlobInfo().
            if (Db.supports(Db.Capability.READ_COMMITTED_ISOLATION))
                dbconn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

            conn = new DbConnection(dbconn, mboxId);
            Db.getInstance().postOpen(conn);
        } catch (SQLException e) {
            try {
                if (dbconn != null && !dbconn.isClosed())
                    dbconn.close();
            } catch (SQLException e2) {
                ZimbraLog.sqltrace.warn("DB connection close caught exception", e);
            }
            throw ServiceException.FAILURE("getting database connection", e);
        }

        // If we're debugging, update the counter with the current stack trace
        if (ZimbraLog.dbconn.isDebugEnabled()) {
            Throwable t = new Throwable();
            conn.setStackTrace(t);

            String stackTrace = SystemUtil.getStackTrace(t);
            synchronized (sConnectionStackCounter) {
                sConnectionStackCounter.increment(stackTrace);
            }
        }
        if (mbox != null)
            Db.registerDatabaseInterest(conn, mbox);

        ZimbraPerf.STOPWATCH_DB_CONN.stop(start);
        return conn;
    } catch (ServiceException se) {
        //if connection open fails unlock
        Db.getInstance().abortOpen(mboxId);
        throw se;
    }
}

From source file:com.cedarsoftware.ncube.NCubeManager.java

private static void validateConnection(Connection c) {
    try {//  www . j a  v a2  s .co  m
        if (c == null) {
            throw new IllegalArgumentException("Connection cannot be null");
        } else if (c.isClosed()) {
            throw new IllegalStateException("Connection already closed.");
        }
    } catch (SQLException e) {
        throw new IllegalStateException("Error closing connection.", e);
    }
}

From source file:bdManager.DBConnectionManager.java

public Connection getConnection() throws SQLException {
    long threadID = Thread.currentThread().getId();
    Connection connection = this.connections.get(threadID);
    if (connection != null) {
        if (connection.isClosed()) {
            return null;
        }//from w  ww.j  a va 2 s  .co m
        return connection;
    } else {
        return openConnection();
    }
}