Solved

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

Posted on 2004-04-28
3
255 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

911 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

19 Experts available now in Live!

Get 1:1 Help Now