Search in sources :

Example 61 with Transactional

use of javax.transaction.Transactional in project microservice_framework by CJSCommonPlatform.

the class DefaultEventRepository method storeEvent.

@Override
@Transactional(dontRollbackOn = OptimisticLockingRetryException.class)
public void storeEvent(final JsonEnvelope envelope) throws StoreEventRequestFailedException {
    try {
        final Event event = eventConverter.eventOf(envelope);
        logger.trace("Storing event {} into stream {} at position {}", event.getName(), event.getStreamId(), event.getSequenceId());
        eventJdbcRepository.insert(event);
    } catch (InvalidPositionException ex) {
        throw new StoreEventRequestFailedException(String.format("Could not store event for position %d of stream %s", envelope.metadata().position().orElse(null), envelope.metadata().streamId().orElse(null)), ex);
    }
}
Also used : StoreEventRequestFailedException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.StoreEventRequestFailedException) Event(uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event) InvalidPositionException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidPositionException) Transactional(javax.transaction.Transactional)

Example 62 with Transactional

use of javax.transaction.Transactional in project microservice_framework by CJSCommonPlatform.

the class EventStreamManager method appendNonConsecutively.

/**
 * Store a stream of events without enforcing consecutive version ids. Reduces risk of throwing
 * optimistic lock error. To be use instead of the append method, when it's acceptable to
 * store events with non consecutive version ids
 *
 * @param streamId - id of the stream to append to
 * @param events   the stream of events to store
 * @return the current stream version
 * @throws EventStreamException if an event could not be appended
 */
@Transactional(dontRollbackOn = OptimisticLockingRetryException.class)
public long appendNonConsecutively(final UUID streamId, final Stream<JsonEnvelope> events) throws EventStreamException {
    final List<JsonEnvelope> envelopeList = events.collect(toList());
    long currentVersion = eventRepository.getStreamSize(streamId);
    validateEvents(streamId, envelopeList);
    for (final JsonEnvelope event : envelopeList) {
        boolean appendedSuccessfully = false;
        long retryCount = 0L;
        while (!appendedSuccessfully) {
            try {
                eventAppender.append(event, streamId, ++currentVersion);
                appendedSuccessfully = true;
            } catch (OptimisticLockingRetryException e) {
                retryCount++;
                if (retryCount > maxRetry) {
                    logger.warn("Failed to append to stream {} due to concurrency issues, returning to handler.", streamId);
                    throw e;
                }
                currentVersion = eventRepository.getStreamSize(streamId);
                logger.trace("Retrying appending to stream {}, with version {}", streamId, currentVersion + 1);
            }
        }
    }
    return currentVersion;
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Transactional(javax.transaction.Transactional)

Example 63 with Transactional

use of javax.transaction.Transactional in project narayana by jbosstm.

the class TransactionalInterceptorBase method getTransactional.

/**
 * <p>
 * Looking for the {@link Transactional} annotation first on the method, second on the class.
 * <p>
 * Method handles CDI types to cover cases where extensions are used.
 * In case of EE container uses reflection.
 *
 * @param ic  invocation context of the interceptor
 * @return instance of {@link Transactional} annotation or null
 */
private Transactional getTransactional(InvocationContext ic) {
    if (interceptedBean != null) {
        // not-null for CDI
        // getting annotated type and method corresponding of the intercepted bean and method
        AnnotatedType<?> currentAnnotatedType = extension.getBeanToAnnotatedTypeMapping().get(interceptedBean);
        AnnotatedMethod<?> currentAnnotatedMethod = null;
        for (AnnotatedMethod<?> methodInSearch : currentAnnotatedType.getMethods()) {
            if (methodInSearch.getJavaMember().equals(ic.getMethod())) {
                currentAnnotatedMethod = methodInSearch;
                break;
            }
        }
        // check existence of the stereotype on method
        Transactional transactionalMethod = getTransactionalAnnotationRecursive(currentAnnotatedMethod.getAnnotations());
        if (transactionalMethod != null)
            return transactionalMethod;
        // stereotype recursive search, covering ones added by an extension too
        Transactional transactionalExtension = getTransactionalAnnotationRecursive(currentAnnotatedType.getAnnotations());
        if (transactionalExtension != null)
            return transactionalExtension;
        // stereotypes already merged to one chunk by BeanAttributes.getStereotypes()
        for (Class<? extends Annotation> stereotype : interceptedBean.getStereotypes()) {
            Transactional transactionalAnn = stereotype.getAnnotation(Transactional.class);
            if (transactionalAnn != null)
                return transactionalAnn;
        }
    } else {
        // null for EE components
        Transactional transactional = ic.getMethod().getAnnotation(Transactional.class);
        if (transactional != null) {
            return transactional;
        }
        Class<?> targetClass = ic.getTarget().getClass();
        transactional = targetClass.getAnnotation(Transactional.class);
        if (transactional != null) {
            return transactional;
        }
    }
    throw new RuntimeException(jtaLogger.i18NLogger.get_expected_transactional_annotation());
}
Also used : Transactional(javax.transaction.Transactional)

Example 64 with Transactional

use of javax.transaction.Transactional in project irida by phac-nml.

the class AnnouncementServiceImpl method markAnnouncementAsUnreadByUser.

/**
 *  {@inheritDoc}
 */
@Override
@Transactional
@PreAuthorize("hasAnyRole('ROLE_USER')")
public void markAnnouncementAsUnreadByUser(Announcement announcement, User user) throws EntityNotFoundException {
    try {
        final AnnouncementUserJoin join = announcementUserJoinRepository.getAnnouncementUserJoin(announcement, user);
        Long id = join.getId();
        announcementUserJoinRepository.delete(id);
    } catch (NullPointerException e) {
        throw new EntityNotFoundException("The user [" + user.getId() + "] has not yet read announcement [" + announcement.getId() + "]: cannot mark as unread.");
    }
}
Also used : AnnouncementUserJoin(ca.corefacility.bioinformatics.irida.model.announcements.AnnouncementUserJoin) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(javax.transaction.Transactional)

Example 65 with Transactional

use of javax.transaction.Transactional in project irida by phac-nml.

the class AnnouncementServiceImpl method create.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@PreAuthorize("hasRole('ROLE_ADMIN')")
public Announcement create(Announcement announcement) {
    final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    final User user = userRepository.loadUserByUsername(auth.getName());
    announcement.setUser(user);
    announcementRepository.save(announcement);
    return announcement;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Authentication(org.springframework.security.core.Authentication) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(javax.transaction.Transactional)

Aggregations

Transactional (javax.transaction.Transactional)299 Test (org.junit.Test)99 PostResult (org.collectiveone.common.dto.PostResult)29 ArrayList (java.util.ArrayList)24 UserDO (org.neusoft.neubbs.entity.UserDO)21 Timestamp (java.sql.Timestamp)18 TopicDO (org.neusoft.neubbs.entity.TopicDO)18 Initiative (org.collectiveone.modules.initiatives.Initiative)16 GetResult (org.collectiveone.common.dto.GetResult)15 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)13 TopicReplyDO (org.neusoft.neubbs.entity.TopicReplyDO)13 HashMap (java.util.HashMap)12 Date (java.util.Date)11 AppUser (org.collectiveone.modules.users.AppUser)11 UUID (java.util.UUID)10 RolesAllowed (javax.annotation.security.RolesAllowed)10 PeerReviewedAssignation (org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 IOException (java.io.IOException)9 TopicContentDO (org.neusoft.neubbs.entity.TopicContentDO)9