/**
* Verifies if the serRollbackOnly works.
*/
public void setRollbackOnly() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
// begins the transaction and sets the rollbackonly
entityManager.getTransaction().begin();
entityManager.getTransaction().setRollbackOnly();
// creates a book
Book book = new Book();
book.setId(ID);
book.setName(ENTITY_NAME);
entityManager.persist(book);
// verifies if the container registered the rollback.
assertTrue(entityManager.getTransaction().getRollbackOnly(),
"The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()");
// tries to make the commit, the container must throw an exception.
try {
entityManager.getTransaction().commit();
fail("The method setRollbackOnly was called, so the transaction cannot make the commit.");
} catch (RollbackException e) {
logger.info("The bean threw an expected exception");
}
entityManager.close();
// verifies if the transaction was rolled back
Book bookResult = entityManager.find(Book.class, new Integer(ID));
assertNull(bookResult, "The container did not make the rollback");
}