FileDocCategorySizeDatePackage
ConnectionPool.javaAPI DocExample6594Mon Mar 31 23:10:16 BST 2003org.dasein.persist

ConnectionPool

public class ConnectionPool extends Object implements ConnectionPoolDataSource
Implementation of a connection pool data source that provides for prepared statement pooling. This data source depends on another data source to provide it actual database connection. That other data source should not be pooled.
Last modified $Date$
version
$Revision$
author
George Reese

Fields Summary
private String
dsn
The data source name of the data source to use for the actual database connections. When configuring this connection pool in your JNDI repository, you should specify a value for this property.
private PrintWriter
logWriter
The required log writer.
private int
loginTimeout
The required login timeout.
Constructors Summary
Methods Summary
public java.lang.StringgetDsn()
The data source name of the data source this pool will use to create its actual connections.

return
the data source name


                               
       
        return dsn;
    
public java.io.PrintWritergetLogWriter()

return
the log writer
throws
java.sql.SQLException never actually thrown

        return logWriter;
    
public intgetLoginTimeout()

return
the login timeout
throws
java.sql.SQLException never actually thrown

        return loginTimeout;
    
public javax.sql.PooledConnectiongetPooledConnection()
Provides an application data source with a reference to a pooled connection. The pooled connection represents the physical connection to the database. Because this library is an abstraction for the sake of statement pooling, our physical connection is itself an abstraction. The job of the physical connection is to provide the application data source with a logical connection.

return
a pooled database connection
throws
java.sql.SQLException a connection error occurred

        return getPooledConnection(null, null);
    
public javax.sql.PooledConnectiongetPooledConnection(java.lang.String uid, java.lang.String pw)
Provides an application data surce with a reference to a pooled connection based on the specified authentication credentials.

param
uid the user ID to use in making the connection
param
pw the password to use in making the connection
return
the pooled connection
throws
java.sql.SQLException a connection error occurred

        if( loginTimeout > 0 ) {
            ConnectionAttempt att = new ConnectionAttempt(dsn, uid, pw);
            long start = System.currentTimeMillis();
            long to = (long)(loginTimeout * 1000);
            
            synchronized( att ) {
                while( (System.currentTimeMillis() - start) < to ) {
                    try { att.wait(to); }
                    catch( InterruptedException e ) { }
                    if( att.connection != null ) {
                        return att.connection;
                    }
                    else if( att.exception != null ) {
                        throw att.exception;
                    }
                    else {
                        to = to - (System.currentTimeMillis() - start);
                    }
                }
                throw new SQLException("Connection timed out.");
            }
        }
        else {
            return new PhysicalConnection(dsn, uid, pw);
        }
    
public voidsetDsn(java.lang.String dsn)
Sets the name of the data source to be used for generating connections.

param
dsn the name of the data source

        this.dsn = dsn;
    
public voidsetLogWriter(java.io.PrintWriter out)
Assigns a print writer for logging.

param
out the new print writer
throws
java.sql.SQLException never thrown

        logWriter = out;
    
public voidsetLoginTimeout(int sec)
Sets a new login timeout for this pool.

param
sec the login timeout in seconds
throws
java.sql.SQLException never thrown

        loginTimeout = sec;