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.util.*;
 
 import javax.naming.*;
 import javax.sql.*;
 
Connection pool for Firebird JDBC driver. Following properties are supported:
  • blobBufferSize size of the buffer used to transfer BLOB data.
  • blockingTimeout time in milliseconds during which getPooledConnection() method will block if no free connection is in pool.
  • charSet similar to encoding, but takes Java character set name instead of Firebird's encoding.
  • database path to a database including the server name; for example localhost/3050:c:/path/to/database.gdb.
  • encoding character encoding for the JDBC connection.
  • freeSize read-only: gives amount of free connections in the pool, when 0, blocking will occur if workingSize is equal to maxPoolSize.
  • isolation default transaction isolation level for connections as string; possible values are:
    • TRANSACTION_READ_COMMITTED
    • TRANSACTION_REPEATABLE_READ
    • TRANSACTION_SERIALIZABLE
  • loginTimeout property from javax.sql.DataSource, in this context is a synonym for blockingTimeout (however value is specified in seconds).
  • maxIdleTime time in milliseconds after which idle physical connection in the pool is closed.
  • maxStatements maximum number of pooled prepared statements, if 0, pooling is switched off.
  • maxPoolSize maximum number of physical connections that can be opened by this data source.
  • minPoolSize minimum number of connections that will remain open by this data source.
  • nonStandardProperty a non-standard connection parameter in form name[=value].
  • password password that is used to connect to database.
  • pingInterval time interval during which connection will be proved for aliveness.
  • pooling allows switching pooling off.
  • statementPooling alternative way to switch statement pooling off.
  • socketBufferSize size of the socket buffer in bytes. In some cases values used by JVM by default are not optimal. This results in performance degradation (especially when you transfer big BLOBs). Usually 8192 bytes provides good results.
  • sqlRole SQL role name.
  • tpbMapping mapping of the TPB parameters to JDBC transaction isolation levels.
  • transactionIsolationLevel default transaction isolation level, number from java.sql.Connection interface.
  • totalSize total number of allocated connections.
  • type type of connection that will be created. There are four possible types: pure Java (or type 4), type 2 that will use Firebird client library to connect to the database, local-mode type 2 driver, and embedded that will use embedded engine (access to local databases). Possible values are (case insensitive):
    • "PURE_JAVA" or "TYPE4" for pure Java (type 4) JDBC connections;
    • "NATIVE" or "TYPE2" to use Firebird client library;
    • "LOCAL" to use Firebird client library in local-mode (IPC link to server);
    • "EMBEDDED" to use embedded engine.
  • userName name of the user that will be used to access the database.
  • workingSize number of connections that are in use (e.g. were obtained using getPooledConnection() method, but not yet closed).

Author(s):
Roman Rokytskyy
    
    public static final AbstractConnectionPool.UserPasswordPair 
    
    private static final String PING_STATEMENT = "SELECT cast(1 AS INTEGER) FROM rdb$database";
    
    private static final Logger LOG = LoggerFactory.getLogger(AbstractFBConnectionPoolDataSource.classfalse);
        
	private transient PrintWriter logWriter;
    
    private transient volatile FBManagedConnectionFactory mcf;
    
Get connection properties. This method returns a live object where connection properties can be set. Usually application does not need to call this method, since this class implements appropriate interface and all properties can set directly. However, this is needed for custom serialization.

Returns:
"live" instance of org.firebirdsql.jdbc.FBConnectionProperties.
        return ;
    }
    
    
Set the connection properties in bulk. This method replaces the instance created in constructor, therefore, if the managed connection factory was already initialized with that instance, change will not be visible to it. In this case appropriate exception is thrown.

