0

We need to do some DB cleanup on session timeout, so implemented HttpSessionBindingListener and added an object to session on user login, we never remove it from session explicitly.

HttpSessionBindingListener.valueUnbound is triggered if we manually call session.invalidate but the problem is it doesn't get triggered on session timeout. I see an error in the console but not sure what the problem is.

Set the object into session on login and invalidate session on logout:

@Named("logincontroller")
@Stateful
public class LoginController implements ILoginController, Serializable {

  @Inject
  private Credentials credentials;
  private ExternalContext ec = null;
  private HttpServletRequest request =null;
  private HttpServletResponse response=null;
  private HttpSession session=null;

  @PostConstruct
  private void getLocalVariables() {
    ec = FacesContext.getCurrentInstance().getExternalContext();
    request= (HttpServletRequest)ec.getRequest();
    session = request.getSession();
  }

  @Override
  public boolean login() {
    ... 
    credentials.setUserName(getUserName().toUpperCase());
    credentials.setUserPassword(getPassword());
    // set the object into session on user login
    session.setAttribute("credentials", credentials); 
  }

  @Override
  public void logout() {
        ...
        try {
            response.sendRedirect(path+"/faces/Exit.html");
            // invalidate the session on logout
            session.invalidate(); 
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
  }
}

Credentials object implementing HttpSessionBindingListener

@Named("credentials")
@SessionScoped
public class Credentials implements ICredentials, Serializable, HttpSessionBindingListener {
  ...
  @Override
  public void valueBound(HttpSessionBindingEvent event) {
    //do nothing
  }

  @Override
  public void valueUnbound(HttpSessionBindingEvent event) {
    try {
        // run DB scripts to clean up
        lockManager.releaseAllLocksForUser(getUserName().toUpperCase()); 
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

Error Stacktrace:

15:13:03,252 INFO  [org.jboss.as.repository] (ServerService Thread Pool -- 66) WFLYDR0009: Content C:<path>jbossstudio10\runtimes\jboss-eap\standalone\data\content\62\7ccffda3936daab4d3148eb2e51584f8372592 is obsolete and will be removed
15:13:03,299 INFO  [org.jboss.as.repository] (ServerService Thread Pool -- 66) WFLYDR0002: Content removed from location C:\<path>\jbossstudio10\runtimes\jboss-eap\standalone\data\content\62\7ccffda3936daab4d3148eb2e51584f8372592\content
15:26:36,245 ERROR [stderr] (default task-22) Exception in thread "default task-22" org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type javax.enterprise.context.SessionScoped
15:26:36,246 ERROR [stderr] (default task-22)   at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:689)
15:26:36,246 ERROR [stderr] (default task-22)   at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:90)
15:26:36,246 ERROR [stderr] (default task-22)   at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.getIfExists(ContextualInstanceStrategy.java:165)
15:26:36,246 ERROR [stderr] (default task-22)   at org.jboss.weld.bean.ContextualInstance.getIfExists(ContextualInstance.java:63)
15:26:36,246 ERROR [stderr] (default task-22)   at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:83)
15:26:36,246 ERROR [stderr] (default task-22)   at org.jboss.weld.bean.proxy.ProxyMethodHandler.getInstance(ProxyMethodHandler.java:125)
15:26:36,246 ERROR [stderr] (default task-22)   at com.facility.security.Credentials$Proxy$_$$_WeldClientProxy.valueUnbound(Unknown Source) // unknown source??
15:26:36,246 ERROR [stderr] (default task-22)   at io.undertow.servlet.core.SessionListenerBridge.attributeRemoved(SessionListenerBridge.java:132)
15:26:36,247 ERROR [stderr] (default task-22)   at io.undertow.server.session.SessionListeners.attributeRemoved(SessionListeners.java:81)
15:26:36,247 ERROR [stderr] (default task-22)   at io.undertow.server.session.InMemorySessionManager$SessionImpl.removeAttribute(InMemorySessionManager.java:500)
15:26:36,247 ERROR [stderr] (default task-22)   at io.undertow.servlet.core.SessionListenerBridge.sessionDestroyed(SessionListenerBridge.java:72)
15:26:36,247 ERROR [stderr] (default task-22)   at io.undertow.server.session.SessionListeners.sessionDestroyed(SessionListeners.java:61)
15:26:36,248 ERROR [stderr] (default task-22)   at io.undertow.server.session.InMemorySessionManager$SessionImpl.invalidate(InMemorySessionManager.java:528)
15:26:36,248 ERROR [stderr] (default task-22)   at io.undertow.server.session.InMemorySessionManager$SessionImpl$2$1.run(InMemorySessionManager.java:357)
15:26:36,248 ERROR [stderr] (default task-22)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
15:26:36,248 ERROR [stderr] (default task-22)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
15:26:36,248 ERROR [stderr] (default task-22)   at java.lang.Thread.run(Thread.java:748)

Do I need to get a new session in valueUnbound to run my DB scripts? I thought that we still have session when valueUnbound is called. I did search stackoverflow but to no avail.

Any help is greatly appreciated. Env: Windows 7 Enterrpise, JDK 1.8, JBoss EAP 7.0.0, CDI 1.2, Mojarra 2.2.12-jbossorg-2, deltaspike 1.8.1, Servlets 3.1, PrimeFaces 6.1, Oracle 11g

1 Answer 1

2

I've never really worked with HttpSessionBindingListener.valueUnbound, but it looks like the ordering of events is a bit jumbled between CDI and servlet behaviour. Specifically it looks like CDI is told to tear down session beans before HttpSessionBindingListener.valueUnbound is invoked. Hence the exception - when the method should be invoked, you no longer have session context active.

Possible solution is to twist your code around and not use HttpSessionBindingListener.valueUnbound and instead make Credentials.valueUnbound a @PreDestroy method. That way it should be invoked by CDI whenever that bean is going to be destroyed, no matter if the cause is session invalidation or timeout.

1
  • thank you Siliarus, adding @PreDestroy method resolved my issue, the method fires both on session timeout and session invalidate Feb 26, 2018 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.