Sunday 24 November 2013

Connection Pooling

Connection Pooling Using Hibernate 


Hello friends .Hope you all aware with Connection Pooling .if you are not than i am suggesting you to go through( https://docs.jboss.org/hibernate/core/4.1/devguide/en-US/html/ch01.html ).there are several ways through which we can pool the Connection Object into java Application.i am demonstrating you to pool Connection Resource using Hibernate.C3P0 Utility is used to manage Number of Connections.you need to add it into Classpath.

First of all you need to create Model class.hope you are aware of various annotations provied by hibernate & i am  using apache derby Database.well i am not suggesting you to use Built in Derby that is bundled with java  6.0 .Still to Demonstrate the Concept i am using Apache Derby :

in above example i am using Annotation based Hibernate Configuration.
package org.Model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;


@Entity
@Table(name="Employee")
public class Employee
{
   
   
    @Column
    private int id;
    @Column
    private String name;
    @Column
    private String address;
   
   
    public Employee() { }
    public int getId()
    {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

 }
hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
<!--Driver class to conenct Derby Database  -->
  <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<!--Connection url referring to datatbase MyDb-->
  <property name="hibernate.connection.url">jdbc:derby://localhost:1527/MyDb;create=true</property>
  <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
   <property name="show_sql">true</property>
   <property name="hbm2ddl.auto">create</property>
   <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">20</property>
  <property name="hibernate.c3p0.timeout">300</property>
  <property name="hibernate.c3p0.max_statements">50</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>

 
  </session-factory>
</hibernate-configuration>

Client Class
package org.Model;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class Client
{

   
    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
          
        SessionFactory sessionfactory=null;
        Session s=null;
      
        try
        {
        /*AnnotationConfiguration config = new AnnotationConfiguration();
        config.addClass(org.Model.Employee.class);*/
        //SessionFactory sessionFactory = config.buildSessionFactory();
      
      
        sessionfactory=new AnnotationConfiguration().configure().buildSessionFactory();
         s=sessionfactory.openSession();
      
      
            Employee e= new Employee();
            e.setId(1);
            e.setName("Apeksha");
            e.setAddress("Bangalore");
          
            s.beginTransaction();
          
                s.save(e);
              
            s.getTransaction().commit();
          
        }
        catch(Exception e)
        {
                System.out.println("Error in creating  Session Factory");
        }
            if(s!=null)
            {
                s.close();
              
            }
          
   }

}
This is how Connection Pooling will be done.Connection pooling improves the performace of Database by simply reducing number of connections required to connect to Database.

Thanks

for Any Query Please Mail me on nppathak90@gmail.com


No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...