Parameters:
props instance of org.firebirdsql.jdbc.FBConnectionProperties.
Throws:
java.lang.IllegalStateException if managed connection factory is already initialized.
    public void setConnectionProperties(FBConnectionProperties props) {
        if (props == null)
            throw new NullPointerException();
        
        checkNotStarted();
        
        this. = props;
    }
    private void checkNotStarted() throws IllegalStateException {
        if ( != null)
            throw new IllegalStateException(
                    "ManagedConnectionFactory is already instantiated, " +
                    "changing connection properties in bulk is not allowed.");
    }
    
        if ( != null) {
            return ;
        }
        synchronized (this) {
            if ( != null) {
                return ;
            }
            GDSType gdsType = GDSType.getType(getType());
            if (gdsType == null)
                gdsType = GDSFactory.getDefaultGDSType();
             = new FBManagedConnectionFactory(gdsType);
        }
        
        return ;
    }
    protected Logger getLogger() {
        return ;
    }
    
    }

    
Get name of the connection queue.

    protected String getPoolName() {
        return getDatabase();
    }
    
	    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);
	}

    
Get pooled connection from the pooled queue.
        connection.addConnectionEventListener(this);
        connection.setManagedEnvironment(false);
        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 {
            getQueue(new AbstractConnectionPool.UserPasswordPair(userpassword)));
	}
    
    
Get XA connection. This method will block until there will be free connection to return.

Returns:
instance of javax.sql.XAConnection.
Throws:
java.sql.SQLException if pooled connection cannot be obtained.
    public XAConnection getXAConnection() throws SQLException {
        
        result.setManagedEnvironment(true);
        
        return result;
    }

    
Get XA connection for the specified user name and password.

Parameters:
user user name.
password password corresponding to specified user name.
Returns:
instance of javax.sql.XAConnection for the specified credentials.
Throws:
java.sql.SQLException always, this method is not yet implemented.
    public XAConnection getXAConnection(String userString password)
            throws SQLException 
    {
        FBPooledConnection result = (FBPooledConnection)getPooledConnection(userpassword);
        
        result.setManagedEnvironment(true);
        
        return result;
    }
    
    
Notify about connection being closed.

Parameters:
connectionEvent instance of javax.sql.ConnectionEvent.
	public void connectionClosed(ConnectionEvent connectionEvent) {
            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.
	    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();
    }
    
    
Check if this configuation defines a pingable connection JDBC pool.

    public boolean isPingable() {
        return true;
    }

    
Get SQL statement that will be used to "ping" the connection.

    public String getPingStatement() {
        String pingStatement = super.getPingStatement();
        if (pingStatement != null)
            return pingStatement;
        else
            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.
    public void setProperties(Properties properties) {
        for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entryiter.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            
            setNonStandardProperty(keyvalue);
        }
    }
    
    
Get type of JDBC driver that will be used. Note, value returned by this method might be different from that used in setType(java.lang.String) if you used synonym (either "TYPE4" or "TYPE2").

Returns:
one of the following values:
  • "PURE_JAVA" for pure Java type 4 JDBC driver.
  • "NATIVE" for type 2 JDBC driver that will use Firebird client library.
  • "EMBEDDED" for type 2 JDBC driver that will use embedded engine.
    public String getType() {
        return .getType();
    }
    
    
Set type of JDBC driver to use.

Parameters:
type type of driver to use. Possible values are (case insensitive):
  • "PURE_JAVA" or "TYPE4" for pure Java type 4 JDBC driver;
  • "NATIVE" or "TYPE2" for type 2 JDBC driver that will use Firebird client library.
  • "EMBEDDED" for type 2 JDBC driver that will use embedded version of the server.
    public void setType(String type) {
        checkNotStarted();
        .setType(type);
    }
    
    
Get type of JDBC driver that is used.

Returns:
type of JDBC driver that is used.
    public GDSType getGDSType() {
        return GDSType.getType(getType());
    }
    
    
Set type of the JDBC driver to use.

Parameters:
gdsType type of the JDBC driver.
    public void setGDSType(GDSType gdsType) {
        checkNotStarted();
        setType(gdsType.toString());
    }
    /*
     * Properties of this data source. These methods are created only
     * for user convenience and are shortcuts to setProperty(String, String)
     * method call with respective keys.
     */
    public String getNonStandardProperty(String key) {
        return .getNonStandardProperty(key);
    }
    
    public void setNonStandardProperty(String keyString value) {
        checkNotStarted();
        .setNonStandardProperty(keyvalue);
    }
    
    
