Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

One of the biggest threats in the cyber realm pertains to advanced persistent threats (APTs). This paper is a compare and contrast of Russian and Chinese APT's.
For months I had no idea how to 'discover' the IP address of the other end of a link (without asking someone who knows), and it drove me batty. Think about it. You can't use Cisco Discovery Protocol (CDP) because it's not implemented on the ASAs.…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

816 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

9 Experts available now in Live!

Get 1:1 Help Now