You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by bibhas <bi...@gmail.com> on 2012/12/18 23:09:31 UTC

TomEE doesn't persist @SessionScoped beans?

Hello,

Platform: TomEE 1.5.0 Web profile.

TomEE (and tomcat) persists objects in HTTP session. However, TomEE doesn't
seem to persist CDI managed beans in session scope. Is this a bug or a
feature?

This is my CDI managed bean:

@Named
@SessionScoped
public class Product implements Serializable {
	private static final long serialVersionUID = -7093352919359481489L;
...
}

I inject it in a test Servlet and use the bean:

    @Inject
    Product p;
    protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ... {
        logger.info("Product name: " + p.getName());
        p.setName("Baseball glove");
    }

The product object is not being persisted and does not survive a server
restart. What do I need to do to change this and have the CDI managed bean
persisted just like any other object in session?

Thanks.

Bibhas.



--
View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

You can edit the site and we ll validate the change.

@SessionScoped is not done for that, just to get a session scope = link
with a single client.
Le 25 juil. 2013 21:56, "zmirc" <m_...@yahoo.com> a écrit :

> I have been searching the whole day about how to get CDI @SessionScoped
> being
> persisted into session on disk, because that's how my log in system works.
> Finally, I've found the solution thanks to this thread.
>
> This property "openejb.session-context=http" should be listed on
> http://tomee.apache.org/properties-listing.html
> <http://tomee.apache.org/properties-listing.html>   also, because there
> might be more people having this issue, and it's a real pain in the ... to
> figure it out.
>
> Thanks a lot!
>
> BTW...why isn't this the default behavior? Isn't this one of the reason why
> @SessionScoped CDI exists?
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4664358.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: TomEE doesn't persist @SessionScoped beans?

Posted by zmirc <m_...@yahoo.com>.
I have been searching the whole day about how to get CDI @SessionScoped being
persisted into session on disk, because that's how my log in system works.
Finally, I've found the solution thanks to this thread.

This property "openejb.session-context=http" should be listed on 
http://tomee.apache.org/properties-listing.html
<http://tomee.apache.org/properties-listing.html>   also, because there
might be more people having this issue, and it's a real pain in the ... to
figure it out.

Thanks a lot!

BTW...why isn't this the default behavior? Isn't this one of the reason why
@SessionScoped CDI exists?



--
View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4664358.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
FYI: openejb.session-context=http will make it active on trunk:
https://issues.apache.org/jira/browse/TOMEE-676



Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2012/12/19 Romain Manni-Bucau <rm...@gmail.com>:
> remove the correctly and we agree ;) (joking)
>
> well JBoss persists stateful with extended entitymanagers too...no
> exception but it doesn't work...
>
> maybe open a jira on issues.apache.org, will be easier to track this feature
>
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
>
> 2012/12/19 bibhas <bi...@gmail.com>:
>> Thanks Romain, for the clarification.
>>
>> By the way, if this matters to anyone, I tested out with JBoss AS 7. It
>> correctly persists session scoped CDI managed beans, once persistence is
>> enabled for Tomcat.
>>
>>
>>
>> --
>> View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4659830.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
remove the correctly and we agree ;) (joking)

well JBoss persists stateful with extended entitymanagers too...no
exception but it doesn't work...

maybe open a jira on issues.apache.org, will be easier to track this feature

Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2012/12/19 bibhas <bi...@gmail.com>:
> Thanks Romain, for the clarification.
>
> By the way, if this matters to anyone, I tested out with JBoss AS 7. It
> correctly persists session scoped CDI managed beans, once persistence is
> enabled for Tomcat.
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4659830.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by bibhas <bi...@gmail.com>.
Thanks Romain, for the clarification.

By the way, if this matters to anyone, I tested out with JBoss AS 7. It
correctly persists session scoped CDI managed beans, once persistence is
enabled for Tomcat.



--
View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4659830.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
well the point is today you can configure session strategy, stateful
one but not CDI one. There is no global persist option.

clearly something we'll need to have a look (probably next year ;)

the best solution today is to store it in the session

