Java Code Examples for javax.persistence.EntityTransaction#getRollbackOnly()

The following examples show how to use javax.persistence.EntityTransaction#getRollbackOnly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MCRURNGranularRESTRegistrationTask.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private static void endTransaction(EntityTransaction tx) {
    if (tx != null && tx.isActive()) {
        if (tx.getRollbackOnly()) {
            tx.rollback();
        } else {
            try {
                tx.commit();
            } catch (RollbackException e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
                throw e;
            }
        }
    }
}
 
Example 2
Source File: MCRJPATestCase.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private void doSchemaOperation(Function<String, String> schemaFunction) {
    EntityManager currentEntityManager = MCREntityManagerProvider.getCurrentEntityManager();
    EntityTransaction transaction = currentEntityManager.getTransaction();
    try {
        transaction.begin();
        getDefaultSchema().ifPresent(
            schemaFunction
                .andThen(currentEntityManager::createNativeQuery)
                .andThen(Query::executeUpdate)::apply);
    } finally {
        if (transaction.isActive()) {
            if (transaction.getRollbackOnly()) {
                transaction.rollback();
            } else {
                transaction.commit();
            }
        }
    }
}
 
Example 3
Source File: TransactionalInterceptor.java    From BeanTest with Apache License 2.0 6 votes vote down vote up
/**
 * Commits the current transaction if it is not already marked as rollback via the {@link EntityTransaction#getRollbackOnly()} method.
 * In that case, a rollback will be executed.
 */
private void processTransaction() throws Exception {
    EntityTransaction transaction = em.getTransaction();
    try {
        
        if (em.isOpen() && transaction.isActive() && isFirstInterceptor()) {
            if (transaction.getRollbackOnly()) {
                transaction.rollback();
                LOGGER.debug("Transaction was rollbacked");
            } else {
                transaction.commit();
                LOGGER.debug("Transaction committed");
            }
            em.clear();
        }
    } catch (Exception e) {
        LOGGER.warn("Error when trying to commit transaction: {0}", e);
        throw e;
    } finally {
        INTERCEPTOR_COUNTER--;
    }

}
 
Example 4
Source File: JerseyConfig.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {

    log.info("[I68] <<< filter");
    EntityManager em = (EntityManager) httpRequest.getAttribute(EM_REQUEST_ATTRIBUTE);

    if (!"GET".equalsIgnoreCase(requestContext.getMethod())) {
        EntityTransaction t = em.getTransaction();
        if (t.isActive()) {
            if (!t.getRollbackOnly()) {
                t.commit();
            }
        }
    }

    em.close();

}
 
Example 5
Source File: JPATool.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public <E> E transactional(Callable<E> action, boolean readOnly) {
	ensureNotInRollbackOnlyTransation();

	EntityTransaction tx = em.getTransaction();
	U.notNull(tx, "transaction");

	boolean newTx = !tx.isActive();

	if (newTx) {
		tx.begin();
	}

	if (readOnly) {
		tx.setRollbackOnly();
	}

	try {
		E result = action.call();

		if (newTx) {
			if (tx.getRollbackOnly()) {
				tx.rollback();
			} else {
				tx.commit();
			}
		}

		return result;

	} catch (Throwable e) {

		if (newTx) {
			if (tx.isActive()) {
				tx.rollback();
			}
		}

		throw U.rte("Transaction execution error, rolled back!", e);
	}
}
 
Example 6
Source File: TransactionUtils.java    From testfun with Apache License 2.0 5 votes vote down vote up
public static void rollbackTransaction() {
    EntityTransaction tx = getTransaction();
    if (tx.isActive() && !tx.getRollbackOnly()) {
        // only flag the transaction for rollback - actual rollback will happen when the method starting the transaction is done
        tx.setRollbackOnly();
    }
}
 
Example 7
Source File: TransactionUtils.java    From testfun with Apache License 2.0 5 votes vote down vote up
public static void endTransaction(boolean newTransaction) {
    EntityTransaction tx = getTransaction();
    if (tx.isActive() && newTransaction) {
        if (tx.getRollbackOnly()) {
            // Rollback the transaction and close the connection if roll back was requested deeper in the stack and this is the method starting the transaction
            tx.rollback();
        }

        else {
            // Commit the transaction only if this is the method starting the transaction and rollback wasn't requested
            tx.commit();
        }
    }
}
 
Example 8
Source File: JpaTransactionManager.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean isRollbackOnly() {
	EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction();
	return tx.getRollbackOnly();
}
 
Example 9
Source File: JpaTransactionManager.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean isRollbackOnly() {
	EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction();
	return tx.getRollbackOnly();
}
 
Example 10
Source File: JpaTransactionManager.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean isRollbackOnly() {
	EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
	return tx.getRollbackOnly();
}
 
Example 11
Source File: JpaTransactionManager.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isRollbackOnly() {
	EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
	return tx.getRollbackOnly();
}
 
Example 12
Source File: JPADAO.java    From rome with Apache License 2.0 4 votes vote down vote up
@Override
public List<? extends Subscriber> subscribersForTopic(final String topic) {
    final LinkedList<JPASubscriber> result = new LinkedList<JPASubscriber>();
    final EntityManager em = factory.createEntityManager();
    final EntityTransaction tx = em.getTransaction();
    tx.begin();

    final Query query = em.createNamedQuery("Subcriber.forTopic");
    query.setParameter("topic", topic);

    try {
        @SuppressWarnings("unchecked")
        final List<JPASubscriber> subscribers = query.getResultList();
        for (final JPASubscriber subscriber : subscribers) {
            if (subscriber.getLeaseSeconds() == -1) {
                result.add(subscriber);
                continue;
            }

            if (subscriber.getSubscribedAt().getTime() < System.currentTimeMillis() - 1000 * subscriber.getLeaseSeconds()) {
                subscriber.setExpired(true);
            } else {
                result.add(subscriber);
            }

            if (subscriber.isExpired() && purgeExpired) {
                em.remove(subscriber);
            }
        }
    } catch (final NoResultException e) {
        tx.rollback();
        em.close();

        return result;
    }

    if (!tx.getRollbackOnly()) {
        tx.commit();
    } else {
        tx.rollback();
    }

    em.close();

    return result;
}