How apache aries recover failed transactions?

336 views Asked by At

I'm using Apache karaf 4.2.6 .I have the following config:

        <repository>mvn:org.ops4j.pax.jdbc/pax-jdbc-features/1.3.5/xml/features</repository>
<feature name="karaf-jpa-example-datasource" version="${project.version}">
    <config name="org.ops4j.datasource-demo">
        osgi.jdbc.driver.class=org.postgresql.Driver
        url=jdbc:postgresql://localhost:5432/demo
        xa=true
        pool=aries
        user=postgres
        password=postgres
        databaseName=demo
        dataSourceName=demo
    </config>
    <capability>
        osgi.service;objectClass=javax.sql.DataSource;effective:=active;osgi.jndi.service.name=demo
    </capability>
</feature>

<feature name="karaf-jpa-example-common" version="${project.version}">
    <feature>transaction</feature>
    <feature>jndi</feature>
    <feature>pax-jdbc-config</feature>
    <feature>pax-jdbc-postgresql</feature>
    <feature>pax-jdbc-pool-aries</feature>
    <feature>jdbc</feature>
    <feature dependency="true">aries-blueprint</feature>
    <feature version="[2,3)">jpa</feature>

    <feature version="[5,6)">hibernate</feature>
    <!--<feature version="[3,4)">openjpa3</feature>-->
    <bundle>mvn:org.apache.karaf.examples/karaf-jpa-example-provider-api/${project.version}</bundle>
</feature>

I made a simple method to make a transaction like this:

 @Transactional(Transactional.TxType.REQUIRES_NEW)
@Override
public void add(String flight, String customer) {
    int t = 0;
    for (;;) {
        System.out.println("adding new booking " + t);
        Booking booking = new Booking();
        booking.setCustomer(customer);
        booking.setFlight(flight);
        entityManager.persist(booking);

        t++;
        if (t > 10) {
            System.out.println("good bye ");
            Runtime.getRuntime().halt(-1);
        }
    }
}

the apache aries config (org.apache.aries.transaction.cfg) under /etc is this:

aries.transaction.recoverable=true
aries.transaction.timeout=10800
aries.transaction.howl.logFileDir=${karaf.data}/txlogTest
aries.transaction.howl.maxLogFiles=2
aries.transaction.howl.maxBlocksPerFile=512
org.apache.karaf.features.configKey = org.apache.aries.transaction

The problem is that it does not commit into the txlogTest log files .I'm wondering if Apache aries support recover failed transaction or not . Actually i remember when i have used websphere when it starts it recover all the failed transaction from tranlog folder. Can i make something like that with karaf/Apache Aries?

Thanks

1

There are 1 answers

0
majid vakili On

You may check out a great jump start Karaf JPA example here

You can use @Transactional annotation simply as follow:

@Transactional
public class BookingServiceImpl implements BookingService {

@PersistenceContext(unitName = "booking-hibernate")
private EntityManager entityManager;

public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}


 @Transactional(Transactional.TxType.REQUIRES_NEW)
 @Override
 public void add(String flight, String customer) {
     Booking booking = new Booking();
     booking.setCustomer(customer);
     booking.setFlight(flight);
     entityManager.persist(booking);
 }

}

According to this documentation transaction recovery depends on your resource which is recoverable or not:

aries.transaction.recoverable property is a flag to enable support of recoverable resource or not. A recoverable resource is a transactional object whose state is saved to stable storage if the transaction is committed, and whose state can be reset to what it was at the beginning of the transaction if the transaction is rolled back. At commit time, the transaction manager uses the two-phase XA protocol when communicating with the recoverable resource to ensure transactional integrity when more than one recoverable resource is involved in the transaction being committed. Transactional databases and message brokers like Apache ActiveMQ are examples of recoverable resources. A recoverable resource is represented using the javax.transaction.xa.XAResource interface in JTA. Default is true