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);
}
}
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;
}
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());
}
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.");
}
}
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;
}
Aggregations