java4all@1986 java. Powered by Blogger.
Showing posts with label Connection pooling through Datasource. Show all posts
Showing posts with label Connection pooling through Datasource. Show all posts

Difference between Jdbc.close and DataSource.close?

>> Thursday, May 26, 2011

  • In Jdbc con.close() terminates the connection to the D.B,it automatically closes the resultset and statement object.
  • But in case of Datasource con.close ,it just return the connection to the connection pooling ,it doesnot close the connection  for the D.B.It is our responsibility to close the reultsset and statement object associated with that connection.

Read more...

DataBase connection in Java Application? or How to get DataSource from TomCat?

The below steps will explain you how to get DataSource from Tomcat.

STEP1:) In Tomcat Folder. Tomcat-->Context.xml 6.0 server.xml 5.0
                   Open Context.XML file ,under context tag add Resource tag with following attributes.

<Resource
           name="jdbc/projectname"
           auth="Container"
           type="javax.sql.DataSource"
           maxActive="100"
           maxIdle=300"
           maxWait="1000"
           username="bhaskar"
           password="bhaskar"
           driverclassName="com.mysql.jdbc.Driver"
           url="jdbc.mysql://localhost:3306/schemanameoftable"  />


    STEP2:
 After configuring in Tomcat we have to get the connection in our application.For This go through belo0w program....


             import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


public class DbUtility {

    private static  Context objContext = null;
    private static  DataSource objDataSource = null;
   
     static
     {
         System.out.println("inside the static block of dbutility");
        try
        {
            objContext = new InitialContext();
             objDataSource =(DataSource)  objContext.lookup("java:/comp/env/jdbc/projectname");
             System.out.println("objDataSource is: "+objDataSource);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Error while in datasource;");
        }
       
       
       
     }
   
    public static Connection getConnection() throws Exception{
       
        Connection con = null;
       
        if(objDataSource == null)
        {
            System.out.println("error while getting the datasource");
        }
        else
        {
            con= objDataSource.getConnection();
        }
       
        return con;
    }
   
   
   
    public static void closeConnecton(Connection objConnection) throws Exception
    {
        System.out.println("inside closing the connection");
        if(objConnection != null)
        {
            objConnection.close();
        }
    }
}
Connection pooling means reusing the connection witout closing the connection.For this go for difference between Jdbc.close and DataSource.close.

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP