Skip to content

Commit

Permalink
Merge pull request #134 from dartartem/wip-db-id-gen
Browse files Browse the repository at this point in the history
(Probably should be tested in projects that depends on it before merge) Replaced TransactionTemplate by EventuateTransactionTemplate in Trans…
  • Loading branch information
cer authored Feb 26, 2021
2 parents b9bd98a + 8161447 commit 580a7f6
Show file tree
Hide file tree
Showing 36 changed files with 463 additions and 131 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package io.eventuate.tram.consumer.jdbc;

import io.eventuate.common.jdbc.EventuateTransactionTemplate;
import io.eventuate.tram.consumer.common.DuplicateMessageDetector;
import io.eventuate.tram.consumer.common.SubscriberIdAndMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.support.TransactionTemplate;

public class TransactionalNoopDuplicateMessageDetector implements DuplicateMessageDetector {

private Logger logger = LoggerFactory.getLogger(getClass());

private TransactionTemplate transactionTemplate;
private EventuateTransactionTemplate transactionTemplate;

public TransactionalNoopDuplicateMessageDetector(TransactionTemplate transactionTemplate) {
public TransactionalNoopDuplicateMessageDetector(EventuateTransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}

Expand All @@ -23,16 +23,14 @@ public boolean isDuplicate(String consumerId, String messageId) {

@Override
public void doWithMessage(SubscriberIdAndMessage subscriberIdAndMessage, Runnable callback) {
transactionTemplate.execute(ts -> {
transactionTemplate.executeInTransaction(() -> {
try {
callback.run();
return null;
} catch (Throwable e) {
logger.error("Got exception - marking for rollback only", e);
ts.setRollbackOnly();
throw e;
}
});

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.eventuate.tram.micronaut.consumer.jdbc;

import io.eventuate.common.jdbc.EventuateTransactionTemplate;
import io.eventuate.tram.consumer.common.DuplicateMessageDetector;
import io.eventuate.tram.consumer.jdbc.TransactionalNoopDuplicateMessageDetector;
import io.micronaut.context.annotation.Factory;
Expand All @@ -13,7 +14,7 @@ public class TransactionalNoopDuplicateMessageDetectorFactory {

@Singleton
@Requires(property = "transactional.noop.duplicate.message.detector.factory.enabled")
public DuplicateMessageDetector duplicateMessageDetector(TransactionTemplate transactionTemplate) {
return new TransactionalNoopDuplicateMessageDetector(transactionTemplate);
public DuplicateMessageDetector duplicateMessageDetector(EventuateTransactionTemplate eventuateTransactionTemplate) {
return new TransactionalNoopDuplicateMessageDetector(eventuateTransactionTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ apply plugin: PublicModulePlugin

dependencies {
compile project(":eventuate-tram-consumer-common")
compile project(":eventuate-tram-optimistic-locking-common-test")
compile "io.eventuate.common:eventuate-common-micronaut-spring-jdbc:$eventuateCommonVersion"

compile "io.micronaut.configuration:micronaut-hibernate-jpa"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,17 @@
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import io.eventuate.tram.consumer.common.MessageHandlerDecorator;
import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractEventuateOptimisticLockingTest;

import javax.inject.Inject;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

abstract public class AbstractEventuateMicronautOptimisticLockingTest {

private static final int UPDATE_THREADS = 3;
abstract public class AbstractEventuateMicronautOptimisticLockingTest extends AbstractEventuateOptimisticLockingTest {

@Inject
private OptimisticLockingDecorator optimisticLockingDecorator;

private AtomicInteger attempts = new AtomicInteger(0);

private CountDownLatch countDownLatch = new CountDownLatch(UPDATE_THREADS);

protected abstract AbstractTestEntityService testEntityService();

@Test
public void shouldRetryOnLockException() throws InterruptedException {

long testEntityId = testEntityService().createTestEntityInTransaction();

for (int i = 0; i < UPDATE_THREADS; i++) updateEntity(testEntityId);

countDownLatch.await();

Assertions.assertTrue(attempts.get() > UPDATE_THREADS);
Assertions.assertEquals(UPDATE_THREADS, testEntityService().getDataInTransaction(testEntityId));
}

private void updateEntity(Long testEntityId) {
new Thread(() -> {

optimisticLockingDecorator.accept(null, subscriberIdAndMessage -> {
attempts.incrementAndGet();

testEntityService().incDataInTransaction(testEntityId);
});

countDownLatch.countDown();

}).start();
@Override
public MessageHandlerDecorator getOptimisticLockingDecorator() {
return optimisticLockingDecorator;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking;

import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractTestEntityService;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;

@MicronautTest(transactional = false)
public class EventuateMicronautOptimisticLockingWithAnnotationTransactionTest extends AbstractEventuateMicronautOptimisticLockingTest {


@Inject
private TestEntityServiceTransactionAnnotation testEntityService;


@Override
protected AbstractTestEntityService testEntityService() {
return testEntityService;
}

@Override
@Test
public void shouldRetryOnLockException() throws InterruptedException {
super.shouldRetryOnLockException();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking;

import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractTestEntityService;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;

Expand All @@ -14,4 +16,10 @@ public class EventuateMicronautOptimisticLockingWithTransactionTemplateTransacti
protected AbstractTestEntityService testEntityService() {
return testEntityService;
}

@Override
@Test
public void shouldRetryOnLockException() throws InterruptedException {
super.shouldRetryOnLockException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking;

import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntity;
import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntityRepository;

import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Singleton
public class TestEntityRepositoryImpl implements TestEntityRepository {
@PersistenceContext
private EntityManager entityManager;

@Override
public TestEntity find(Long entityId) {
return entityManager.find(TestEntity.class, entityId);
}

@Override
public void persist(TestEntity testEntity) {
entityManager.persist(testEntity);
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking;

import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractTestEntityService;
import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntityRepository;
import io.micronaut.spring.tx.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Singleton
public class TestEntityServiceTransactionAnnotation extends AbstractTestEntityService {

@Inject
private TestEntityRepository testEntityRepository;

@Transactional
@Override
public Long createTestEntityInTransaction() {
return createTestEntity();
}
@Transactional
@Override
public Long createTestEntityInTransaction() {
return createTestEntity();
}

@Transactional
@Override
public void incDataInTransaction(Long entityId) {
incData(entityId);
}
@Transactional
@Override
public void incDataInTransaction(Long entityId) {
incData(entityId);
}

@Transactional
@Override
public long getDataInTransaction(Long entityId) {
@Transactional
@Override
public long getDataInTransaction(Long entityId) {
return getData(entityId);
}

@Override
public TestEntityRepository getTestEntityRepository() {
return testEntityRepository;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking;

import io.eventuate.common.jdbc.EventuateTransactionTemplate;
import io.eventuate.tram.jdbc.optimistic.locking.common.test.AbstractTestEntityServiceTransactionTemplate;
import io.eventuate.tram.jdbc.optimistic.locking.common.test.TestEntityRepository;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class TestEntityServiceTransactionTemplate extends AbstractTestEntityService {
public class TestEntityServiceTransactionTemplate extends AbstractTestEntityServiceTransactionTemplate {

@Inject
private EventuateTransactionTemplate eventuateTransactionalTemplate;
private TestEntityRepository testEntityRepository;

@Override
public Long createTestEntityInTransaction() {
return eventuateTransactionalTemplate.executeInTransaction(this::createTestEntity);
}
@Inject
private EventuateTransactionTemplate eventuateTransactionalTemplate;

@Override
public void incDataInTransaction(Long entityId) {
eventuateTransactionalTemplate.executeInTransaction(() -> {
incData(entityId);
return null;
});
public EventuateTransactionTemplate getEventuateTransactionalTemplate() {
return eventuateTransactionalTemplate;
}

@Override
public long getDataInTransaction(Long entityId) {
return eventuateTransactionalTemplate.executeInTransaction(() -> {
return getData(entityId);
});
public TestEntityRepository getTestEntityRepository() {
return testEntityRepository;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ datasources:
jpa:
default:
packages-to-scan:
- 'io.eventuate.tram.micronaut.spring.jdbc.optimistic.locking'
- 'io.eventuate.tram.jdbc.optimistic.locking.common.test'
properties:
hibernate:
hbm2ddl:
Expand Down
8 changes: 8 additions & 0 deletions eventuate-tram-optimistic-locking-common-test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependencies {
compile project(":eventuate-tram-consumer-common")
compile project(":eventuate-tram-consumer-common")
compile "io.eventuate.common:eventuate-common-jdbc:$eventuateCommonVersion"
compile 'javax.persistence:javax.persistence-api:2.2'

compile "junit:junit:4.12"
}
Loading

0 comments on commit 580a7f6

Please sign in to comment.