Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Releasing connection to pool

Status
Not open for further replies.

lauritspetersen

Programmer
Mar 23, 2003
1
DK
Hi

I have a problem with giving back connections to a connectionpool. I suspect the problem to be within the code. I have defined an init method and a destroy method but after two weeks or so the common resource exception occurs. Please take a look at the code and tell me what might be wrong.


private Connection connection;
private Statement statement;
private Context ctx;

public void init(ServletConfig config) throws ServletException
{
super.init(config);

try
{
ctx = new InitialContext();
Object o = ctx.lookup("defaultPool");
DataSource ds = (DataSource) PortableRemoteObject.narrow(o, DataSource.class);
this.connection = ds.getConnection();
this.statement = connection.createStatement();
}
catch (SQLException e){System.out.println(e);}
catch(NamingException ne){System.out.println(ne);}
}

public void service.......

public void destroy()
{
try
{
statement.close();
connection.close();
ctx.close();
}
catch (SQLException e){System.out.println(e);}
catch(NamingException ne){System.out.println(ne);}
}
 
init and destroy are used in instantiating and removing servlet in/from memory. And servlet can be reused anytime at containers will. I dont thing init and destroy are good places, where to create and close conections. Place it in service method. Remember, it actually means just to get and return connection to pool, so it has almost no performance impact.
Vladislav
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top