Back Forum Reply New

Two JPA persistence units

I have two persistence units which I am loading into a org..orm.jpa.persistenceunit.Defaul  tPersistenceUnitManager - the configuration is like this;Code: lt;bean name=quot;dataSourcequot; class=quot;org..jndi.JndiObjectFactoryBeanquot;gt;   lt;property name=quot;jndiNamequot; value=quot;java:comp/env/jdbc/myDSquot; /gt; lt;/beangt;  lt;context:load-time-weaver weaver-class=quot;org..instrument.classloading.InstrumentationLoadTimeWeaverquot; /gt; lt;context:annotation-config /gt; lt;tx:annotation-driven transaction-manager=quot;transactionManagerquot;/gt; lt;tx:annotation-driven transaction-manager=quot;transactionManagerElecquot;/gt;
lt;bean id=quot;pumquot; class=quot;org..orm.jpa.persistenceunit.DefaultPersistenceUnitManagerquot;gt;   lt;property name=quot;persistenceXmlLocationsquot;gt;     lt;listgt;       lt;valuegt;classpath*:META-INF/first-persistence.xmllt;/valuegt;       lt;valuegt;classpath*:META-INF/second-persistence.xmllt;/valuegt;     lt;/listgt;   lt;/propertygt;   lt;property name=quot;dataSourcesquot;gt;     lt;mapgt;       lt;entry key=quot;firstDSquot; value-ref=quot;dataSourcequot; /gt;       lt;entry key=quot;secondDSquot; value-ref=quot;dataSourcequot; /gt;     lt;/mapgt;   lt;/propertygt;   lt;property name=quot;defaultDataSourcequot; ref=quot;dataSourcequot; /gt; lt;/beangt;
lt;bean id=quot;jpaDialectquot; class=quot;org..orm.jpa.vendor.HibernateJpaDialectquot; /gt; lt;bean id=quot;jpaVendorAdapterquot; class=quot;org..orm.jpa.vendor.HibernateJpaVendorAdapterquot; p:database=quot;ORACLEquot; /gt;   lt;bean id=quot;transactionManagerquot; class=quot;org..orm.jpa.JpaTransactionManagerquot;gt;  lt;qualifier value=quot;firstPUquot;/gt;   lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot; /gt;   lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryfirstquot; /gt;   lt;property name=quot;jpaDialectquot; ref=quot;jpaDialectquot;/gt; lt;/beangt;  lt;bean id=quot;transactionManagerSecondquot; class=quot;org..orm.jpa.JpaTransactionManagerquot;gt;   lt;qualifier value=quot;secondPUquot;/gt;   lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot; /gt;   lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactorySecondquot; /gt;   lt;property name=quot;jpaDialectquot; ref=quot;jpaDialectquot;/gt; lt;/beangt;
lt;bean id=quot;entityManagerFactoryFirstquot; class=quot;org..orm.jpa.LocalContainerEntityManagerFactoryBeanquot;gt;   lt;property name=quot;jpaVendorAdapterquot; ref=quot;jpaVendorAdapterquot;/gt;   lt;property name=quot;persistenceUnitManagerquot; ref=quot;pumquot; /gt;   lt;property name=quot;persistenceUnitNamequot; value=quot;firstquot; /gt; lt;/beangt;
lt;bean id=quot;entityManagerFactorySecondquot; class=quot;org..orm.jpa.LocalContainerEntityManagerFactoryBeanquot;gt;   lt;property name=quot;jpaVendorAdapterquot; ref=quot;jpaVendorAdapterquot;/gt;   lt;property name=quot;persistenceUnitManagerquot; ref=quot;pumquot; /gt;   lt;property name=quot;persistenceUnitNamequot; value=quot;secondquot; /gt; lt;/beangt;
The problem that I have is that a service bean, marked with @Transactional and using the firstPU defined in first-persistence.xml (Injected via @ @PersistenceContext(unitName=quot;firstPUquot;) on the entity manager field) calls a service bean method marked @Transactional(propagation=Propagation.REQUIRES_NE  W)  which uses the secondPU defined in second-persistence.xml (Injected via @ @PersistenceContext(unitName=quot;secondPUquot;) on its entity manager field) ... the problem is the transaction NEVER propagates at all to the second PU. The entities in the first PU save just fine but the secondPU always gives me quot;javax.persistence.TransactionRequiredExceptio  n: no transaction is in progressquot; ... I'm assumming here because there are two transaction managers, two entityManagers, etc meaning the transactions aren't sharing any context? I've tried using JTATransactionManagers and hooking them up to the JOTM configured  in Jetty but at that point I always seems to hit a Jetty brick wall getting it configured. My actual production target app server is glassfish.

I have spent two days configuring various types of transaction managers - JTA, non-JTA, it's driving me mad.

The section in the manual quot;13.5.1.4 Dealing with multiple persistence unitsquot; doesn't show what the transaction manager configuration is ... I can't find any relevant information to make this work. Does anyone have any further information or can illuminate me as to where to look next. I am exhausted and seriously considering converting everything to EJB3 as I've done exacly this in that environment with a Hibernate provider in the past.

thanks for any help
scot

The long and short of it is you'll need a JtaTransactionManager once your transactions are spanning multiple datasources.  Obviously, this goes back to your problems configuring JOTM/Jetty.

One thought:  Glassfish isn't THAT hard to deploy locally, and it's fairly lightweight.  You might want to use that for development instead of Jetty.

Hope this helps
- Don


Originally Posted by dbrinkerThe long and short of it is you'll need a JtaTransactionManager once your transactions are spanning multiple datasources.  Obviously, this goes back to your problems configuring JOTM/Jetty.

One thought:  Glassfish isn't THAT hard to deploy locally, and it's fairly lightweight.  You might want to use that for development instead of Jetty.

Hope this helps
- Don

Yes it does thanks, it looks like I'm stuck between a rock and a hard place having two persistence units and trying to use Jetty for tests. Given this scenario, it's as you say, the Jetty/JTOM configuration is the one that has to give way. I'll need to look at if I can boot glassfish through maven for automated integration testing, as in the way you use the jetty:run goal in maven. But that is probably easier than configuring Jetty/JTOM ...

thanks

This guy's blog entry 2009/0...naged-jpa.html has helped me a great deal get it going with glassfish -
¥
Back Forum Reply New