Method that allows setting non-standard property in the form "key=value" form. This method is needed by some containers to specify properties in the configuration.

Parameters:
propertyMapping mapping between property name (key) and its value. Name and value are separated with "=", ":" or whitespace character. Whitespace characters on the beginning of the string and between key and value are ignored. No escaping is possible: "\n" is backslash-en, not a new line mark.
    public void setNonStandardProperty(String propertyMapping) {
        checkNotStarted();
        .setNonStandardProperty(propertyMapping);
    }
    public int getBlobBufferSize() {
        return .getBlobBufferSize();
    }
    public void setBlobBufferSize(int blobBufferSize) {
        checkNotStarted();
        .setBlobBufferSize(blobBufferSize);
    }
    public String getEncoding() {
        return .getEncoding();
    }
    public void setEncoding(String encoding) {
        checkNotStarted();
        .setEncoding(encoding);
    }
    public String getCharSet() {
        return .getCharSet();
    }
    
    public void setCharSet(String charSet) {
        checkNotStarted();
        .setCharSet(charSet);
    }
    
    public String getPassword() {
        return .getPassword();
    }
    public void setPassword(String password) {
        checkNotStarted();
        .setPassword(password);
    }
    public int getSocketBufferSize() {
        checkNotStarted();
        return .getSocketBufferSize();
    }
    public void setSocketBufferSize(int socketBufferSize) {
        checkNotStarted();
        .setSocketBufferSize(socketBufferSize);
    }
    
    public String getRoleName() {
        return .getRoleName();
    }
    
    public void setRoleName(String roleName) {
        checkNotStarted();
        .setRoleName(roleName);
    }

    

Deprecated:
please use getRoleName() instead.
    public String getSqlRole() {
        return getRoleName();
    }

    

