Wednesday, August 01, 2012

Concept of Connection Pool using JAVA Code


class MypoolCountMgr
{

 String databaseUrl = "jdbc:oracle://IpAddresss:XXXX/myDatabase";
 String userName = "UN";
 String password = "UP";

 Vector poolCount = new Vector();

 public MypoolCountMgr()
 {
  initialize();
 }

 public MypoolCountMgr(
  String databaseUrl,
  String userName,
  String password
  )
 {
  this.databaseUrl = databaseUrl;
  this.userName = userName;
  this.password = password;
  initialize();
 }

 private void initialize()
 {
  //initialize
  initializepoolCount();
 }

 private void initializepoolCount()
 {
  while(!checkIfpoolCountIsFull())
  {
   System.out.println("Connection is available. We are proceeding to add new connections");
   //Adding new connection
   poolCount.addElement(createNewConnectionForPool());
  }
  System.out.println("Connection Pool is full.");
 }

 private synchronized boolean checkIfpoolCountIsFull()
 {
  final int MAX_POOL_SIZE = 5;

  //Check if the pool size
  if(poolCount.size() < 5)
  {
   return false;
  }

  return true;
 }

 //Normal Code to Creating a new connection
 private Connection createNewConnectionForPool()
 {
  Connection connection = null;

  try
  {
   Class.forName(DRIVER_TOLOAD);
   connection = DriverManager.getConnection(databaseUrl, userName, password);
   System.out.println("Connection: "+connection);
  }
  catch(SQLException sqle)
  {
   System.err.println("SQLException: "+sqle);
   return null;
  }
  catch(ClassNotFoundException cnfe)
  {
   System.err.println("ClassNotFoundException: "+cnfe);
   return null;
  }

  return connection;
 }

 public synchronized Connection getConnectionFromPool()
 {
  Connection connection = null;

  //Check if there is a connection available
  if(poolCount.size() > 0)
  {
   connection = (Connection) poolCount.firstElement();
   poolCount.removeElementAt(0);
  }
    return connection;
 }

 public synchronized void returnConnectionToPool(Connection connection)
 {
  //Adding the connection from the client back to the connection pool
  poolCount.addElement(connection);
 }

 public static void main(String args[])
 {
  MypoolCountMgr MypoolCountMgr = new MypoolCountMgr();
 }

}

Wednesday, July 04, 2012

Steps Involved to install Apache 2.4.2 server on Red Hat Linux


 -- Steps Involved to install Apache 2.4.2 server.

1 - Download Apache 4.2.2 server.
Please find the below link to download the Apache 4.2.2. server http://httpd.apache.org/docs/2.4/install.html#download

For Liux O/S: http://httpd.apache.org/download.cgi#apache24

Download httpd-2.4.2.tar.gz

Untar above zip in current “/” directory of Linux using following commands:

1) gzip -d httpd-2.4.2.tar.gz
2) tar xvf httpd-2.4.2.tar

2 - Download apr, apr-util and pcre required to run Apache Server. 
Please find the below link to download the apr, apr-util and pcre ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

 

Keep the above files inside /httpd-2.4.2/srclib  and perform following steps

1) cd /httpd-2.4.2/srclib
2) gzip -d apr-1.4.6.tar.gz
3) tar xvf apr-1.4.6.tar
4) gzip -d apr-util-1.4.1.tar.gz
5) tar xvf apr-util-1.4.1.tar
6) gzip -d pcre-8.20.tar.gz
7) tar xvf pcre-8.20.tar

Rename all the above folder inside /httpd-2.4.2/srclib from
-apr-1.4.6 to apr
-apr-util-1.4.1 to apr-util
-pcre-8.20.tar to pcre

3 - Configure pcre package.

Execute the following steps to configure pcre package on Linux Box.

1) cd /httpd-2.4.2/srclib/pcre
2) ./configure
3) make
4) make install

4 - Installing Apache Server.

Please follow following steps to installed Apache Server.

1) cd /httpd-2.4.2
2) ./configure --prefix=/httpd-2.4.2 \
--with-included-apr
3) make
4) make install

Make following changes into httpd.conf

1) Vi /httpd-2.4.2/conf/httpd.conf
a. Listen IPADDRESSOFMACHINE:9090
b. ServerName IPADDRESSOFMACHINE:9090

5 - Starting Apache Server.

Set the Path variable with following command:

export PATH=$PATH$:/httpd-2.4.2/bin:

Stop the existing httpd service

service httpd stop

Run the Apache Server with following command

/httpd-2.4.2/bin/apachectl -k start

Note : Please cross verify that your 9090 port is available and not in used by other application.