Tagged: jta

Overriding JTA into RESOUCE-LOCAL while Unit Testing


references

JTA

Let’s say you have a very simple persistence.xml looks like this.

<persistence-unit name="somePU" transaction-type="JTA">
  <jta-data-source>jdbc/someDS</jta-data-source>
  <exclude-unlisted-classes>false</exclude-unlisted-classes>
  <properties>
    <property name="javax.persistence.validation.mode" value="CALLBACK" />
  </properties>
</persistence-unit>

RESOUCE-LOCAL

You can, maybe you should, override it for unit testing. Following codes uses EclipseLink dependent API.

final Map<String, String> properties = new HashMap<>();
properties.put(PersistenceUnitProperties.TRANSACTION_TYPE,
               PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
properties.put(PersistenceUnitProperties.JDBC_DRIVER,
               "org.apache.derby.jdbc.EmbeddedDriver");
properties.put(PersistenceUnitProperties.JDBC_URL,
               "jdbc:derby:memory:testDB;create=true");
properties.put(PersistenceUnitProperties.DDL_GENERATION,
               PersistenceUnitProperties.CREATE_ONLY);
properties.put(PersistenceUnitProperties.LOGGING_LEVEL,
               Level.FINE.getName());
properties.put(PersistenceUnitProperties.TARGET_SERVER,
               TargetServer.None);
ENTITY_MANAGER_FACTORY
    = createEntityManagerFactory("somePU", properties);

various ways to persist entities in jax-rs resources


references

via EJB

@Stateless
public class TestService {

    public Test persist(final Test test) {
        em.persist(test);
        return test;
    }

    @PersistenceContext(unitName = "pu")
    private EntityManager em;
}
@Path("/tests")
public class TestsResource {

    @POST
    @Path("/1")
    public void create1(final Test test) {
        ts.persist(test);
    }

    @EJB
    private TestService ts;
}

via PersistenceUnit

@POST
@Path("/2")
public void create2(final Test test) throws Exception {

    final EntityManager em = emf.createEntityManager();
    try {
        try {
            ut.begin();
            em.joinTransaction();
            em.persist(test);
            ut.commit();
        } catch (final Exception e) {
            ut.rollback();
            throw e;
        }
    } finally {
        em.close();
    }
}

@POST
@Path("/3")
public void create3(final Test test) throws Exception {

    try {
        ut.begin();
        final EntityManager em = emf.createEntityManager();
        try {
            em.persist(test);
        } finally {
            em.close();
        }
        ut.commit();
    } catch (final Exception e) {
        ut.rollback();
        throw e;
    }
}

@PersistenceUnit(unitName = "pu")
private transient EntityManagerFactory emf;

@Resource
private UserTransaction ut;

via PersistenceContext

@POST
@Path("/4")
public void create4(final Test test) throws Exception {

    try {
        ut.begin();
        em.joinTransaction();
        em.persist(test);
        ut.commit();
    } catch (final Exception e) {
        ut.rollback();
        throw e;
    }
}

@PersistenceContext(unitName = "pu")
private transient EntityManager em;

@Resource
private UserTransaction ut;

via PersistenceContext with Transactional Interceptors

@POST
@Path("/5")
@Transactional(rollbackOn = Exception.class)
public void create5(final Test Test) throws Exception {

    em.persist(test);
}

@PersistenceContext(unitName = "pu")
private transient EntityManager em;