Deprecated:
please use setRoleName(java.lang.String) instead.
    public void setSqlRole(String sqlRole) {
        setRoleName(sqlRole);
    }
    public String getTpbMapping() {
        return .getTpbMapping();
    }
    public void setTpbMapping(String tpbMapping) {
        checkNotStarted();
        .setTpbMapping(tpbMapping);
    }
    public String getUserName() {
        return .getUserName();
    }
    public void setUserName(String userName) {
        checkNotStarted();
        .setUserName(userName);
    }
    
    public int getBuffersNumber() {
        return .getBlobBufferSize();
    }
    public String getDatabase() {
        return .getDatabase();
    }
    }
    public String getDefaultIsolation() {
        return .getDefaultIsolation();
    }
    public int getDefaultTransactionIsolation() {
    }
    public String getSqlDialect() {
        return .getSqlDialect();
    }
    public TransactionParameterBuffer getTransactionParameters(int isolation) {
        return .getTransactionParameters(isolation);
    }
    public String getUseTranslation() {
        return .getUseTranslation();
    }
    public boolean isTimestampUsesLocalTimezone() {
    }
    public boolean isUseStandardUdf() {
        return .isUseStandardUdf();
    }
    public boolean isUseStreamBlobs() {
        return .isUseStreamBlobs();
    }
    public void setBuffersNumber(int buffersNumber) {
        checkNotStarted();
        .setBuffersNumber(buffersNumber);
    }
    public void setDatabase(String database) {
        checkNotStarted();
        .setDatabase(database);
    }
    public void setDefaultIsolation(String isolation) {
        checkNotStarted();
        .setDefaultIsolation(isolation);
    }
    public void setDefaultTransactionIsolation(int defaultIsolationLevel) {
        checkNotStarted();
        .setDefaultTransactionIsolation(defaultIsolationLevel);
    }
    public void setSqlDialect(String sqlDialect) {
        checkNotStarted();
        .setSqlDialect(sqlDialect);
    }
    public void setTimestampUsesLocalTimezone(boolean timestampUsesLocalTimezone) {
        checkNotStarted();
        .setTimestampUsesLocalTimezone(timestampUsesLocalTimezone);
    }
    public void setTransactionParameters(int isolationTransactionParameterBuffer tpb) {
        checkNotStarted();
        .setTransactionParameters(isolationtpb);
    }
    public void setUseStandardUdf(boolean useStandardUdf) {
        checkNotStarted();
        .setUseStandardUdf(useStandardUdf);
    }
    public void setUseStreamBlobs(boolean useStreamBlobs) {
        checkNotStarted();
        .setUseStreamBlobs(useStreamBlobs);
    }
    public void setUseTranslation(String translationPath) {
        checkNotStarted();
        .setUseTranslation(translationPath);
    }
    public boolean isDefaultResultSetHoldable() {
    }
    public void setDefaultResultSetHoldable(boolean isHoldable) {
        checkNotStarted();
    }
    
    public int getSoTimeout() {
        checkNotStarted();
        return .getSoTimeout();
    }
    public void setSoTimeout(int soTimeout) {
        checkNotStarted();
        .setSoTimeout(soTimeout);
    }
    
    public int getConnectTimeout() {
    }
    public void setConnectTimeout(int connectTimeout) {
    	checkNotStarted();
    	.setConnectTimeout(connectTimeout);
    }
    protected static final String REF_PROPERTIES = "properties";
    protected static final String REF_NON_STANDARD_PROPERTY = "nonStandard";
    
    public Reference getDefaultReference() {
        Reference ref = super.getDefaultReference();
        
        byte[] data = serialize();
        ref.add(new BinaryRefAddr(data));
        
        return ref;
    }
    
    {
        if (ds == null)
            return null;
        for (int i = 0; i < ref.size(); i++) {
            RefAddr element = ref.get(i);
            String type = element.getType();
            if (.equals(type))
                ds.setNonStandardProperty(element.getContent().toString());
            else
            if (.equals(type)) {
                byte[] data = (byte[]) element.getContent();
                FBConnectionProperties props = (FBConnectionPropertiesdeserialize(data);
                ds.setConnectionProperties(props);
            } else 
            if (element.getContent() instanceof String)
                ds.setNonStandardProperty(typeelement.getContent().toString());
        }
        return ds;
    }
    
    
        return FBPooledDataSourceFactory.createFBConnectionPoolDataSource();
    }

    
    private class PooledConnectionManagerImpl implements PooledConnectionManager {
        private final WeakReference<FBManagedConnectionFactorymcf;
            this. = new WeakReference<FBManagedConnectionFactory>(mcf);
        }
        public PooledObject allocateConnection(Object keyPooledConnectionQueue queue)throws SQLException {
            if (!(key instanceof AbstractConnectionPool.UserPasswordPair))
                throw new SQLException("Incorrect key.");
            final FBManagedConnectionFactory connectionFactory = .get();
            if (connectionFactory == null) {
                throw new SQLException("Weak reference to connection factory is null. Underlying connection pool has not been initialized or has been closed.");
            }
            final String userName = pair.getUserName();
            final String password = pair.getPassword();
            try {
                final FBConnectionRequestInfo defaultCri = connectionFactory.getDefaultConnectionRequestInfo();
                if (userName != null)
                    defaultCri.setUserName(userName);
                if (password != null)
                    defaultCri.setPassword(password);
                final FBManagedConnection managedConnection = (FBManagedConnectionconnectionFactory.createManagedConnection(nulldefaultCri);
                managedConnection.setConnectionSharing(false);
                managedConnection.setManagedEnvironment(false);
                final PingablePooledConnection pooledConnection;
                if (isPingable())
                    pooledConnection =
                            new FBPooledConnection(
                                    managedConnection,
                                    defaultCri,
                                    getPingStatement(),
                                    getPingInterval(),
                                    isStatementPooling(),
                                    getMaxStatements(),
                                    isKeepStatements(),
                                    queue);
                else
                    pooledConnection =
                            new FBPooledConnection(
                                    managedConnection,
                                    defaultCri,
                                    isStatementPooling(),
                                    getMaxStatements(),
                                    isKeepStatements(),
                                    queue);
                return pooledConnection;
            } catch(ResourceException ex) {
                throw new FBSQLException(ex);
            }
        }
    }
New to GrepCode? Check out our FAQ X