wood burning stoves 2.0*
The moose likes JDBC and the fly likes Cancel JDBC Connection when Browser Cancel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Cancel JDBC Connection when Browser Cancel" Watch "Cancel JDBC Connection when Browser Cancel" New topic
Author

Cancel JDBC Connection when Browser Cancel

Kened Fernandez
Greenhorn

Joined: Feb 02, 2005
Posts: 2
Please I have a pool of connection to an Oracle DataBase, and several JSP sharing that pool of connection. All the servlet in my server ask for a new Connection from that pool every time a user does a query. But I want to close the connection everytime the user press stop in the browser or when some timeout has expired. Can anyone give me some ideas how to detect that?

I am using a class which manages the pool of connection, store that class like a ServletContext attribute, and every time any java servlet needs a new connection that class manage the opening of the new connection, and give the new connection to the servlet. So in the case somebody cancel the search, how can I detect that in the servlet (or in the pool connections class) and cancel the connection.

Please any note, paper or advice will be very helpful
Thanks.


Kened Fernandez<br />SCJP 1.4 (85%)<br />SCWCD in progress...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30075
    
149

Kened,
Welcome to JavaRanch!

There isn't any way of the server knowing that the browser cancelled the request. Two best practices:
1) Use the application server's connection pool (It sounds like you may already be doing this.)
2) Close the connection after each query. Don't rely on the user telling you when the request is done. You know when the query is complete, so give the connection back to the pool then.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Sounds like want to dedicate a connection to a session, dont dedicate a connection to a session. you would shortly slip into performance lack.

Have you considered this.

no of sessions = no of connections.


Your server would definitely turn into a bottleneck condition. Why a new connection always. You are using connection pooling mate. You forced me to think you as, you dont really know the pooling mechanism. is it?
[ February 04, 2005: Message edited by: Adeel Ansari ]
Kened Fernandez
Greenhorn

Joined: Feb 02, 2005
Posts: 2
Guys, Thanks for the responses. Sorry for the delay.
I am using my own connection pool class, already made in the company where I work, but I have noticed when a user cancel the query, the connection (and the query) is still alive, and I would like to avoid this.

I am closing the connection everytime the query finished, but what happen if the result of the query is big, and the user cancel it, the connection continues...What can I do to avoid this?

Thanks.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30075
    
149

Kened,
Now I understand! No, there isn't any way of the server knowing that the user cancelled the request on the browser. You have to let the request complete or timeout and naturally close the connection then.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cancel JDBC Connection when Browser Cancel
 
Similar Threads
Browser Refresh
Help with closing jdbc connections
A problem in Connection pool design
Tomcat, MySQL,JSP/Servlets -> Connection Pooling
Help please!!! Is there a method for closing sessions java-side?