This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Connection Pooling

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Java Guru's,
I have come across the following scenerio's on connection pooling at my work.
I am really not sure whether it is a better approach to initialize a connection object inside each and every method of Adapter class or just initialize it once in a constructor and eventually use it and release it upon usage.
The reason I have this doubt in my mind is, I have worked on the same issue with both the approaches and both are working fine.
Could you please le me know which approach should I go for and wny?
Scenerio 1.
Initilizing the connection object in each and every method.
Code goes as below..
Class xxxAdapter
{
Connection conn=null;
Statement stmt=null;
public xxxAdapter()
{
}
public void getName(int param1, String param2) throws Exception
{
String sql="SELECT EMP_NAME FROM EMP WHERE EMP_ID="'+param1+"' AND EMP_DEPT='"+param2+"'";
try
{
conn = DBConnectionPool().getConnection(); // Initilize the connection object to get the connection from pool
stmt= conn.createStatement(sql);
--- REST CODE GOES HERE ----
}
catch(SQLException e)
{
// Exception caught
}
finally
{
if(conn != null)
DBConnectionPool().freeConnection(conn);//return the connection to the pool
}
}
public void getDetails(String param1) throws Exception
{
String sql="SELECT * FROM EMP WHERE EMP_NAME="'+param1+"' ";
try
{
conn = DBConnectionPool().getConnection(); // Initilize the connection object to get the connection from pool
stmt= conn.createStatement(sql);
--- REST CODE GOES HERE ----
}
catch(SQLException e)
{
// Exception caught
}
finally
{
if(conn != null)
DBConnectionPool().freeConnection(conn);//return the connection to the pool
}
}
Scenerio 2.
Initilizing the connection in constructor only once and use it when needed.
Code goes as below..
Class xxxAdapter
{
Connection conn=null;
Statement stmt=null;
public xxxAdapter()
{
conn = DBConnectionPool().getConnection(); // Initilize the connection object to get the connection from pool
}
public xxxAdapter(Connection connIn)
{
conn = connIn; // Initilize the connection object with the connection object of calling class.
}
public void getName(int param1, String param2) throws Exception
{
String sql="SELECT EMP_NAME FROM EMP WHERE EMP_ID="'+param1+"' AND EMP_DEPT='"+param2+"'";
try
{
stmt= conn.createStatement(sql);
--- REST CODE GOES HERE ----
}
catch(SQLException e)
{
// Exception caught
}
finally
{
if(conn != null)
DBConnectionPool().freeConnection(conn);//return the connection to the pool
}
}
public void getDetails(String param1) throws Exception
{
String sql="SELECT * FROM EMP WHERE EMP_NAME="'+param1+"' ";
try
{
stmt= conn.createStatement(sql);
--- REST CODE GOES HERE ----
}
catch(SQLException e)
{
// Exception caught
}
finally
{
if(conn != null)
DBConnectionPool().freeConnection(conn);//return the connection to the pool
}
}
Awaiting to have your comments..
[ March 30, 2003: Message edited by: san patil ]
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic