Search in sources :

Example 66 with EntityTransaction

use of javax.persistence.EntityTransaction in project opencast by opencast.

the class SchedulerServiceDatabaseImpl method deleteEvent.

@Override
public void deleteEvent(String mediapackageId) throws NotFoundException, SchedulerServiceDatabaseException {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        Opt<ExtendedEventDto> entity = getExtendedEventDto(mediapackageId, em);
        if (entity.isNone()) {
            throw new NotFoundException("Event with ID " + mediapackageId + " does not exist");
        }
        em.remove(entity.get());
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        if (tx.isActive())
            tx.rollback();
        logger.error("Could not delete extended event: {}", getStackTrace(e));
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException) SchedulerServiceDatabaseException(org.opencastproject.scheduler.impl.SchedulerServiceDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 67 with EntityTransaction

use of javax.persistence.EntityTransaction in project joynr by bmwcarit.

the class DiscoveryEntryStorePersisted method add.

/*
     * (non-Javadoc)
     * @see io.joynr.capabilities.CapabilitiesStore#add(io.joynr.
     * capabilities .DiscoveryEntry)
     */
@Override
public synchronized void add(DiscoveryEntry discoveryEntry) {
    logger.debug("adding discovery entry: {}", discoveryEntry);
    if (!(discoveryEntry instanceof GlobalDiscoveryEntryPersisted)) {
        return;
    }
    GlobalDiscoveryEntryPersisted globalDiscoveryEntry = (GlobalDiscoveryEntryPersisted) discoveryEntry;
    if (globalDiscoveryEntry.getDomain() == null || globalDiscoveryEntry.getInterfaceName() == null || globalDiscoveryEntry.getParticipantId() == null || globalDiscoveryEntry.getAddress() == null) {
        String message = "discoveryEntry being registered is not complete: " + discoveryEntry;
        logger.error(message);
        throw new JoynrCommunicationException(message);
    }
    GlobalDiscoveryEntryPersisted discoveryEntryFound = entityManager.find(GlobalDiscoveryEntryPersisted.class, discoveryEntry.getParticipantId());
    EntityTransaction transaction = entityManager.getTransaction();
    try {
        transaction.begin();
        if (discoveryEntryFound != null) {
            entityManager.merge(discoveryEntry);
        } else {
            entityManager.persist(discoveryEntry);
        }
        transaction.commit();
    } catch (Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        logger.error("unable to add discoveryEntry: " + discoveryEntry, e);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException)

Example 68 with EntityTransaction

use of javax.persistence.EntityTransaction in project joynr by bmwcarit.

the class DiscoveryEntryStorePersisted method touch.

@Override
public void touch(String clusterControllerId) {
    String query = "from GlobalDiscoveryEntryPersisted where clusterControllerId=:clusterControllerId";
    EntityTransaction transaction = entityManager.getTransaction();
    try {
        transaction.begin();
        @SuppressWarnings("unchecked") List<DiscoveryEntry> capabilitiesList = entityManager.createQuery(query).setParameter("clusterControllerId", clusterControllerId).getResultList();
        for (DiscoveryEntry discoveryEntry : capabilitiesList) {
            logger.trace("  --> BEFORE entry {} last seen {}", discoveryEntry.getParticipantId(), discoveryEntry.getLastSeenDateMs());
            ((GlobalDiscoveryEntryPersisted) discoveryEntry).setLastSeenDateMs(System.currentTimeMillis());
            logger.trace("  --> AFTER  entry {} last seen {}", discoveryEntry.getParticipantId(), discoveryEntry.getLastSeenDateMs());
        }
        transaction.commit();
    } catch (RuntimeException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        logger.error("Error updating last seen date for cluster controller with ID {}", clusterControllerId, e);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) DiscoveryEntry(joynr.types.DiscoveryEntry)

Example 69 with EntityTransaction

use of javax.persistence.EntityTransaction in project joynr by bmwcarit.

the class DiscoveryEntryStorePersisted method removeCapabilityFromStore.

private boolean removeCapabilityFromStore(String participantId) {
    GlobalDiscoveryEntryPersisted discoveryEntry = entityManager.find(GlobalDiscoveryEntryPersisted.class, participantId);
    EntityTransaction transaction = entityManager.getTransaction();
    try {
        transaction.begin();
        entityManager.remove(discoveryEntry);
        transaction.commit();
    } catch (Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        logger.error("unable to remove capability: " + participantId, e);
        return false;
    } finally {
    }
    return true;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException)

Example 70 with EntityTransaction

use of javax.persistence.EntityTransaction in project joynr by bmwcarit.

the class ChannelDatabase method addChannel.

@Override
public void addChannel(Channel channel) {
    logger.trace("add channel {}", channel);
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        BounceProxyInformationEntity bpInfoEntity = em.find(BounceProxyInformationEntity.class, channel.getBounceProxy().getId());
        if (bpInfoEntity == null) {
            tx.rollback();
            logger.error("No bounce proxy with ID {} registered for channel {}", channel.getBounceProxy().getId(), channel.getChannelId());
            throw new IllegalArgumentException("No bounce proxy with ID '" + channel.getBounceProxy().getId() + "' registered for channel '" + channel.getChannelId() + "'");
        }
        ChannelEntity entity = new ChannelEntity(channel.getChannelId(), bpInfoEntity, channel.getLocation().toString());
        em.persist(entity);
        tx.commit();
    } catch (RuntimeException ex) {
        logger.error("Error persisting channel with ID {}: error: {}", channel.getChannelId(), ex.getMessage());
        if (tx != null && tx.isActive())
            tx.rollback();
        throw ex;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ChannelEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity) BounceProxyInformationEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.BounceProxyInformationEntity)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)540 EntityManager (javax.persistence.EntityManager)447 Query (javax.persistence.Query)143 Person (org.datanucleus.samples.annotations.models.company.Person)101 List (java.util.List)90 TypedQuery (javax.persistence.TypedQuery)82 NoResultException (javax.persistence.NoResultException)77 ArrayList (java.util.ArrayList)69 NotFoundException (org.opencastproject.util.NotFoundException)61 PersistenceException (javax.persistence.PersistenceException)59 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)55 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)50 JPAQuery (org.datanucleus.api.jpa.JPAQuery)32 EntityManagerFactory (javax.persistence.EntityManagerFactory)29 Predicate (javax.persistence.criteria.Predicate)26 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)21 Connection (java.sql.Connection)20 RollbackException (javax.persistence.RollbackException)20 HashSet (java.util.HashSet)19 Employee (org.datanucleus.samples.annotations.models.company.Employee)19