Solved

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

Posted on 2004-04-28
3
254 Views
Last Modified: 2008-02-26
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
Comment
Question by:sarahchi
3 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 25 total points
ID: 10943428
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now