Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Firebird Open Source J2ee connector - jdbc driver
   *
   * Distributable under LGPL license.
   * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * LGPL License for more details.
  *
  * This file was created by members of the firebird development team.
  * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  *
  * All rights reserved.
  */
 package org.firebirdsql.pool;
 
 import java.sql.*;
 
 import javax.naming.*;
 import javax.sql.*;
 
Generic implementation of javax.sql.ConnectionPoolDataSource that uses java.sql.DriverManager to open physical connections to the database.

Author(s):
Roman Rokytskyy
 
 
     private static final Logger logger = LoggerFactory.getLogger(AbstractDriverConnectionPoolDataSource.classfalse);
     
     public static final UserPasswordPair EMPTY_USER_PASSWORD = new UserPasswordPair();
 
     private PrintWriter logWriter;
         
     private String jdbcUrl;
     private String driverClassName;
 
     private final Properties props = new Properties();
         
     public String getJdbcUrl() {
         return ;
     }
     
     public void setJdbcUrl(String jdbcUrl) {
         this. = jdbcUrl;
     }
     
     public String getDriverClassName() {
         return ;
     }
     
     public void setDriverClassName(String driverClassName) {
         this. = driverClassName;
     }
     
     public String getProperty(String name) {
         return .getProperty(name);
     }
     
     public void setProperty(String nameString value) {
         .setProperty(namevalue);
     }
     
     public Properties getProperties() {
         return ;
     }
    
    
Set JDBC properties that will be passed when opening a connection.

Parameters:
properties instance of java.util.Properties containing properties of a connection to open.
See also:
getProperties()
 
     public void setProperties(Properties properties) {
         if (properties == null)
             throw new NullPointerException("Specified properties are null.");
         
         .putAll(properties);
     }
 
     protected Logger getLogger() {
        return ;
    }
    public PrintWriter getLogWriter() {
        return ;
    }
    public void setLogWriter(PrintWriter out) {
         = out;
    }

    
Get login timeout.

Returns:
value set in setLoginTimeout(int) method or 0.
    public int getLoginTimeout() {
        return getBlockingTimeout() / 1000;
    }

    
Set login timeout for new connection. Currently ignored.

Parameters:
seconds how long pool should wait until new connection is granted.
    public void setLoginTimeout(int seconds) {
        setBlockingTimeout(seconds * 1000);
    }
    
    public int getTransactionIsolationLevel() {
        return ;
    }
    public void setTransactionIsolationLevel(int transactionIsolation) {
        this. = transactionIsolation;
    }
    public String getIsolation() {
        switch (getTransactionIsolationLevel()) {
            case .:
                return .;
            case .:
                return .;
            case .:
                return .;
            default:
                throw new IllegalStateException(
                        "Unknown transaction isolation level");
        }
    }
    public void setIsolation(String isolationthrows SQLException {
                .equalsIgnoreCase(isolation))
                .equalsIgnoreCase(isolation))
                .equalsIgnoreCase(isolation))
        else
            throw new FBSQLException("Unknown transaction isolation.",
                    .);
    }

    
    
Get connection manager that will allocate physical connections to the database.

Returns:
instance of PooledConnectionManager class.
        return ;
    }

    
Get name of this connection pool.

Returns:
name of the pool, equal to the JDBC URL value.
    protected String getPoolName() {
        return getJdbcUrl();
    }
    
    
Get pooled connection from the pooled queue.
        PingablePooledConnection connection = (PingablePooledConnectionsuper.getPooledConnection(queue);
        connection.addConnectionEventListener(this);
        return connection;
    }

    
Get pooled connection. This method will block until there will be free connection to return.

Returns:
instance of javax.sql.PooledConnection.
Throws:
java.sql.SQLException if pooled connection cannot be obtained.
    }

    
Get pooled connection for the specified user name and password.

