[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

how to discorrect from database using JavaBean when user click on "logout" button?

dear experts,

I created a logout button on my JSP page, but not sure how to implement it correctly.
if I just call window.close(), then this button does not really disconnect from database when the window is close.
but, i am using JavaBean to call the database object to connect and disconnect from database. however, there are several JavaBeans on each page. if I want to create a "generic" logout button that I can put it on every page, it will call every JavaBean to disconnect from database.

How to do this? is the right way to design the logout button? thanks.
0
sarahchi
Asked:
sarahchi
1 Solution
 
searlasCommented:
First things first, you shouldn't have a Connection per bean, or even a Connection per user.  You should have a Connection pool, which you quickly borrow a connection from, make a database call, and return it to the pool ready to be used by something else.  This is built into application servers via the use of DataSources.  See:
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Resources3.html

Secondly, you must not rely on users being well behaved (i.e. clicking logout).

Now, ploughing on regardless... if a bean holds a connection to the db, and the bean is held in a session (i.e. the bean is unique to a user, not shared between many users.)  You could get your beans to implement HttpSessionBindingListener; having an empty implementation for the valueBound method, and creating an implementation for valueUnbound that closes the connection to the database.  Your logout button could iterate over all attributes stored in the session, removing them one by one (thereby causing their valueUnbound methods to be called.)  Then you'd call session.invalidate(); to complete the logout action.

http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpSessionBindingListener.html
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now