Skip to content

Commit

Permalink
EntityManagerFactory.withTransaction() as default implementation in t…
Browse files Browse the repository at this point in the history
…he interface.

Signed-off-by: Tomáš Kraus <tomas.kraus@oracle.com>
  • Loading branch information
Tomas-Kraus committed Jun 1, 2023
1 parent 2e4ab3a commit af4248c
Showing 1 changed file with 100 additions and 3 deletions.
103 changes: 100 additions & 3 deletions api/src/main/java/jakarta/persistence/EntityManagerFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -17,6 +17,8 @@
package jakarta.persistence;

import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -224,6 +226,41 @@ public interface EntityManagerFactory extends AutoCloseable {
*/
public <T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph);

/**
* Create a new application-managed {@code EntityManager}, start a resource-local
* transaction, and call the given function, passing both the {@code EntityManager}
* and the {@code EntityTransaction}.
* The given function is responsible for finishing the transaction by calling {@code commit}
* or {@code rollback} method.
*
* @param work a function to be called in the scope of the transaction
*/
default void withTransaction(BiConsumer<EntityManager, EntityTransaction> work) {
try (EntityManager em = this.createEntityManager()) {
EntityTransaction t = em.getTransaction();
t.begin();
work.accept(em, t);
}
}

/**
* Create a new application-managed {@code EntityManager}, start a resource-local
* transaction, and call the given function, passing both the {@code EntityManager}
* and the {@code EntityTransaction}.
* The given function is responsible for finishing the transaction by calling {@code commit}
* or {@code rollback} method.
*
* @param work a function to be called in the scope of the transaction
* @return the value returned by the given function
*/
default <R> R withTransaction(BiFunction<EntityManager, EntityTransaction, R> work) {
try (EntityManager em = this.createEntityManager()) {
EntityTransaction t = em.getTransaction();
t.begin();
return work.apply(em, t);
}
}

/**
* Create a new application-managed {@code EntityManager}, start a resource-local
* transaction, and call the given function, passing the {@code EntityManager}.
Expand All @@ -234,7 +271,20 @@ public interface EntityManagerFactory extends AutoCloseable {
*
* @param work a function to be called in the scope of the transaction
*/
public void withTransaction(Consumer<EntityManager> work);
default void withTransaction(TransactionVoidWork work) {
try (EntityManager em = this.createEntityManager()) {
EntityTransaction t = em.getTransaction();
try {
t.begin();
work.work(em);
t.commit();
} catch (Exception e) {
t.rollback();
throw new PersistenceException("Application-managed transaction failed", e);
}
}
}

/**
* Create a new application-managed {@code EntityManager}, start a resource-local
* transaction, and call the given function, passing the {@code EntityManager}.
Expand All @@ -246,5 +296,52 @@ public interface EntityManagerFactory extends AutoCloseable {
* @param work a function to be called in the scope of the transaction
* @return the value returned by the given function
*/
public <R> R withTransaction(Function<EntityManager,R> work);
default <R> R withTransaction(TransactionWork<R> work) {
try (EntityManager em = this.createEntityManager()) {
EntityTransaction t = em.getTransaction();
try {
t.begin();
R result = work.work(em);
t.commit();
return result;
} catch (Exception e) {
t.rollback();
throw new PersistenceException("Application-managed transaction failed", e);
}
}
}

/**
* A task that runs in a transaction, returns no result and may throw an exception.
* Implementors define a single method with {@link EntityManager} argument called {@code work}.
*/
@FunctionalInterface
interface TransactionVoidWork {
/**
* Executes the function. Throws an exception if unable to do so.
*
* @param em the application-managed {@link EntityManager} instance
* @throws Exception when unable to compute a result
*/
void work(EntityManager em) throws Exception;
}

/**
* A task that runs in a transaction, returns result and may throw an exception.
* Implementors define a single method with {@link EntityManager} argument called {@code work}.
*
* @param <R> the result type of method {@code work}
*/
@FunctionalInterface
interface TransactionWork<R> {
/**
* Executes the function. Throws an exception if unable to do so.
*
* @param em the application-managed {@link EntityManager} instance
* @return the computed result
* @throws Exception when unable to compute a result
*/
R work(EntityManager em) throws Exception;
}

}

0 comments on commit af4248c

Please sign in to comment.