Search in sources :

Example 1 with TransactionalAdvice

use of io.micronaut.transaction.annotation.TransactionalAdvice in project micronaut-sql by micronaut-projects.

the class InitController method init.

@Get
@TransactionalAdvice
void init() {
    try {
        Class.forName("net.bytebuddy.ByteBuddy");
        throw new IllegalStateException("ByteBuddy shouldn't be present on classpath");
    } catch (ClassNotFoundException e) {
    // Ignore
    }
    try {
        Class.forName("javassist.util.proxy.ProxyFactory");
        throw new IllegalStateException("Javassist shouldn't be present on classpath");
    } catch (ClassNotFoundException e) {
    // Ignore
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Running on " + System.getProperty("java.version") + " " + System.getProperty("java.vendor.version"));
        LOG.info("Populating data");
    }
    Owner fred = new Owner();
    fred.setName("Fred");
    fred.setAge(45);
    Owner barney = new Owner();
    barney.setName("Barney");
    barney.setAge(40);
    entityManager.persist(fred);
    entityManager.persist(barney);
    Pet dino = new Pet();
    dino.setName("Dino");
    dino.setOwner(fred);
    Pet bp = new Pet();
    bp.setName("Baby Puss");
    bp.setOwner(fred);
    bp.setType(Pet.PetType.CAT);
    Pet hoppy = new Pet();
    hoppy.setName("Hoppy");
    hoppy.setOwner(barney);
    entityManager.persist(dino);
    entityManager.persist(bp);
    entityManager.persist(hoppy);
}
Also used : Owner(example.domain.Owner) Pet(example.domain.Pet) TransactionalAdvice(io.micronaut.transaction.annotation.TransactionalAdvice) Get(io.micronaut.http.annotation.Get)

Example 2 with TransactionalAdvice

use of io.micronaut.transaction.annotation.TransactionalAdvice in project micronaut-data by micronaut-projects.

the class DefaultStoredDataOperation method getTransactionDefinition.

@NonNull
@Override
public final Optional<TransactionDefinition> getTransactionDefinition() {
    if (transactionDefinition == null) {
        AnnotationValue<TransactionalAdvice> annotation = method.getAnnotation(TransactionalAdvice.class);
        if (annotation != null) {
            DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
            attribute.setName(method.getDeclaringType().getSimpleName() + "." + method.getMethodName());
            attribute.setReadOnly(annotation.isTrue("readOnly"));
            annotation.intValue("timeout").ifPresent(value -> attribute.setTimeout(Duration.ofSeconds(value)));
            final Class[] noRollbackFors = annotation.classValues("noRollbackFor");
            // noinspection unchecked
            attribute.setNoRollbackFor(noRollbackFors);
            annotation.enumValue("propagation", TransactionDefinition.Propagation.class).ifPresent(attribute::setPropagationBehavior);
            annotation.enumValue("isolation", TransactionDefinition.Isolation.class).ifPresent(attribute::setIsolationLevel);
            this.transactionDefinition = attribute;
        } else {
            transactionDefinition = NO_TRANSACTION;
        }
    }
    if (transactionDefinition != NO_TRANSACTION) {
        return Optional.of(transactionDefinition);
    }
    return Optional.empty();
}
Also used : TransactionalAdvice(io.micronaut.transaction.annotation.TransactionalAdvice) DefaultTransactionAttribute(io.micronaut.transaction.interceptor.DefaultTransactionAttribute) NonNull(io.micronaut.core.annotation.NonNull)

Aggregations

TransactionalAdvice (io.micronaut.transaction.annotation.TransactionalAdvice)2 Owner (example.domain.Owner)1 Pet (example.domain.Pet)1 NonNull (io.micronaut.core.annotation.NonNull)1 Get (io.micronaut.http.annotation.Get)1 DefaultTransactionAttribute (io.micronaut.transaction.interceptor.DefaultTransactionAttribute)1