You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by bu...@apache.org on 2002/05/29 08:43:51 UTC

DO NOT REPLY [Bug 9480] New: - Data connection pooiling

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9480>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=9480

Data connection pooiling

           Summary: Data connection pooiling
           Product: Tomcat 3
           Version: 3.2.3 Final
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Jasper
        AssignedTo: tomcat-dev@jakarta.apache.org
        ReportedBy: netcomindia@vsnl.com


Our Java Web application is running on Tomcat 3.2.3 and once in every 
few days,we are getting the following sort of error.

java.lang.OutOfMemoryError: unable to create new native thread
	at java.lang.Thread.start(Native Method)
	at SeBase.DataConnection.createConnection(DataConnection.java:211)
	at SeBase.DataConnection.getConnection(DataConnection.java:191)
	at SeBase.CommonLogin.checkDatabase(CommonLogin.java:100)
	at SeBase.CommonLogin.setValues(CommonLogin.java:88)
	at 
sebase._0002fsebase_0002fcommon_0005floginaction_0002ejspcommon_0005floginactio
n_jsp_0._jspService
(_0002fsebase_0002fcommon_0005floginaction_0002ejspcommon_0005floginaction_jsp_
0.java:211)


This happens only on pages which access the Database. Other JSPs are running 
fine even after the error has occurred. Because we are not creating threads in 
our application except for Database connection.

	For Database connection, we are using a custom Data connection pooling 
mechanism (code given below). In that, while creating every new connection, we 
are creating a new thread which will check the usage of the connections after 
put to sleep for 2 minutes. If the connections are not in use for more than 1 
minute, it will  close the connections and come out of run method. We have 
verified that the data connections are getting closed properly. But the 
threads created during the process are not getting removed. 

	Is it the right way? Or can we use one single thread which will check 
all the data connections every 2 minutes and will run from the starting of the 
application server till it is closed? Can it lead us to any other problem?

/**
 * Returns a JDCConnecton object if it is availabe in vector or creates a
 * new connection add it to vector and returns the same
 */
   public synchronized Connection getConnection() throws Exception,SQLException
   {
      ConnectionPool f_obj_conn;   //for storing the connection pool object
      for(int i = 0; i < f_obj_connections.size(); i++) //for loop for getting 
any free connection
      {
         f_obj_conn = (ConnectionPool)f_obj_connections.elementAt(i);
         if (f_obj_conn.lease() && !f_obj_conn.isClosed())   //check for status
         {
				System.out.print("\nUsing Old Connection #" +  
f_int_total_conn + "Connection name " + f_obj_conn);
            return f_obj_conn;
         }   //end of if loop
      }   //end of for loop
      f_obj_conn = createConnection();
      return f_obj_conn;
   }


/**
 * Creates a physical connection with the concerned database
 */
   public synchronized ConnectionPool createConnection()throws Exception
   {
      ConnectionPool f_obj_conn_pool = null;   //for storing the pool object
      if(f_obj_connections.size() < f_int_max_conn)   //check for max size
      {
        Class.forName(f_str_driver);
        Connection f_obj_connection;   //for storing the connection
        f_obj_connection = DriverManager.getConnection
(f_str_url,f_str_user,f_str_password);
        f_obj_conn_pool = new ConnectionPool(f_obj_connection, this);
        f_obj_reaper = new ConnectionReaper(f_obj_conn_pool);
  		  f_int_total_conn = f_int_total_conn + 1;	
        f_obj_conn_pool.putThread(f_obj_reaper);
        f_obj_conn_pool.lease();
        f_obj_connections.addElement(f_obj_conn_pool);
		  f_obj_reaper.start();	
  		  System.out.print("\nNo Of Connection #" +  f_int_total_conn 
+ "New Connection Created " + f_obj_connection);
      }
      if(f_obj_conn_pool == null)throw new Exception("SERVER TOO BUSY");
      return f_obj_conn_pool;
   }


/**
 * Closes all the stale connection as per specfied in vector size
 */

   public synchronized void reapConnections()throws Exception
   {
      int f_int_index = 0;
      long f_long_stale;   //for storing the stale or elapse time of connection
      f_long_stale = System.currentTimeMillis() - f_long_timeout;
      Enumeration f_obj_connlist;   //for getting the enumeration of connection
      f_obj_connlist = f_obj_connections.elements();
      //while loop for reaping connection
      while((f_obj_connlist != null) && (f_obj_connlist.hasMoreElements()))
      {
         ConnectionPool f_obj_pool;   //for storing the connection pool object
         f_obj_pool = (ConnectionPool)f_obj_connlist.nextElement();
         //checking for status of connection
         if(!f_obj_pool.inUse())
         {
            removeConnection(f_obj_pool.f_obj_conn);
            Thread thread = f_obj_pool.getThread();
            thread = null;
				f_int_removed_conn = f_int_removed_conn + 1;
				f_obj_connections.removeElement(f_obj_pool);
     			f_obj_connections.trimToSize();     
         }
         if((f_obj_pool.inUse()) && (f_long_stale > f_obj_pool.getLastUse()))
         {
            removeConnection(f_obj_pool.f_obj_conn);
            Thread thread = f_obj_pool.getThread();
            thread = null;
  			   f_int_removed_conn = f_int_removed_conn + 1;
				f_obj_connections.removeElement(f_obj_pool);
     			f_obj_connections.trimToSize();                 
         }
      }   //end of while loop
   }



/**
 * Removes the connecton object stored in the Connection vector
 * @param ConnectionPool
 */

   private synchronized void removeConnection(Connection f_obj_conn)throws 
Exception
   {
      Connection connection = f_obj_conn;
      connection.close();
		if(connection.isClosed())
			System.out.print("\nConnection Closed  Connection Size 
#" +  f_int_removed_conn + "Connection " + connection);
		else
			System.out.print("Connection not closed");      
   }

/**
 * Run Method for a each  Thread of DataConnection object
 */
   public void run()
   {
      while(true)   //while loop for invoking sleep method
      {
         try
         {
            //System.out.println("sleep called " + this.f_obj_pool);
              sleep(f_long_delay);
         }
         catch( InterruptedException e)
         {

         }
         try
         {
         f_obj_pool.closeConnections();
         }
         catch(Exception e)
         {}

      }   //end of while loop
   }
}

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>