4

My config is a bean that I inject in my code wherever I need it. However, when injected, I get a new instance of the bean instead of the one from the session.

My bean:

@Named
@SessionScoped
public class TestModel implements Serializable {

    private static final long serialVersionUID = 4873651498076344849L;

    private String version;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void changeVersion() {       
        this.version = "Version 2";
        System.out.println("New version : " + version + ", Object : " + this);
    }
}

When injected in different classes, all occurences are different instances. When annotating the bean with @ApplicationScoped, it is the same instance.

I do need the bean to be @SessionScoped since every user should have his own config.

The WebApp is running on TomEE 1.7.4

UPDATE: I created a new project to test it, and the SessionScope works. I now need to find out what is wrong with my current project in order to fix it.

Facets:

  • CDI 1.0
  • Dynamic Web Module 3.0
  • Java 1.8
  • JSF 2.2 (MyFaces impl from TomEE)
  • JPA 2.1

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Project</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>omega</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
  </context-param>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
</web-app>

faces-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

</faces-config>

beans.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.1" bean-discovery-mode="annotated">           
</beans>

Any ideas ?

Tim
  • 3,910
  • 8
  • 45
  • 80
  • I guess that the problem is a session scope. Because you might have modified a different `Config` (from another session). Please change it to `Singleton` or `ApplicationScoped` and check if problem still exists. – G. Demecki Jul 29 '16 at 14:51
  • Glad it worked but since `Singleton` and `ApplicationScoped` are basically the same, how did you solve your problem, when every user shall have his own config? – G. Demecki Aug 01 '16 at 06:46
  • You are right. I tried with two simultaneous sessions, and the values updated for every user. So back to square one. On session scoped, the value does not get updated in the servlet :-( – Tim Aug 01 '16 at 07:04
  • 4
    But to be honest: this doesn't make sense. `SessionScoped` should work just fine. Please make sure that during your tests you're using the same HTTP session for both requests (`request.getSession().getId()` has to be the same). – G. Demecki Aug 01 '16 at 07:25
  • You found the problem. The ID is not the same. How to fix this ? I sent the request from the same browser window just after updating the config. – Tim Aug 01 '16 at 07:36
  • But did the config was updated in the same browser window? Or did you invalidate your session somewhere in your code? – G. Demecki Aug 01 '16 at 08:31
  • I have a webapp with xhtml pages. Config.xhtml is used to update the config. The servlet runs in the background of that webapp. – Tim Aug 01 '16 at 08:34
  • Then I guess your `config.xhtml` doesn't update the `Config` object you think. Please update your question and include also some info how you use this config object on your page (and how `Config` bean is declared) – G. Demecki Aug 01 '16 at 08:48
  • 1
    If the session id is different each time, your problem lies there. Find out why the session is new/different each time... – Kukeltje Aug 01 '16 at 09:17
  • All right, I fixed the session ID. Still got the problem. Updating the first post. – Tim Aug 01 '16 at 11:27
  • What is the fully qualified name of @SessionScoped annotation? Is it `javax.enterprise.context` or `javax.faces.bean`? – AsSiDe Aug 01 '16 at 17:48
  • javax.enterprise.context – Tim Aug 01 '16 at 20:39
  • I am getting two different IDs when comparing HttpServletRequest.getSession().getId() vs FacesContext..getExternalContext().getSession.getId(). Shouldn't it be the same ? – Tim Aug 02 '16 at 11:00
  • Any more suggestions ? I am about to give up – Tim Aug 03 '16 at 07:13
  • Try in a different container? E.g. wildfly (and read carefully, I stated **try**, not use/switch in production). Then you know if the the container is the problem or not – Kukeltje Aug 03 '16 at 08:01
  • I know, but I have to use TomEE anyway. So if it does not work with it, I'll need to find a workaround. And accorting to Silarius, it works with Wildfly – Tim Aug 03 '16 at 08:50
  • Update: I found out that it is not limited to my servlet, but that the whole @SessionScoped does not work. – Tim Aug 03 '16 at 10:16
  • Found a similar case: http://stackoverflow.com/questions/14581666/example-for-cdi-sessionscoped-login-with-tomee – Tim Aug 03 '16 at 10:22

3 Answers3

2

Looks like your test doesn't work:

testModel object = model.TestModel@689a6064
New version : Version 2, Object : model.TestModel@61606aa6

So you update an instance which is not the same as the one linked to the session (another request not reusing the same session I'd say)

Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13
  • Well that's the problem. The bean I am updating when changing the version is not the same as the one injected in the Servlet. But it should be the same. Or I did not understand your comment – Tim Aug 01 '16 at 15:19
  • My comment was you don't show how you call "changeVersion()" so it can be you don't reuse the same session – Romain Manni-Bucau Aug 02 '16 at 07:16
1

You are doing it right. That is, from CDI perspective, you made no mistake and what you want is perfectly legit and should work (assuming you solved the problem of multiple sessions, which you did).

I just tried this with my own piece of code and it works as expected. You can check it on GitHub. The sample is more or less identical to yours.

However, I am running Wildfly 10 and therefore Weld 2.3 which comes with it (Weld being a reference impl of CDI). While you are running TomEE which contains OpenWebBeans (another CDI implementation).

To me it seems like you either missed some TomEE/OWB specific configuration (unrealistic scenario) or, more likely, you found a bug. In any case, if I were you, I would try asking on their forums or creating an issue in their tracking system because, once again, there is imho nothing wrong with your bean/servlet setup.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Siliarus
  • 6,393
  • 1
  • 14
  • 30
  • Thanks. I also have a Wildfly 10, I'll try it too. Then I'll ask OpenWebBeans. I'll let you know – Tim Aug 01 '16 at 14:07
  • I am not sure about the sessions after all: http://stackoverflow.com/questions/38718106/httpservletrequest-getsession-getid-vs-facescontext-getexternalcontext-ge – Tim Aug 02 '16 at 10:58
0

We have @SessionScope annotation in both JSF & CDI. Please review whether the annotation you are using in your old project is from JSF or from CDI. Find more on the difference between the annotation from JSF & CDI

Akash Mishra
  • 682
  • 1
  • 5
  • 13