Example usage for javax.sql PooledConnection getConnection

List of usage examples for javax.sql PooledConnection getConnection

Introduction

In this page you can find the example usage for javax.sql PooledConnection getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Creates and returns a Connection object that is a handle for the physical connection that this PooledConnection object represents.

Usage

From source file:biz.source_code.miniConnectionPoolManager.TestMiniConnectionPoolManager.java

private synchronized Connection getConnection2() throws SQLException {
        if (isDisposed)
            throw new IllegalStateException("Connection pool has been disposed."); // test again with lock
        PooledConnection pconn;
        if (!recycledConnections.empty()) {
            pconn = recycledConnections.pop();
        } else {//from ww  w .  java  2s  .  com
            pconn = dataSource.getPooledConnection();
        }
        Connection conn = pconn.getConnection();
        activeConnections++;
        pconn.addConnectionEventListener(poolConnectionEventListener);
        assertInnerState();
        return conn;
    }

From source file:org.enhydra.jdbc.pool.StandardPoolDataSource.java

/**
 * getConnection allows to get an object from the pool and returns it
 * to the user. In this case, we return an PooledConnection
 *///from  w  w w  .j av a  2  s .  c  o  m
public Connection getConnection(String _user, String _password) throws SQLException {
    log.debug("StandardPoolDataSource:getConnection");
    Connection ret = null;
    PooledConnection con = null;

    synchronized (this) {
        if (!onOff) {
            log.debug("StandardPoolDataSource:getConnection must configure the pool...");
            pool.start(); // the pool starts now
            onOff = true; // and is initialized
            log.debug("StandardPoolDataSource:getConnection pool config : \n" + pool.toString());
        }
    }

    try {
        try {
            log.debug("StandardPoolDataSource:getConnection Try to give a " + "connection (checkOut)");
            con = (PooledConnection) pool.checkOut(_user, _password);
            // get a connection from the pool
            log.debug("StandardPoolDataSource:getConnection checkOut return" + "a new connection");
        } catch (Exception e) {
            e.printStackTrace();
            log.debug(
                    "StandardPoolDataSource:getConnection SQLException in StandardPoolDataSource:getConnection"
                            + e);
            throw new SQLException(
                    "SQLException in StandardPoolDataSource:getConnection no connection available " + e);
        }

        ret = con.getConnection();
    } catch (Exception e) {
        log.debug("StandardPoolDataSource:getConnection exception" + e);
        e.printStackTrace();
        SQLException sqle = new SQLException(
                "SQLException in StandardPoolDataSource:getConnection exception: " + e);
        if (e instanceof SQLException)
            sqle.setNextException((SQLException) e);
        if (con != null) {
            pool.checkIn(con);
        }
        throw sqle;
    }
    log.debug("StandardPoolDataSource:getConnection return a connection");
    return ret;
}

From source file:org.enhydra.jdbc.pool.StandardPoolDataSource.java

/**
 * This method tests if a connection is closed or not
 *//*from w  w  w. jav  a  2  s.  c om*/
public boolean checkThisObject(Object o) {

    PooledConnection con;
    Connection ret;
    log.debug("StandardPoolDataSource:checkThisObject verify the current object");
    try {
        con = (PooledConnection) o;
        ret = con.getConnection(); // get the connection from the pool
        if (ret.isClosed()) {
            return false;
        }
        try {
            ret.close();
        } catch (Exception e) {
            log.error("StandardPoolDataSource:checkThisObject can't closed the connection: " + e);
        }

        return true;
    } catch (java.sql.SQLException e) {
        log.error(
                "StandardPoolDataSource:checkThisObject Error java.sql.SQLException in StandardPoolDataSource:checkThisObject");
        return false;
    }
}

From source file:org.enhydra.jdbc.pool.StandardPoolDataSource.java

/**
 * This method tests if a connection is valid or not
 *//*  w  w  w.  ja  va 2  s  . c  o  m*/
public boolean testThisObject(Object o) {
    Connection ret = null;
    log.debug("StandardPoolDataSource:testThisObject verify the current object");
    try {
        PooledConnection con = (PooledConnection) o;
        ret = con.getConnection();
        Statement s = ret.createStatement();
        s.execute(jdbcTestStmt);
        s.close();
        try {
            ret.close();
        } catch (Exception e) {
            log.error("StandardPoolDataSource:checkThisObject can't closed the connection: " + e);
        }
        return true;
    } catch (java.sql.SQLException e) {
        log.error(
                "StandardPoolDataSource:checkThisObject Error java.sql.SQLException in StandardPoolDataSource:testThisObject");
        return false;
    }
}

From source file:org.enhydra.jdbc.pool.StandardXAPoolDataSource.java

/**
 * This method tests if a connection is valid or not. It overrides the
 * method in StandardPoolDataSource to take into account global transactions:
 * if global transaction is in progress - suspend it so that
 * connection testing happens ouside of transaction.
 * If connection testing fails - it will not affect transaction
 * and next good connection can join the transaction
 *//*from w w  w  .j ava  2  s.c om*/
public boolean testThisObject(Object o) {
    Connection ret = null;
    log.debug("StandardXAPoolDataSource:testThisObject verify the current object");
    Transaction suspended = null;
    try {
        Transaction tx = transactionManager == null ? null : transactionManager.getTransaction();
        boolean isActive = tx == null ? false : tx.getStatus() == Status.STATUS_ACTIVE;
        if (isActive) {
            suspended = transactionManager.suspend();
        }

        PooledConnection con = (PooledConnection) o;
        ret = con.getConnection();

        Statement s = ret.createStatement();
        s.execute(jdbcTestStmt);
        s.close();
        try {
            ret.close();
        } catch (Exception e) {
            log.error("StandardXAPoolDataSource:checkThisObject can't closed the connection: " + e);
        }
        return true;
    } catch (SQLException e) {
        log.error(
                "StandardXAPoolDataSource:checkThisObject Error java.sql.SQLException in StandardXAPoolDataSource:testThisObject");
        return false;
    } catch (SystemException e) {
        log.error(
                "StandardXAPoolDataSource:checkThisObject Error java.sql.SystemException in StandardXAPoolDataSource:testThisObject");
        return false;
    } finally {
        if (suspended != null) {
            try {
                transactionManager.resume(suspended);
            } catch (Exception ex) {
                log.error(
                        "StandardXAPoolDataSource:checkThisObject Error Exception in StandardXAPoolDataSource:testThisObject");
                return false;
            }
        }
    }
}