karaf-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Schneider <ch...@die-schneider.net>
Subject Re: Karaf JTA and JDBC DataSource
Date Mon, 16 Mar 2015 08:29:29 GMT
I think the issue may be that your Connection does not take part in the 
transaction.

If you would do it by hand:
You use an XADataSource, create an XAconnection and get the XAResource 
from it. Then you would also need to inject the TransactionManager, 
create a Transaction and call  enlistResource with the XAResource from 
above. Without that step the db connection will simply not participate 
in the transaction. It is not recommended to do this by hand.

Instead use the following approach:
What you need is an XADataSource for your database. You then need to 
wrap it to handle the pooling and XA management (for example with dbcp2).
The result will be a normal DataSource that you then use for jdbc.

As this is quite complicated and can easily go wrong when doing by hand 
I suggest you use pax-jdbc to do the wrapping for you:
See 
https://ops4j1.jira.com/wiki/display/PAXJDBC/Pooling+and+XA+support+for+DataSourceFactory
It is simple and works nicely.

Btw. The reason why you do not absolutely need to do this with JPA is 
that aries also does the wrapping under the covers. Aries does not 
configure pooling though. So even with aries you should use pax jdbc.

Please also see my tutorials for complete examples:
http://www.liquid-reality.de/display/liquid/2012/01/13/Apache+Karaf+Tutorial+Part+6+-+Database+Access
http://www.liquid-reality.de/display/liquid/2015/03/05/Apache+Karaf+Tutorial+Part+9+-+Annotation+based+blueprint+and+JPA

You should even be able to combine the approach of tutorial 9 to use 
annotation based transactions with jdbc.

Christian


On 16.03.2015 08:41, ellirael wrote:
> Hi. When I use JTA with JPA everything works fine. Blueprint example:
>      <bean id="jpaPersonService" class="mynamespace.JpaPersonService">
>          <jpa:context property="entityManager" unitname="kb" />
>          <tx:transaction method="addPerson changePerson deletePerson"
> value="Required" />
>      </bean>
>
> When I use JTA with JDBC DataSource transaction doesn't start. Blueprint
> example:
>      <reference id="dataSource" interface="javax.sql.DataSource"
>                 filter="(osgi.jndi.service.name=jdbc/kb)" />
>      <bean id="jdbcPersonService" class="mynamespace.JdbcPersonService">
>          <property name="dataSource" ref="dataSource" />
>          <tx:transaction method="addPerson changePerson deletePerson"
> value="Required" />
>      </bean>
>
> How can I do JTA and JDBC to be friends?
>
>
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/Karaf-JTA-and-JDBC-DataSource-tp4039081.html
> Sent from the Karaf - User mailing list archive at Nabble.com.


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
http://www.talend.com


Mime
View raw message