@Override public <S> Entry<S> loadSaga(Class<S> sagaType, String sagaIdentifier) { EntityManager entityManager = entityManagerProvider.getEntityManager(); List<SerializedSaga> serializedSagaList = entityManager .createNamedQuery(LOAD_SAGA_NAMED_QUERY, SerializedSaga.class) .setParameter("sagaId", sagaIdentifier) .setParameter("sagaType", getSagaTypeName(sagaType)) .setMaxResults(1) .getResultList(); if (serializedSagaList == null || serializedSagaList.isEmpty()) { return null; } SerializedSaga serializedSaga = serializedSagaList.get(0); S loadedSaga = serializer.deserialize(serializedSaga); if (injector != null) { injector.injectResources(loadedSaga); } Set<AssociationValue> associationValues = loadAssociationValues(entityManager, sagaType, sagaIdentifier); if (logger.isDebugEnabled()) { logger.debug( "Loaded saga id [{}] of type [{}]", sagaIdentifier, loadedSaga.getClass().getName()); } return new EntryImpl<>(associationValues, loadedSaga); }
@Override public Set<String> findSagas(Class<?> sagaType, AssociationValue associationValue) { EntityManager entityManager = entityManagerProvider.getEntityManager(); List<String> entries = entityManager .createNamedQuery(FIND_ASSOCIATION_IDS_NAMED_QUERY, String.class) .setParameter("associationKey", associationValue.getKey()) .setParameter("associationValue", associationValue.getValue()) .setParameter("sagaType", getSagaTypeName(sagaType)) .getResultList(); return new TreeSet<>(entries); }
@Override public void deleteSaga( Class<?> sagaType, String sagaIdentifier, Set<AssociationValue> associationValues) { EntityManager entityManager = entityManagerProvider.getEntityManager(); try { entityManager .createNamedQuery(DELETE_ASSOCIATIONS_NAMED_QUERY) .setParameter("sagaId", sagaIdentifier) .executeUpdate(); entityManager .createNamedQuery(DELETE_SAGA_NAMED_QUERY) .setParameter("id", sagaIdentifier) .executeUpdate(); } catch (EntityNotFoundException e) { logger.info( "Could not delete SagaEntry {}, it appears to have already been deleted.", sagaIdentifier); } if (useExplicitFlush) { entityManager.flush(); } }
@Override public void insertSaga( Class<?> sagaType, String sagaIdentifier, Object saga, TrackingToken token, Set<AssociationValue> associationValues) { EntityManager entityManager = entityManagerProvider.getEntityManager(); SagaEntry<?> entry = new SagaEntry<>(saga, sagaIdentifier, serializer); entityManager.persist(entry); for (AssociationValue associationValue : associationValues) { storeAssociationValue(entityManager, sagaType, sagaIdentifier, associationValue); } if (logger.isDebugEnabled()) { logger.debug( "Storing saga id {} as {}", sagaIdentifier, new String(entry.getSerializedSaga(), Charset.forName("UTF-8"))); } if (useExplicitFlush) { entityManager.flush(); } }
@Override public void updateSaga( Class<?> sagaType, String sagaIdentifier, Object saga, TrackingToken token, AssociationValues associationValues) { EntityManager entityManager = entityManagerProvider.getEntityManager(); SagaEntry<?> entry = new SagaEntry<>(saga, sagaIdentifier, serializer); if (logger.isDebugEnabled()) { logger.debug( "Updating saga id {} as {}", sagaIdentifier, new String(entry.getSerializedSaga(), Charset.forName("UTF-8"))); } int updateCount = entityManager .createNamedQuery(UPDATE_SAGA_NAMED_QUERY) .setParameter("serializedSaga", entry.getSerializedSaga()) .setParameter("revision", entry.getRevision()) .setParameter("sagaId", entry.getSagaId()) .setParameter("sagaType", entry.getSagaType()) .executeUpdate(); for (AssociationValue associationValue : associationValues.addedAssociations()) { storeAssociationValue(entityManager, sagaType, sagaIdentifier, associationValue); } for (AssociationValue associationValue : associationValues.removedAssociations()) { removeAssociationValue(entityManager, sagaType, sagaIdentifier, associationValue); } if (updateCount == 0) { logger.warn("Expected to be able to update a Saga instance, but no rows were found."); } if (useExplicitFlush) { entityManager.flush(); } }