• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i know when browser is closed

 
Rehana Shaik
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
how would i know when user closes the browser.because i have to roll back some of the transactions when user closes the browser. from html i can use onunload method .but i want to know browser closing event at server side.At the same time how can i know particular session is inactive? our session timeout period is about 15 minutes so i can't wait for session timeout and rollback.
Thanks in advance
[ March 08, 2004: Message edited by: Rehana Shaik ]
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Relying on browser events using JavaScript is inherently unsafe - I'd recommend you don't use it. Rather than trying to roll back a transaction when the browser is closed, include some confirmation action to commit the whole transaction once the user has done whatever you need them to do - if it is never reached, then the transaction is never committed.
You will only know if the session is inactive if it has timedout. Reduce the size of your session timeout.
[ March 08, 2004: Message edited by: cluck cluck ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Cluck Cluck"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!
Good Answer to the question though.
Mark
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leaving a transaction open and waiting for user intervention is usually a bad idea. You might want to re-think what you're doing. If you can (if it's not a lot of data), you might want to gather up everything you need to perform the transaction and store it in the session. Then, at the end, you use that information to perform the entire transaction during one request. If the user closes their browser or their session times out, the data for the transaction is lost. You could actually save the data in the database (marking it as temporary or provisional somehow) and listen for session timeout events (via an HttpSessionListener) to clean it up. However you do it, I would NOT leave open a JDBC connection to the database while waiting for the user to do something. That can be quite a resource hog, especially if your session-timeout value is large. Just my $0.02.
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the same time how can i know particular session is inactive?

Add a class implementing HttpSessionListener interface in your application. this interface has 2 methods -
1. sessionCreated
2. sessionDestroyed
when your session gets inactive, sessionDestroyed() method would be called to indicate the same.
Dont forget to register this class as listener in your web.xml.
I hope, this helps. :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic