Hi Matteo,

Apologies for the lack of replies to this. I’m afraid that you hit the 
beginning of the August vacation season!

The fundamental issue that you have is that it isn’t possible for you to mix an 
external JTA TransactionManager with the Aries Transaction Control service. 
Even if Aries added a Transaction Control implementation with a “pluggable” 
backend TransactionManager then the Transaction Control service would still be 
responsible for managing the Transaction Lifecycle, which I think will not fit 
with what you want (i.e. you’re using the Karaf TransactionManager because you 
want other things to use those transactions). Furthermore, if you were using an 
external Transaction Manager then resource access that you perform in other 
components (without using transaction control) will actually be “separate” 
access (i.e. isolated from the changes you make using Transaction Control) 
because there’s nothing making sure that you use the same physical connection 
throughout the transaction.

In summary


  *   The exception that you’re seeing is then because, as you’ve rightly 
identified, the XA Aries Transaction Control needs access to an XADataSource so 
that it can enlist it in the transaction as needed. The DBCP DataSource isn’t 
an XADataSource, and can’t be unwrapped to get one, so this fails.
  *   If you want to use Transaction Control you need to to pass a raw 
XADataSource (the resource provider will pool it for you)
  *   The use of Transaction Control is separate from anything that you do with 
an external Transaction Manager, and the resource lifecycle is only managed 
when you are inside a Transaction Control scope.


I hope this helps,

All the best,

Tim

On 2 Aug 2019, at 14:24, Matteo Rulli 
<matteo.ru...@gmail.com<mailto:matteo.ru...@gmail.com>> wrote:

Hello

I'm currently working in a Karaf-based system where TransactionManager and 
DataSources are already provided through pax.jdbc.config, geronimo and aries 
aries.transaction.blueprint. I would like to introduce TransactionControl as 
provided by Aries and I'm trying to configure it with the following approach:

1. // Retrieve existing data source (provided via pax-jdbc):
2. DataSource dataSource = getService(bcontext, DataSource.class, 
"(osgi.jndi.service.name=target-persistence-unit)");
3. Map<String, Object> jpaProps = new HashMap<>();
4. jpaProps.put("javax.persistence.dataSource", dataSource);
5. EntityManagerFactoryBuilder emfb = getService(bcontext, 
EntityManagerFactoryBuilder.class);
6. JPAEntityManagerProviderFactory resourceProviderFactory = 
getService(bcontext, JPAEntityManagerProviderFactory.class);
7. EntityManager em = resourceProviderFactory.getProviderFor(emfb, jpaProps, 
jpaProps).getResource(txControl);

Unfortunately line 7 fails with

java.lang.IllegalArgumentException: The datasource supplied to create the 
EntityManagerFactory is not an XADataSource and so cannot be enlisted. Please 
provide either a javax.persistence.jtaDataSource, a 
javax.persistence.nonJtaDataSource, or a javax.persistence.dataSource which 
implements XADataSource
at 
org.apache.aries.tx.control.jpa.xa.impl.JPAEntityManagerProviderFactoryImpl.handleNormalDataSource(JPAEntityManagerProviderFactoryImpl.java:184)
at 
org.apache.aries.tx.control.jpa.xa.impl.JPAEntityManagerProviderFactoryImpl.getProviderFor(JPAEntityManagerProviderFactoryImpl.java:104)
at 
org.apache.aries.tx.control.jpa.common.impl.ResourceTrackingJPAEntityManagerProviderFactory.lambda$getProviderFor$0(ResourceTrackingJPAEntityManagerProviderFactory.java:43)
at 
org.apache.aries.tx.control.resource.common.impl.TrackingResourceProviderFactory.doGetResult(TrackingResourceProviderFactory.java:47)
at 
org.apache.aries.tx.control.jpa.common.impl.ResourceTrackingJPAEntityManagerProviderFactory.getProviderFor(ResourceTrackingJPAEntityManagerProviderFactory.java:43)
...

The Data Source is actually wrapped by DBCP2 and I guess this is why Aries 
fails to recognise it as a proper XA data source.

Given that I must use existing TransactionManager (as provided by OSGi JTA 
Service spec) and pax.jdbc.config DataSources, my questions are

  *   Is this the right approach to configure the TransactionControl? I have 
the feeling that something is missing in Aries to support "external" 
DataSources and TransactionManager
  *   Could you please provide guidance on how to achieve this? I think this is 
somehow related to this 
post<http://karaf.922171.n3.nabble.com/OSGi-Transaction-control-fails-with-hibernate-td4051418.html>:
 is the content of that thread still actual?

Many thanks,
Matteo

Reply via email to