Sunday, December 19, 2010

How to configure root application in Tomcat??

http://techlightening.wordpress.com/2009/05/13/tomcat-change-default-web-application/

Tuesday, December 7, 2010

How can we write connection pooling using Vector, ArrayList classes in java??

//Comment
package connection.pooling;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Vector;

public class ConnectionPooling {

Vector connections = null;
static ConnectionPooling instance = null;
public static final int MAX_CONNECTIONS = 10;

private ConnectionPooling() {
initialize();
}

public synchronized void removeAllConnections() {

if (connections == null) {
return;
}

try {
int sz = connections.size();
for (int i = 0; i < sz / 2; i++) {
connections.remove(i);
System.out.println("Removing Connection " + i);
}
if (connections != null && connections.size() > 0) {
connections.removeAllElements();
System.out.println("Removing all the remaining Connections");
}
connections = null;
} catch (Exception e) {
System.out.println("Error " + e);
}
instance = null;

}

public static synchronized ConnectionPooling getInstance() {
if (instance == null)
instance = new ConnectionPooling();

return instance;
}

public synchronized void initialize() {

if (connections == null) {

try {
Class.forName("com.mysql.jdbc.Driver");
connections = new Vector();
int count = 0;
while (count < MAX_CONNECTIONS) {
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test",
"root", "root");
connections.addElement(c);
count++;
}
System.out.println("total connections created r: " + count);
} catch (Exception e) {
System.out.println("initialise:Exception");
e.printStackTrace();
instance.removeAllConnections();
}
}
}
public synchronized Connection getConnection() {
System.out.println("getConnection");
Connection c = null;
if (connections == null)
return null;
if (connections.size() > 0) {
c = (Connection) connections.elementAt(0);
connections.removeElementAt(0);
}
return c;
}

public synchronized void putConnection(Connection c) {

if (c != null) {
connections.addElement(c);
notifyAll();
}
}

public static void main(String[] args) {
ConnectionPooling cp = ConnectionPooling.getInstance();
cp.removeAllConnections();
}
}

What is Connection Pooling????

Many applications needs to connect to database for retrieving, updating, deleting and inserting the data. For every activity with the database there needs to be a connection established by the application server. If tens of thousands of connections are made to database server for every request then it chokes-up the network and server hangs.

To avoid this the Connection Pooling mechanism provides way of storing established connections in the memory.

This is nothing but pool all the connections at one place. Every time a database connection needs to be established a request is made to pool or any object which holds all the connections to provide a connection. Once that particular database activity is completed the connection is returned back to the pool.

Many J2EE application servers provide their own connection pooling mechanism.