Is it possible to do something like this: When a user session starts I read a certain integral attribute from the database. As the user performs certain activities in this session, I update that variable(stored in session) & when the session ends, then I finally store that value to the DB.
My question is how do I identify using the JSF framework if the user session has ended & I should then store the value back to DB?
Apart from the HttpSessionListener
, you can use a session scoped managed bean for this. You use @PostConstruct
(or just the bean's constructor) and @PreDestroy
annotations to hook on session creation and destroy
@ManagedBean
@SessionScoped
public class SessionManager {
@PostConstruct
public void sessionInitialized() {
// ...
}
@PreDestroy
public void sessionDestroyed() {
// ...
}
}
The only requirement is that this bean is referenced in a JSF page or as @ManagedProperty
of any request scoped bean. Otherwise it won't get created. But in your case this should be no problem as you're apparently already using a session scoped managed bean, just adding a @PreDestroy
method ought to be sufficient.