Execute code when the session variables expire

Hi,

I created an application that starts with a login window and if the user name and password are correct and the state of the user in the database table is equal to 0, then the user is allowed to use the application. The table "user" has the following fields: id,usr_name,password,state.

Every time the usr_name and password are correct, the state of the user is changed to 1 instead of 0, I did this to make sure there's only one user with that usr_name and password logged in.

There's also a log out button so that the user closes the application and his state is set to 0.

If the user doesn't click on the log out button, I have no problem with the session variables, user (String), loggedon(boolean) because they are set to null and false automatically when the session expires, but I don't know how to set the state of the user to "0" in the table... I already tried it in the destroy() function, but it doens't work...

My question is, is there a way to execute code at the same time when the session variables expire? because I need to do this so that I can change the state of the user to "0" otherwise no other user is able to use the system with that same name and password eventhogh they are correct because the state of the user is going to remain "1"

Thank you

Yese

[1335 byte] By [Yesenia] at [2007-11-26 9:09:37]
# 1
Have you tried putting your database update code in the destroy method of SessionBean1?Message was edited by: dkible
dkible at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 2

Hi dkible,

Thank you for replying..., I did as you said, I place the following code in the destroy method of the sessionbean

com.sun.sql.rowset.CachedRowSetXImpl bloqueausuario = new com.sun.sql.rowset.CachedRowSetXImpl();

try{

bloqueausuario.setDataSourceName("java:comp/env/jdbc/aces");

bloqueausuario.setTableName("aces.usuarios");

bloqueausuario.setCommand("update usuarios set estado = " + new Integer(0) +" where usr_id = " + this.getUserbloqueo());

bloqueausuario.execute();

bloqueausuario.commit();

} catch (Exception e) {

log("Exception occurred!!", e);

}

but it didn't work, because I checked in the database table and the state of the user remains "1"...

Any other idea?

Thank you

Yese

Yesenia at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 3

Are you sure the code was called? You do not mention how you invalidate the session to cause the destroy() method to be called.

Are you doing something like?

FacesContext context = FacesContext.getCurrentInstance ();

HttpSession session = (HttpSession) context.getExternalContext().getSession (false);

session.invalidate ();

dkible at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 4

Hi dkible,

yes, you are right, I didn't call that part of code you said, but I don't know where to place that code, could you tell me where? As far as I understand, that code forces the session variables to invalidate..., I'm not sure... feel free to correct me. What I need is that this code : com.sun.sql.rowset.CachedRowSetXImpl bloqueausuario = new com.sun.sql.rowset.CachedRowSetXImpl();

try{

bloqueausuario.setDataSourceName("java:comp/env/jdbc/aces");

bloqueausuario.setTableName("aces.usuarios");

bloqueausuario.setCommand("update usuarios set estado = " + new Integer(0) +" where usr_id = " + this.getUserbloqueo());

bloqueausuario.execute();

bloqueausuario.commit();

} catch (Exception e) {

log("Exception occurred!!", e);

}

can be executed with out even calling a button's action or any other event, just like it happens when you don't use your browser, and leave your application opened, the session variables invalidate by themselves by default...

Thank you so much for your help =)

Yese

Message was edited by:

Yesenia

Yesenia at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 5

I also have the same problem with conncurrent user login probelm , but my problem is if the user close the brower without logout , how can I trigger about the session close or session expired .

I found that

FacesContext context = FacesContext.getCurrentInstance ();

HttpSession session = (HttpSession) context.getExternalContext().getSession (false);

session.invalidate ();

can trigger event session1.destroy() .

but session expired or browse close can not trigger sessino1.destroy().

Could anyone help me to solve this problem ?

Thanks

mickeyleehk at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 6

The session can be inactivated programmatically as shown. If the session is inactive for a period of time, the server will invalidate the session, calling the destroy method. This covers the situation where the user closes the browser window or leaves the page idle.

For testing, I'd add a test button with the inactivation code in the button action method,

dkible at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 7

Hi

yes, if the session the server automatically invalidates the session, but does it execute the destroy function of the SessionBean1? because I did as you suggested yesterday, but it didn't work, I wanted the state of the user in the database table was set to "0"...

but it didn't do it, or I don't know what happened...

Thank you

Yese

Yesenia at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 8

How are you verifying that destroy is not being called. Do you look in the server log to see whether some other problem is happening?

I put this code in my session bean's destroy method and, in web.xml, set the session to time out after 1 minute.

118public void destroy() {

119log("Session Bean Destroy Called");

120}

I ran the program and waited a minute. I then viewed the server log and saw the following. It proves that destroy gets called when the session times out due to inactivity. However, I guess once the session has expired, you can't write to the server log ;^)

[#|2006-08-01T18:06:05.886-0700|INFO|sun-appserver-pe8.2|org.apache.catalina.se ssion.ManagerBase|_ThreadID=42;|Session attribute event listener threw exception

java.lang.NullPointerException

at com.sun.rave.web.ui.appbase.FacesBean.getExternalContext(FacesBean.java:120)

at com.sun.rave.web.ui.appbase.FacesBean.log(FacesBean.java:293)

at sessiontimeout.SessionBean1.destroy(SessionBean1.java:119) << line 119****

jetsons at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...
# 9

i also set the session time out to 1 min , after that I received a error mesage in the log file

|2006-08-02T23:12:55.015+0800|INFO|sun-appserver-pe8.1_02|org.apache.catalina.s ession.ManagerBase|_ThreadID=20;|Session attribute event listener threw exception

java.lang.NullPointerException

at com.sun.rave.web.ui.appbase.FacesBean.getExternalContext(FacesBean.java:120)

at com.sun.rave.web.ui.appbase.FacesBean.log(FacesBean.java:293)

at webapplication3.SessionBean1.destroy(SessionBean1.java:155)

at com.sun.rave.web.ui.appbase.servlet.LifecycleListener.attributeRemoved(Lifecycl eListener.java:338)

at org.apache.catalina.session.StandardSession.removeAttribute(StandardSession.jav a:1411)

at org.apache.catalina.session.StandardSession.removeAttribute(StandardSession.jav a:1337)

at org.apache.catalina.session.StandardSessionFacade.removeAttribute(StandardSessi onFacade.java:154)

at com.sun.rave.web.ui.appbase.servlet.LifecycleListener.sessionDestroyed(Lifecycl eListener.java:221)

at org.apache.catalina.session.StandardSession.expire(StandardSession.java:709)

at org.apache.catalina.session.StandardSession.isValid(StandardSession.java:600)

at org.apache.catalina.session.StandardManager.processExpires(StandardManager.java :786)

at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java :4822)

at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChil dren(ContainerBase.java:1620)

at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Contain erBase.java:1609)

at java.lang.Thread.run(Thread.java:595)

|#]

it did not go into session.destroy()

Any clue to trigger a event ?

mickeyleehk at 2007-7-6 23:27:37 > top of Java-index,Development Tools,Java Tools...