package org.pandaa.test;

import javax.transaction.UserTransaction;

import bitronix.tm.TransactionManagerServices;

public final class TransactionHelper {
	
	
	private TransactionHelper() {}
	
	public static void inTransaction(Runnable r) {
		UserTransaction ut = TransactionManagerServices.getTransactionManager();
		try {
			ut.begin();
			r.run();
			ut.commit();
		} catch (Exception e) {
			try {
				ut.rollback();
			} catch (Exception e1) {
				throw new RuntimeException("Failed to rollback transaction", e1);
			} 
			throw new RuntimeException("Failed to commit user transaction", e);
		}
	}
}