Changeset 15:cba8574112a9

Updated transactional handling
author unexist
date Fri, 07 May 2021 15:10:19 +0200
parents e3ea5d841c0a
children 60fcfb959b7b
files todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/DueDate.java todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/TodoBase.java todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/TodoService.java todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxService.java todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/persistence/TodoRepositoryImpl.java
diffstat 5 files changed, 20 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/DueDate.java	Thu May 06 17:21:21 2021 +0200
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/DueDate.java	Fri May 07 15:10:19 2021 +0200
@@ -14,8 +14,11 @@
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import dev.unexist.showcase.todo.infrastructure.serializer.DateSerializer;
 
+import javax.persistence.Embeddable;
+import javax.persistence.Embedded;
 import java.time.LocalDate;
 
+@Embeddable
 public class DueDate {
     @JsonSerialize(using = DateSerializer.class)
     private LocalDate start;
--- a/todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/TodoBase.java	Thu May 06 17:21:21 2021 +0200
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/TodoBase.java	Fri May 07 15:10:19 2021 +0200
@@ -11,19 +11,26 @@
 
 package dev.unexist.showcase.todo.domain.todo;
 
+import javax.persistence.Column;
+import javax.persistence.Embedded;
+import javax.persistence.MappedSuperclass;
 import javax.validation.constraints.NotBlank;
 
+@MappedSuperclass
 public class TodoBase {
 
     @NotBlank
+    @Column
     private String title;
 
     @NotBlank
+    @Column
     private String description;
 
+    @Column
     private Boolean done;
 
-    @NotBlank
+    @Embedded
     private DueDate dueDate;
 
     /**
--- a/todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/TodoService.java	Thu May 06 17:21:21 2021 +0200
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/domain/todo/TodoService.java	Fri May 07 15:10:19 2021 +0200
@@ -53,7 +53,7 @@
         OutboxEvent event = new OutboxEvent();
 
         event.setAggregateId(todo.getId());
-        event.setEventType("topic");
+        event.setEventType("todo_created");
 
         try {
             ObjectMapper mapper = new ObjectMapper();
--- a/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxService.java	Thu May 06 17:21:21 2021 +0200
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/outbox/OutboxService.java	Fri May 07 15:10:19 2021 +0200
@@ -13,18 +13,21 @@
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.event.Observes;
-import javax.enterprise.event.ObservesAsync;
 import javax.enterprise.event.TransactionPhase;
 import javax.inject.Inject;
+import javax.transaction.Transactional;
 import java.util.UUID;
 
+import static javax.transaction.Transactional.TxType.REQUIRES_NEW;
+
 @ApplicationScoped
 public class OutboxService {
 
     @Inject
-    private OutboxRepository outboxRepository;
+    OutboxRepository outboxRepository;
 
-    public void handleOutboxEvent(@Observes(during=TransactionPhase.AFTER_SUCCESS) OutboxEvent event) {
+    @Transactional(REQUIRES_NEW)
+    public void handleOutboxEvent(@Observes(during = TransactionPhase.AFTER_SUCCESS) OutboxEvent event) {
         UUID uuid = UUID.randomUUID();
 
         OutboxEntity entity = new OutboxEntity(uuid,
--- a/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/persistence/TodoRepositoryImpl.java	Thu May 06 17:21:21 2021 +0200
+++ b/todo-service/src/main/java/dev/unexist/showcase/todo/infrastructure/persistence/TodoRepositoryImpl.java	Fri May 07 15:10:19 2021 +0200
@@ -20,6 +20,8 @@
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.transaction.Transactional;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;