You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Chris Owens <ct...@chris-owens.com> on 2013/07/25 06:27:04 UTC

Shared Persistence Context between Spring Beans and EJBs

I am trying to integrate some EJBs into a predominantly Spring application,
hosted on tomEE+ and I'm just not understanding how to get the persistence
context to propagate along with a transaction that starts in an EJB and
invokes a method from a Spring bean.

I have set up container managed datasources, transaction manager, and
entitymanager (here's a relevant snippet from an applicationContext.xml).


Anything that is entirely Spring or entirely EJB works just fine.  But, if I
get hold of a managed entity within an EJB and then invoke a method from a
Spring bean, passing the entity as an argument, the Spring bean thinks its
unmanaged.  This is in spite of the EJB method being declared
@TransactionAttribute and the Spring method being declared @Transactional
with propagation Mandatory. Somehow the persistence context is not
propagating with the transaction.

Any ideas?



--
View this message in context: http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Chris Owens <ct...@chris-owens.com>.
This was the last straw for me with Spring -- I am accelerating the project
to get rid of Spring and will be testing the all-EJB configuration this
weekend. 



--
View this message in context: http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664395.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Did you isolate ejb/cdi from spring part  of your app? Different jars i mean

It can help
Le 26 juil. 2013 19:48, "Chris Owens" <ct...@chris-owens.com> a
écrit :

> And this, too, which is an openEJB problem.
>
>
>
> I have verified that the file exists and is readable;  its contents are:
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664375.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Chris Owens <ct...@chris-owens.com>.
And this, too, which is an openEJB problem.



I have verified that the file exists and is readable;  its contents are:




--
View this message in context: http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664375.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Chris Owens <ct...@chris-owens.com>.
This is a promising approach, but it is not working.  Simply including the
DeltaSpike dependencies in the Maven POM (and not changing anything else)
causes Spring to fail.   Including DeltaSpike in the pom causes every Spring
Bean that contains a @PersistenceContext annotation to throw this validation 
error:


I know this is not an openEJB problem, but any ideas?




--
View this message in context: http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664372.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Romain Manni-Bucau <rm...@gmail.com>.
personally i'd configure the EMF in spring simply getting the one of tomee
using the same trick (but producing the EMF)



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



2013/7/25 Chris Owens <ct...@chris-owens.com>

> So... putting it all together, if I understand correctly (I am in part
> writing this down because I suspect this thread will be useful to others).
> Please correct where I have it wrong:
>
> 1) WEB-INF/resources.xml defines the datasources.
>
> 2) META-INF/persistence.xml defines a persistence unit making reference to
> the datasources that were defined in resources.xml
>
> Those two alone are enough for OpenEJB to create an entitymanager,
> transactionmanager, etc.  Then...
>
> 3) META-INF/spring/applicationContext.xml contains the following, so that
> Spring and OpenEJB see the same transaction manager:
>
>
> 4) WEB-INF/web.xml contains a persistence-unit-ref that refers to the
> persistence unit defined in persistence.xml   (I have lost track of why I
> need this at this point.)
>
> 5) Add class EmProducer as per your message.
>
> 6) within EJBs, when I want an Entity Manager, I just do
>
>
> 7) Within Spring beans, when I want an Entity Manager, I do
>
>
> I still have a problem.  The Spring code is full of aspectj files
> (generated
> long ago by Spring Roo), each of which contains a @PersitenceContext
> annotation and an EntityManager field.  How, short of overriding Roo's
> madness and editing them all by hand to replace that code with the
> BeanProvider version, could I arrange things so that Spring injects the
> right EntityManager?
>
> And finally, Spring complains if you don't configure an
> EntityManagerFactory.  I think I could do the same thing for an EMF that
> you
> do for an EM in your EMProducer example.  Is there an obvious better way?
>
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664359.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Chris Owens <ct...@chris-owens.com>.
So... putting it all together, if I understand correctly (I am in part
writing this down because I suspect this thread will be useful to others). 
Please correct where I have it wrong:

1) WEB-INF/resources.xml defines the datasources.

2) META-INF/persistence.xml defines a persistence unit making reference to
the datasources that were defined in resources.xml

Those two alone are enough for OpenEJB to create an entitymanager,
transactionmanager, etc.  Then...

3) META-INF/spring/applicationContext.xml contains the following, so that
Spring and OpenEJB see the same transaction manager:


4) WEB-INF/web.xml contains a persistence-unit-ref that refers to the
persistence unit defined in persistence.xml   (I have lost track of why I
need this at this point.)

5) Add class EmProducer as per your message.

6) within EJBs, when I want an Entity Manager, I just do 


7) Within Spring beans, when I want an Entity Manager, I do 


I still have a problem.  The Spring code is full of aspectj files (generated
long ago by Spring Roo), each of which contains a @PersitenceContext
annotation and an EntityManager field.  How, short of overriding Roo's
madness and editing them all by hand to replace that code with the
BeanProvider version, could I arrange things so that Spring injects the
right EntityManager?

