commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hussain, Arshad" <Hussa...@aetna.com>
Subject DBCP-Abandoned Connection are not being re-claimed , can some one help pleae
Date Tue, 26 Aug 2008 14:53:59 GMT
I am trying to test if Abandoned Connection are being re-claimed,  this
not working  and  after 5-8 connection the loops hangs.  
Can anyone help.
 
1. DBBean.java -Establishes Connection Pooling
2. PerformConnection .java-  Here  I leave connection open  for every
alternative counter  
 
 
DBbean is creates connection pool c
 connectionPool = new GenericObjectPool(null);
 connectionPool.setMinIdle(2);
  connectionPool.setMaxActive(3);
 ConnectionFactory connectionFactory =
                new DriverManagerConnectionFactory(connectString,
                                                   Config.getDbUserId(),
 
Config.getDbPassword()
                                                  );
 
          
        AbandonedConfig  config = new  AbandonedConfig ();
        config.setRemoveAbandoned(true);
        config.setRemoveAbandonedTimeout(5);
        config.setLogAbandoned(true);
        PoolableConnectionFactory poolableConnectionFactory =
  new
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,fal
se,true,config);
        connectionPool.setFactory(poolableConnectionFactory );
        dataSource = new PoolingDataSource(connectionPool);
         

 


************************************************************************
************************************************************************
************************************************************************
*****
 
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import org.apache.commons.dbcp.AbandonedConfig;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import org.apache.commons.pool.KeyedObjectPoolFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import phc.common.Config;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.Statement;
import java.sql.*;
 

public class DBBean
{
    public static Connection dbCon;
    private static PoolingDataSource dataSource=null;
    private static boolean initalFlag=false;
    
    private static SharedPoolDataSource sharedDatasource;
   
   private static final String driver =
"oracle.jdbc.driver.OracleDriver";
   private static final String username = Config.getDbUserId();
   private static final String password = Config.getDbPassword();
   private static final String jdbcConnection =
      "jdbc:oracle:thin:@" + Config.getDbConnectString();
   protected static GenericObjectPool connectionPool;  
   
   
   
    public DBBean()
    {
    }
 
    public static Connection connect()
        throws Exception
    {
       String connectString = "";
        Connection dbCon = null;
        if(!initalFlag || dataSource==null)
        {
 
         setupDataSource();
          initalFlag=true;
 
        }
        
        
        
       return dataSource.getConnection();
    }
 
    public void close(Connection dbCon)
        throws Exception
    {
        dbCon.close();
    }
 
    static String dbDriver = "oracle.jdbc.driver.OracleDriver";
  
  
  
   public static void setupDataSource() {
       
        String connectString = "";
        try{
           Class.forName("oracle.jdbc.driver.OracleDriver");
           connectString = "jdbc:oracle:thin:@" +
Config.getDbConnectString();
           
               
             connectionPool = new GenericObjectPool(null);
             connectionPool.setMinIdle(2);
             connectionPool.setMaxActive(3);
            
 
       ConnectionFactory connectionFactory =
                new DriverManagerConnectionFactory(connectString,
                                                   Config.getDbUserId(),
 
Config.getDbPassword()
                                                  );
 
          
        AbandonedConfig  config = new  AbandonedConfig ();
        config.setRemoveAbandoned(true);
        config.setRemoveAbandonedTimeout(5);
        config.setLogAbandoned(true);
        PoolableConnectionFactory poolableConnectionFactory =
  new
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,fal
se,true,config);
        connectionPool.setFactory(poolableConnectionFactory );
        dataSource = new PoolingDataSource(connectionPool);
         
         
        }catch (Exception e) {e.printStackTrace();}
    
    }
    
    
    
      public static void main(String[] args)
        {
             long vTotalMem =0;
             long vFreeMem=0;
             boolean flag=true;
        try
        {
          
                Connection conn = connect();
                 new PerformConnection(conn,true).start();
            } 
 
            long currentTiming = System.currentTimeMillis();
            
            // Perform the SQL operation many times.  Notice that
            // we are kicking these off as simultaneous threads,
            // closer to the behavior one might expect in a
            // multi-threaded environment such as a web application
            for (int x = 0; x <10; x++)
            {
                
                Trace.log(" x=" +  x +"connectionPool.getNumActive()" +
DbBean.connectionPool.getNumActive() );
                Trace.log(" x=" +  x +"connectionPool.getNumIdle()" +
DbBean.connectionPool.getNumIdle() );
                Trace.log(" x=" +  x +"connectionPool.getMaxActive() " +
DbBean.connectionPool.getMaxActive() );
                 vTotalMem = Runtime.getRuntime().totalMemory();
              vFreeMem = Runtime.getRuntime().freeMemory();
              Trace.log("*  Total:   " + vTotalMem +" Bytes    FreeMem:
" + vFreeMem +" Bytes");
                Connection conn = connect();
               if( x%2==0)
               flag=false;
               else 
               flag=true;
               Trace.log("connection flag="+ flag);
                new PerformConnection(conn,flag).start();
                if(  connectionPool.getNumIdle() < 2 &&
(connectionPool.getNumActive() > connectionPool.getMaxActive() - 3)) 
                  Trace.log ("Condition met" );
                 else 
                  Trace.log ("Condition not met" );
 
            }
              System.out.println("out of the loop");   
        
            float timing =
                (System.currentTimeMillis() - currentTiming)
                    / 1000f;
            System.out.println("Seconds with pool : " + timing);
            currentTiming = System.currentTimeMillis();
        
        }
        catch (Exception e) { e.printStackTrace(); }
 
        
        }
 
 
 
 
 
 
 
}
 
 
 

************************************************************************
************************************************************************
************************************************************************
*****
import java.sql.Connection;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class PerformConnection  extends Thread
{
  private static final String testSQL = "SELECT * from dual ";
 
    Connection connection;
 
   boolean connectionStatus ; 
  
     PerformConnection(Connection myConnection, boolean
connectionStatus)
    {
        this.connection = myConnection;
        this.connectionStatus=connectionStatus;
    }
 
 
     public void run()
    {
        Connection myConnection = connection;
        Statement myStatement = null;
        ResultSet myResult = null;
        try
        {
            myStatement = myConnection.createStatement();
            myResult = myStatement.executeQuery(testSQL);
 
           
            int numcols = myResult.getMetaData().getColumnCount();
            while (myResult.next())
            {
                for (int i = 1; i <= numcols; i++)
                { myResult.getString(i); }
            }
 
        } catch (SQLException e)
        {
            e.printStackTrace();
        } finally
        {
            try { myResult.close(); } catch (Exception e) { }
            try { myStatement.close(); } catch (Exception e) { }
            if( this.connectionStatus)
            try { myConnection.close(); } catch (Exception e) { }
        }
        
        
        
    }
    
 
           
           
 
 
}


This e-mail may contain confidential or privileged information. If
you think you have received this e-mail in error, please advise the
sender by reply e-mail and then delete this e-mail immediately.
Thank you. Aetna   
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message