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.io.*;
 
 import javax.naming.*;
Base class for connection pool implementations. Main feature of this class is that it implements ConnectionPoolConfiguration interface and releives developers from creating getters and setters for pool configuration parameters. Additionally this class provides basic functionality for JNDI-enabled connection pools. No other functionality is available.

Author(s):
Roman Rokytskyy
 
 public abstract class BasicAbstractConnectionPool 
     extends AbstractConnectionPool 
 {
 
     /*
      * Following fields contain information about the pool characteristics.
      */
     private int minPoolSize = .;
     private int maxPoolSize = .;
 
 
     private String pingStatement;
 
     private boolean pooling = true;
     private boolean statementPooling = true;
     private boolean keepStatements = true;
     
     private Reference reference;
    
    
Create instance of this class. Default constructor introduced to make it available to subclasses.
 
     protected BasicAbstractConnectionPool() {
         super();
     }
 
     public abstract int getLoginTimeout() throws SQLException;
     public abstract void setLoginTimeout(int secondsthrows SQLException;
 
     public abstract PrintWriter getLogWriter() throws SQLException;
     public abstract void setLogWriter(PrintWriter printWriterthrows SQLException;
     
     public abstract PooledConnection getPooledConnection() throws SQLException;
     public abstract PooledConnection getPooledConnection(
         String userString passwordthrows SQLException;
     
         return this;
     }
     
     public int getBlockingTimeout() {
         return ;
     }
 
     public void setBlockingTimeout(int blockingTimeout) {
         this. = blockingTimeout;
     }

    

Deprecated:
non-standard name, use getMaxIdleTime()
 
     public int getIdleTimeout() {
         return getMaxIdleTime();
    }

    

Deprecated:
non-standard name, use setMaxIdleTime(int)
    public void setIdleTimeout(int idleTimeout) {
        setMaxIdleTime(idleTimeout);
    }
    
    public int getMaxIdleTime() {
        return ;
    }
    
    public void setMaxIdleTime(int maxIdleTime) {
        this. = maxIdleTime;
    }

    

Deprecated:
non-standard name, use getMaxPoolSize()
    public int getMaxConnections() {
        return getMaxPoolSize();
    }

    

Deprecated:
non-standard name, use setMaxPoolSize(int)
    public void setMaxConnections(int maxConnections) {
        setMaxPoolSize(maxConnections);
    }
    
    public int getMaxPoolSize() {
        return ;
    }
    
    public void setMaxPoolSize(int maxPoolSize) {
        this. = maxPoolSize;
    }

    

Deprecated:
non-standard name, use getMinPoolSize()
    public int getMinConnections() {
        return getMinPoolSize();
    }

    

Deprecated:
non-standard name, use setMinPoolSize(int)
    public void setMinConnections(int minConnections) {
        setMinPoolSize(minConnections);
    }
    
    public int getMinPoolSize() {
        return ;
    }
    
    public void setMinPoolSize(int minPoolSize) {
        this. = minPoolSize;
    }
    public int getPingInterval() {
        return ;
    }
    public void setPingInterval(int pingInterval) {
        this. = pingInterval;
    }
    public String getPingStatement() {
        return ;
    }
    public void setPingStatement(String pingStatement) {
        this. = pingStatement;
    }
    public int getRetryInterval() {
        return ;
    }
    public void setRetryInterval(int retryInterval) {
        this. = retryInterval;
    }
    public boolean isPingable() {
        return getPingInterval() > 0 && getPingStatement() != null;
    }
    public boolean isPooling() {
        return ;
    }
    public void setPooling(boolean pooling) {
        this. = pooling;
    }
    public boolean isStatementPooling() {
        return ;
    }
    public void setStatementPooling(boolean statementPooling) {
        
        // no statement pooling if max allowed statements is 0
        if (getMaxStatements() == 0)
            statementPooling = false;
        
        this. = statementPooling;
    }
    
    public boolean isKeepStatements() {
        return ;
    }
    
    public void setKeepStatements(boolean keepStatements) {
        this. = keepStatements;
    }
    
    public int getMaxStatements() {
        return ;
    }
    
    public void setMaxStatements(int maxStatements) {
        this. = maxStatements;
        
        if (maxStatements > 0 && !isStatementPooling())
            setStatementPooling(true);
        else
        if (maxStatements == 0)
            setStatementPooling(false);
    }
    
    private static final String REF_BLOCKING_TIMEOUT = "blockingTimeout";
    private static final String REF_RETRY_INTERVAL = "retryInterval";
    private static final String REF_LOGIN_TIMEOUT = "loginTimeout";
    private static final String REF_PING_INTERVAL = "pingInterval";
    private static final String REF_PING_STATEMENT = "pingStatement";
    
    private static final String REF_POOLING = "pooling";
    private static final String REF_STATEMENT_POOLING = "statementPooling";
    
    private static final String REF_MAX_STATEMENTS = "maxStatements";
    private static final String REF_MAX_POOL_SIZE = "maxPoolSize";
    private static final String REF_MIN_POOL_SIZE = "minPoolSize";
    private static final String REF_MAX_IDLE_TIME = "maxIdleTime";
    
    private static final String REF_MAX_CONNECTIONS = "maxConnections";
    private static final String REF_MIN_CONNECTIONS = "minConnections";
    private static final String REF_IDLE_TIMEOUT = "idleTimeout";
    protected abstract BasicAbstractConnectionPool createObjectInstance();
    
Get object instance for the specified name in the specified context. This method constructs new datasource if obj represents javax.naming.Reference, whose factory class is equal to this class.
    
    public Object getObjectInstance(Object objthrows Exception 
    {
        
        if (!(obj instanceof Reference)) return null;
    
        Reference ref = (Reference)obj;
    
        if (!getClass().getName().equals(ref.getClassName()))
            return null;
    
        
        int[] addressesToRemove = new int[ref.size()];
        
        for (int i = 0; i < ref.size(); i++) {
            RefAddr element = ref.get(i);
            
            String type = element.getType();
            String addr = element.getContent().toString();
            
            if (.equals(type))
                ds.setBlockingTimeout(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setMaxIdleTime(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setMaxIdleTime(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setLoginTimeout(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setMaxPoolSize(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setMinPoolSize(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setMaxPoolSize(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setMinPoolSize(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setPingInterval(Integer.parseInt(addr));
            else
            if (.equals(type))
                ds.setRetryInterval(Integer.parseInt(addr));
            else
            if (.equals(type)) 
                ds.setPingStatement(addr);
            else
            if (.equals(type))
                ds.setPooling(Boolean.valueOf(addr).booleanValue());
            else
            if (.equals(type))
                ds.setStatementPooling(Boolean.valueOf(addr).booleanValue());
            else
            if (.equals(type))
                ds.setMaxStatements(Integer.parseInt(addr));
            else
                continue;
            
            // set flag that we matched the address, so it has to be removed
            // we rely on fact that if none address was matched, last else
            // clause will be executed and next code will not be executed.
            addressesToRemove[i] = 1;
        }
        
        // removed matched addresses
        for (int i = addressesToRemove.length - 1; i >= 0 ; i--) {
            if (addressesToRemove[i] == 1)
                ref.remove(i);
        }
        
        return ds;
    }
    
    protected String getRefAddr(Reference refString type) {
        RefAddr addr = ref.get(type);
        if (addr == null)
            return null;
        else
            return addr.getContent().toString();
    }

    
Get JDNI reference.

Returns:
instance of javax.naming.Reference.
    public Reference getReference() {
        if ( == null)
            return getDefaultReference();
        else
            return ;
    }

    
Set JNDI reference for this data source.

Parameters:
reference JNDI reference.
    public void setReference(Reference reference) {
        this. = reference;
    }

    
Get default JNDI reference for this datasource. This method is called if datasource is used in non-JCA environment.

Returns:
instance of javax.naming.Reference containing all information that allows to reconstruct the datasource.
    public Reference getDefaultReference() {
        Reference ref = new Reference(getClass().getName(), 
                getClass().getName(), null);
        
            ref.add(new StringRefAddr(
                String.valueOf(getBlockingTimeout())));
    
            ref.add(new StringRefAddr(,
                String.valueOf(getMaxIdleTime())));
    
            ref.add(new StringRefAddr(
                String.valueOf(getMaxPoolSize())));
    
            ref.add(new StringRefAddr(,
                String.valueOf(getMinPoolSize())));
            
            ref.add(new StringRefAddr(
                String.valueOf(getPingInterval())));
        
            ref.add(new StringRefAddr(
                    String.valueOf(getRetryInterval())));
        
        if (getPingStatement() != null)
            ref.add(new StringRefAddr(,
                    String.valueOf(getPingStatement())));
        
        if (!isPooling())
            ref.add(new StringRefAddr(,
                    String.valueOf(isPooling())));
        
        if (!isStatementPooling())
            ref.add(new StringRefAddr(,
                    String.valueOf(isStatementPooling())));
        
            ref.add(new StringRefAddr(,
                    String.valueOf(getMaxStatements())));
            
        return ref;
    }
    public static byte[] serialize(Object obj) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        
        try {
            ObjectOutputStream out = new ObjectOutputStream(bout);
            out.writeObject(obj);
            out.flush();
        } catch(IOException ex) {
            return null;
        }
        
        return bout.toByteArray();
    }
    public static Object deserialize(byte[] data) {
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        
        try {
            ObjectInputStream in = new ObjectInputStream(bin);
            return in.readObject();
        } catch(IOException ex) {
            return null;
        } catch(ClassNotFoundException ex) {
            return null;
        }
    }
    
New to GrepCode? Check out our FAQ X