that's said i just looked the code and 2 points:
1) when you shutdown a tomee the application is destroyed so beans too
2) the main point is the implementation of SessionContext whch doesn't
use the session today (that's in OpenWebBeans more than TomEE/OpenEJB)

Nothing important to implement but the point is then it will slow down
your app at runtime...

if you want to try to contribute just hack
org.apache.openejb.cdi.CdiAppContextsService#initSessionContext and
replace the line currentSessionContext = new SessionContext(); by a
session context using the http session

Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2012/12/19 bibhas <bi...@gmail.com>:
> Hi Romain,
>
> Let me explain more. In Tomcat session objects can be persisted on disk so
> that they survive application or server restart. This is configured in
> context.xml by setting a path name:
>
> <Manager pathname="some_path" />
>
> Persistence can be disabled by setting an empty path name. But, by default,
> persistence is enabled in Tomcat and TomEE.
>
>> that's probably not the default everybody expect.
>
> If someone doesn't want session persistence, they can easily disable that.
>
>> then the point is mainly with stateful beans
>
> I am not talking about EJB here. I am talking about plain POJO JavaBeans
> that are managed by CDI. I am not using any entity manager from this bean.
>
> @Named
> @SessionScoped
> public class MyBean implements Serializable {
> ...
> }
>
> I agree that trying to persist a session EJB that uses entity manager can be
> tricky. I won't even try something like that. What I am trying to do is
> persist plain JavaBeans that are in CDI session scope.
>
> The issue that I am trying to point out is that TomEE is being inconsistent.
> When persistence is enabled, regular session objects are persisted but not
> CDI beans. This is causing unnecessary headache for my application, nothing
> that can not be worked around with a bit of code. But, I will like to avoid
> this if possible.
>
> What do I need to do to persist CDI managed JavaBeans that are in session or
> conversation scope?
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4659823.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by bibhas <bi...@gmail.com>.
Hi Romain,

Let me explain more. In Tomcat session objects can be persisted on disk so
that they survive application or server restart. This is configured in
context.xml by setting a path name:

<Manager pathname="some_path" />

Persistence can be disabled by setting an empty path name. But, by default,
persistence is enabled in Tomcat and TomEE.

> that's probably not the default everybody expect. 

If someone doesn't want session persistence, they can easily disable that.

> then the point is mainly with stateful beans 

I am not talking about EJB here. I am talking about plain POJO JavaBeans
that are managed by CDI. I am not using any entity manager from this bean. 

@Named
@SessionScoped
public class MyBean implements Serializable {
...
}

I agree that trying to persist a session EJB that uses entity manager can be
tricky. I won't even try something like that. What I am trying to do is
persist plain JavaBeans that are in CDI session scope.

The issue that I am trying to point out is that TomEE is being inconsistent.
When persistence is enabled, regular session objects are persisted but not
CDI beans. This is causing unnecessary headache for my application, nothing
that can not be worked around with a bit of code. But, I will like to avoid
this if possible. 

What do I need to do to persist CDI managed JavaBeans that are in session or
conversation scope?



--
View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4659823.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
well why should it be done ;)?

that's not the case because that's probably not the default everybody expect.

but i think you are right if we say it can be desired.

then the poin tis mainly with stateful beans (which can be
sessionscoped): if you have an extended em you'll be tempted to
serialize it and even if it can work it is broken (you loose entity
state etc). So it shouldn't be done at all IMO


Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2012/12/19 bibhas <bi...@gmail.com>:
> My question is, if Tomcat persists all session objects on disk, shouldn't
> session scoped CDI beans also be persisted? Why is that not the case? What's
> the point in implementing Serializable then?
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4659821.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by bibhas <bi...@gmail.com>.
My question is, if Tomcat persists all session objects on disk, shouldn't
session scoped CDI beans also be persisted? Why is that not the case? What's
the point in implementing Serializable then?



--
View this message in context: http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814p4659821.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: TomEE doesn't persist @SessionScoped beans?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

You can put it in session normally

Note: dont do it with an extended entitymanager to stay consistent
Le 19 déc. 2012 07:24, "bibhas" <bi...@gmail.com> a écrit :

> Hello,
>
> Platform: TomEE 1.5.0 Web profile.
>
> TomEE (and tomcat) persists objects in HTTP session. However, TomEE doesn't
> seem to persist CDI managed beans in session scope. Is this a bug or a
> feature?
>
> This is my CDI managed bean:
>
> @Named
> @SessionScoped
> public class Product implements Serializable {
>         private static final long serialVersionUID = -7093352919359481489L;
> ...
> }
>
> I inject it in a test Servlet and use the bean:
>
>     @Inject
>     Product p;
>     protected void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ... {
>         logger.info("Product name: " + p.getName());
>         p.setName("Baseball glove");
>     }
>
> The product object is not being persisted and does not survive a server
> restart. What do I need to do to change this and have the CDI managed bean
> persisted just like any other object in session?
>
> Thanks.
>
> Bibhas.
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/TomEE-doesn-t-persist-SessionScoped-beans-tp4659814.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>