Parameters:
user user name.
password password corresponding to specified user name.
Returns:
instance of javax.sql.PooledConnection for the specified credentials.
Throws:
java.sql.SQLException always, this method is not yet implemented.
    public PooledConnection getPooledConnection(String userString passwordthrows SQLException {
        return (PooledConnectiongetPooledConnection(getQueue(new UserPasswordPair(userpassword)));
    }
    
    
Notify about connection being closed.

Parameters:
connectionEvent instance of javax.sql.ConnectionEvent.
    public void connectionClosed(ConnectionEvent connectionEvent) {
        PooledObjectEvent event = new PooledObjectEvent(connectionEvent.getSource());
        pooledObjectReleased(event);
    }
    
    
Notify about physical connection being closed.

Parameters:
connectionEvent instance of javax.sql.ConnectionEvent.
    public void physicalConnectionClosed(ConnectionEvent connectionEvent) {
        PooledObjectEvent event = new PooledObjectEvent(connectionEvent.getSource(), true);
        pooledObjectReleased(event);
    }
    
    
Notify about the deallocation of the physical connection.

Parameters:
connectionEvent instance of javax.sql.ConnectionEvent.
    public void physicalConnectionDeallocated(ConnectionEvent connectionEvent) {
        PooledObjectEvent event = new PooledObjectEvent(connectionEvent.getSource(), true);
        physicalConnectionDeallocated(event);
    }
    
    
Notify about serious error when using the connection. Currently these events are ignored.

Parameters:
event instance of javax.sql.ConnectionEvent containing information about an error.
    public void connectionErrorOccurred(ConnectionEvent event) {
        if (getLogger() != null)
            getLogger().error("Error occured in connection."
                event.getSQLException());
    }
    public int getFreeSize() throws SQLException {
        return getQueue().size();
    }
    public int getTotalSize() throws SQLException {
        return getQueue().totalSize();
    }
    public int getWorkingSize() throws SQLException {
        return getQueue().workingSize();
    }
    /*
     * JNDI-related stuff
     */
    protected static final String PROPERTIES = "properties";
    protected static final String DRIVER_CLASS_NAME = "driverClassName";
    protected static final String JDBC_URL = "jdbcUrl";
    
    
Create instance of this data source.
        return FBPooledDataSourceFactory.createDriverConnectionPoolDataSource();
    }
    
    
Get object instance for the specified name in the specified context.

    
Get default JNDI reference for this instance.
    public Reference getDefaultReference() {
        Reference ref = super.getDefaultReference();
        
        if (getDriverClassName() != null)            
            ref.add(new StringRefAddr(getDriverClassName()));
        if (getJdbcUrl() != null)            
            ref.add(new StringRefAddr(getJdbcUrl()));
        
        byte[] data = serialize(getProperties());
        ref.add(new BinaryRefAddr(data));
        
        return ref;
    }
    
    {
        if (ds == null)
            return null;
        Reference ref = (Referenceobj;
        String addr;
        addr = getRefAddr(ref);
        if (addr != null)
            ds.setDriverClassName(addr);
        addr = getRefAddr(ref);
        if (addr != null)
            ds.setJdbcUrl(addr);
        RefAddr binAddr = ref.get();
        if (binAddr != null) {
            byte[] data = (byte[]) binAddr.getContent();
            Properties props = (Propertiesdeserialize(data);
            if (props != null)
                ds.setProperties(props);
        }
        return ds;
    }

    
Pooled connection manager that uses java.sql.DriverManager to allocate physical connections.
    private class DriverPooledConnectionManager implements PooledConnectionManager {
    
        private static final String USER_NAME_PROPERTY = "user";
        private static final String PASSWORD_PROPERTY = "password";
        
        private boolean driverInitialized;
    
        public PooledObject allocateConnection(Object keyPooledConnectionQueue queuethrows SQLException {
            if (!) {
                try {
                    Class.forName(getDriverClassName());
                     = true;
                    
                } catch(ClassNotFoundException ex) {
                    throw new FBSQLException(
                        "Class " + getDriverClassName() + " not found.");
                }
            }
            
            if (!(key instanceof UserPasswordPair))
                throw new FBSQLException("Incorrect key.");
            final UserPasswordPair pair = (UserPasswordPairkey;
                
            // set all properties
            final Properties props = new Properties();
            props.putAll(getProperties());
            final String userName = pair.getUserName();
            if (userName != null)
                props.setProperty(userName);
            final String password = pair.getPassword();
            if (password != null)
                props.setProperty(password);
            
            // open JDBC connection to the database
            Connection connection = DriverManager.getConnection(getJdbcUrl(), props);
            
            // wrap connection into PooledObject implementation
            final PingablePooledConnection pooledConnection;
            if (isPingable())
                pooledConnection = new PingablePooledConnection(
                        connectiongetPingStatement(), getPingInterval(),
                        isStatementPooling(), getMaxStatements(), isKeepStatements(), queue);
            else
                pooledConnection = new PingablePooledConnection(
                        connectionisStatementPooling(), getMaxStatements(),
                        isKeepStatements(), queue);
            
            pooledConnection.setDefaultTransactionIsolation(getTransactionIsolationLevel());
            return pooledConnection;
        }
    }
    
 }
New to GrepCode? Check out our FAQ X