0

I'm using apache karaf as an OSGI container, Aries Blueprint for declaring the beans and Hibernate 4.3.6 as JPA provider. The database is MySQL. I need to rollback a transaction when an exception is thrown. I`ll illustrate it with the following fragment of code:

  @Transactional(Transactional.TxType.REQUIRED)
  public void testTransactionMethod() {
    Role role = createRole();
    roleDao.save(role);

    User user = new User();
    userDao.save(user);
  }

Where the implementation of the dao.save() method for both of the DAOs is:

public T save(T entity) {
    return entityManager.merge(entity);
}

The save() method of the DAO is not annotated with @Transactional . My persistence.xml is the following:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.0">
  <persistence-unit name="examplePersistenceUnit" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/sqlds)</jta-data-source>
    <properties>
      <property name="hibernate.connection.url" value="jdbcURL"/>
      <property name="hibernate.connection.username" value="username"/>
      <property name="hibernate.connection.password" value="password"/>
      <property name="hibernate.connection.driver_class" value="DriverClass"/>

      <property name="hibernate.dialect" value="DB_Dialect"/>
      <property name="hibernate.show_sql" value="false"/>
      <property name="hibernate.hbm2ddl.auto" value="none"/>
    </properties>
  </persistence-unit>
</persistence>

What I expect is when userDao.save(user) throws an exception, the whole transaction to be rolled back and not having anything saved in my ROLE table, but actually there is a new Role entry in my database. I have also tried using @Transactional(rollbackOn = Exception.class), but still get the same result.

EDIT

As requested I add my dataSource cofiguration. I use pax-jdbc. I have two cfg files. First one is org.apache.aries.jpa.examplePersistenceUnit.cfg

hibernate.connection.url = jdbc:mysql://url/schema
hibernate.connection.username = username
hibernate.connection.password = password
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.hbm2ddl.auto = none

The second one is org.ops4j.datasource-example.cfg

osgi.jdbc.driver.name = mysql
    databaseName = ${user.name}
    dataSourceName = jdbc/sqlds
    url=jdbc:mysql://url/schema
    user=${user.name}
    password=${user.name}

Also in my blueprint I have the following:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"
    xmlns:tx="http://aries.apache.org/xmlns/transactions/v2.0.0"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
    <jpa:enable />
    <tx:enable />

1 Answers1

0

You have to use XA datasource.

You can do it, by modifying the value of osgi.jdbc.driver.name your cfg file.

See the link below for more information:

https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/61767716/Pooling+and+XA+support+for+DataSourceFactory