And finally, Spring complains if you don't configure an
EntityManagerFactory.  I think I could do the same thing for an EMF that you
do for an EM in your EMProducer example.  Is there an obvious better way?





--
View this message in context: http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664359.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Romain Manni-Bucau <rm...@gmail.com>.
I was thinking to:

public class EmProducer {
@PersistenceContext
private EntityManager em;

@Produces
public EntityManager em() {
  return em;
}
}

// then to get it

BeanProvider.getContextualReference(EntityManager.class);

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



2013/7/25 Chris Owens <ct...@chris-owens.com>

> Can you walk me through how to get my hands on the entity manager or emf so
> that I could expose it with deltaspike
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664356.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Chris Owens <ct...@chris-owens.com>.
Can you walk me through how to get my hands on the entity manager or emf so
that I could expose it with deltaspike 




--
View this message in context: http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664356.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hmm so your setup sounds right

the transaction manager used is a GeronimoTransactionManager, you can check
this class maybe.

The point is to know is spring doesn't use its own handling of
transactions, maybe try to reproduce in a smaller sample to be sure that's
a config error (openejb + spring in an embedded test would reproduce it)

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



2013/7/25 Chris Owens <ct...@chris-owens.com>

> Romain, thank you for your quick response!
>
> I used comp/env/comp/TransationManager based on your advice here:
> https://issues.apache.org/jira/browse/TOMEE-38.  I know I am getting /a/
> transaction manager; how do I know if I am getting the one I want?
>
> For the EMF, I have this in my web.xml.  Is this enough to get hold of the
> EMF and bind to it?  I'm using this info:
> http://tomee.apache.org/persistence-unit-ref.html.
>
>
>
> Can you help me find my way with the CDI/DeltaSpike approach?
> In particular,  I don't know how to find the EMF that TomEE automatically
> creates and then expose it.  I'm thinking I want something like
>
>
> And, umm... forgive my complete ignorance, but could you help me with the
> "easily doable in XML" part?  How do I get at the value returned by the
> static method within my xml file without creating another bean, etc. etc.
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664348.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Chris Owens <ct...@chris-owens.com>.
Romain, thank you for your quick response!

I used comp/env/comp/TransationManager based on your advice here:
https://issues.apache.org/jira/browse/TOMEE-38.  I know I am getting /a/
transaction manager; how do I know if I am getting the one I want?

For the EMF, I have this in my web.xml.  Is this enough to get hold of the
EMF and bind to it?  I'm using this info:
http://tomee.apache.org/persistence-unit-ref.html. 



Can you help me find my way with the CDI/DeltaSpike approach? 
In particular,  I don't know how to find the EMF that TomEE automatically
creates and then expose it.  I'm thinking I want something like


And, umm... forgive my complete ignorance, but could you help me with the
"easily doable in XML" part?  How do I get at the value returned by the
static method within my xml file without creating another bean, etc. etc.




--
View this message in context: http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343p4664348.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Shared Persistence Context between Spring Beans and EJBs

Posted by Romain Manni-Bucau <rm...@gmail.com>.
you are sure of:

<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName"
        value="java:comp/env/comp/TransactionManager"/>
</bean>

<jee:jndi-lookup id="entitiesEntityManagerFactory"
     jndi-name="persistence/entitiesPersistenceUnit"
     resource-ref="true"/>


?



To get the transaction manager java:comp/Transactionmanager would be
the default name, you can call the static method
org.apache.openejb.OpenEJB.getTransactionManager() too (easily doable
in xml ;). About the emf the jndi name is not the unit name so if you
didn't bind it somewhere else it will not work. An easy workaround can
be to produce the EMF through CDI and get it from BeanProvider of
DeltaSpike or manually (3 lines of code)


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



2013/7/25 Chris Owens <ct...@chris-owens.com>

> I am trying to integrate some EJBs into a predominantly Spring application,
> hosted on tomEE+ and I'm just not understanding how to get the persistence
> context to propagate along with a transaction that starts in an EJB and
> invokes a method from a Spring bean.
>
> I have set up container managed datasources, transaction manager, and
> entitymanager (here's a relevant snippet from an applicationContext.xml).
>
>
> Anything that is entirely Spring or entirely EJB works just fine.  But, if
> I
> get hold of a managed entity within an EJB and then invoke a method from a
> Spring bean, passing the entity as an argument, the Spring bean thinks its
> unmanaged.  This is in spite of the EJB method being declared
> @TransactionAttribute and the Spring method being declared @Transactional
> with propagation Mandatory. Somehow the persistence context is not
> propagating with the transaction.
>
> Any ideas?
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Shared-Persistence-Context-between-Spring-Beans-and-EJBs-tp4664343.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>