Tuesday, April 27, 2010

Database Connection pooling

For building any application which has a database, we need to every time query database. So almost every function requires to create connection with database. Creating connection with database takes lot of time which can be an overhead to database. Also we need to limit the no of simultaneous connection to the database to protect from failure. Hence, if we use already created connection we can improve the performance of the application to great extent. The mechanism of using already created connection is called as Database Pooling. I have implemented a simple database pooling which I am sharing with you. you can download the jar file from here.
To use just import the jar file in eclipse or put in your class path. After that you need to initialize the connection parameters. for this purpose if you are making web-application, use ServletContextListener to initialize by importing the context-params as show below:


<context-param>
 <param-name>url</param-name>
 <param-value>jdbc:mysql://192.168.0.11:3306/test</param-value>
</context-param>
<context-param> 
 <param-name>driver</param-name>
 <param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
 <param-name>login</param-name>
 <param-value>login</param-value>
</context-param>
<context-param>
 <param-name>password</param-name>
 <param-value>password</param-value>
</context-param>
<listener>
 <listener-class>database.MyServletContextListener</listener-class>
</listener>

In above configuration replace the driver, url, login and password according to your database.

Now we need to write the ServletContextListener. it is as shown below:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import database.DbConnect;
import database.MyConnection;
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent arg0) {
/* creating ten connections, MyConnection is a static class which has the
* connection properties and DbConnect is a class which gives us the
* static functions getConnection() and release();
*/
ServletContext context = getServletContext();
String url = context.getInitParameter("url");
String driver = context.getInitParameter("driver");
String login = context.getInitParameter("login");
String password = context.getInitParameter("password");
try {
  MyConnection.initialize(url,login,password,driver);
  DbConnect.connect();
 } catch (Exception e) {
  e.printStackTrace();
 }
} 

public void contextDestroyed(ServletContextEvent arg0) { // closing all active connections DbConnect.close(); }

Now our connections are ready to be used, below is the example of getting the connections and releasing.
MyConnection conn = null;

try {
conn = DbConnect.getConnection();
Statement s = conn.connection.createStatement();

} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
DbConnect.release(conn);
}

hope so it might be useful to you. Kindly share your comments and doubts here. thank you.