Package javax.sql

Examples of javax.sql.PooledConnection


   * @see net.sf.hajdbc.Database#connect(java.lang.Object, java.lang.String)
   */
  @Override
  public Connection connect(ConnectionPoolDataSource dataSource, String password) throws SQLException
  {
    PooledConnection connection = this.requiresAuthentication() ? dataSource.getPooledConnection(this.getUser(), password) : dataSource.getPooledConnection();
   
    return connection.getConnection();
  }
View Full Code Here


                    + " doesn't implement javax.sql.ConnectionPoolDataSource");
            }
        }
       
        // try to get a connection with the supplied username/password
        PooledConnection conn = null;
        try {
            if (username != null) {
                conn = cpds.getPooledConnection(username, password);
            }
            else {
                conn = cpds.getPooledConnection();
            }
            if (conn == null) {
                throw new SQLException(
                    "Cannot connect using the supplied username/password");
            }
        }
        finally {
            if (conn != null) {
                try {
                    conn.close();
                }
                catch (SQLException e) {
                    // at least we could connect
                }
            }
View Full Code Here

        if (username != this.username || password != this.password)
        {
            throw new SQLException("Username and password are invalid.");
        }

        PooledConnection pcon = null;
        if (pool.empty() && totalConnections < maxConnections)
        {
            pcon = getNewConnection();
        }
        else
View Full Code Here

     * @exception SQLException if there is aproblem with the db connection
     */
    private PooledConnection getNewConnection()
        throws SQLException
    {
        PooledConnection pc = null;
        if (username == null)
        {
            pc = cpds.getPooledConnection();
        }
        else
        {
            pc = cpds.getPooledConnection(username, password);
        }
        pc.addConnectionEventListener(this);

        // Age some connections so that there will not be a run on the db,
        // when connections start expiring
        //
        // I did some experimentation here with integers but as this
View Full Code Here

    private PooledConnection popConnection()
        throws Exception
    {
        while (!pool.empty())
        {
            PooledConnection con = (PooledConnection) pool.pop();

            // It's really not safe to assume this connection is
            // valid even though it's checked before being pooled.
            if (isValid(con))
            {
                return con;
            }
            else
            {
                // Close invalid connection.
                con.close();
                totalConnections--;

                // If the pool is now empty, create a new connection.  We're
                // guaranteed not to exceed the connection limit since we
                // just killed off one or more invalid connections, and no
View Full Code Here

     * Creates and returns new PooledConnection object, adding itself as a listener
     * for connection events.
     */
    protected PooledConnection newPooledConnection(String userName, String password)
        throws SQLException {
        PooledConnection connection =
            (userName != null)
                ? poolDataSource.getPooledConnection(userName, password)
                : poolDataSource.getPooledConnection();
        connection.addConnectionEventListener(this);
        return connection;
    }
View Full Code Here

    public void dispose() throws SQLException {
        synchronized (this) {
            // clean connections from the pool
            ListIterator<PooledConnection> unusedIterator = unusedPool.listIterator();
            while (unusedIterator.hasNext()) {
                PooledConnection con = unusedIterator.next();
                // close connection
                con.close();
                // remove connection from the list
                unusedIterator.remove();
            }

            // clean used connections
            ListIterator<PooledConnection> usedIterator = usedPool.listIterator();
            while (usedIterator.hasNext()) {
                PooledConnection con = usedIterator.next();
                // stop listening for connection events
                con.removeConnectionEventListener(this);
                // close connection
                con.close();
                // remove connection from the list
                usedIterator.remove();
            }
        }
View Full Code Here

        throws SQLException {

        int i = 0;
        int startPoolSize = getPoolSize();
        for (; i < addConnections && startPoolSize + i < maxConnections; i++) {
            PooledConnection newConnection = newPooledConnection(userName, password);
            unusedPool.add(newConnection);
        }

        return i;
    }
View Full Code Here

    }

    protected synchronized void shrinkPool(int closeConnections) {
        int idleSize = unusedPool.size();
        for (int i = 0; i < closeConnections && i < idleSize; i++) {
            PooledConnection con = unusedPool.remove(i);

            try {
                con.close();
            } catch (SQLException ex) {
                // ignore
            }
        }
    }
View Full Code Here

    /** Returns connection from the pool. */
    public synchronized Connection getConnection(String userName, String password)
            throws SQLException {

        PooledConnection pooledConnection = uncheckPooledConnection(userName, password);

        try {
            return uncheckConnection(pooledConnection);
        }
        catch (SQLException ex) {

            try {
                pooledConnection.close();
            }
            catch (SQLException ignored) {
            }
           
            // do one reconnect attempt...
            pooledConnection = uncheckPooledConnection(userName, password);
            try {
                return uncheckConnection(pooledConnection);
            }
            catch (SQLException reconnectEx) {
                try {
                    pooledConnection.close();
                }
                catch (SQLException ignored) {
                }
               
                throw reconnectEx;
View Full Code Here

TOP

Related Classes of javax.sql.PooledConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.