Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 5bb9bb0

Browse files
authored
Merge pull request #6 from bobocode-projects/shtramak
minor refactor
2 parents b03f979 + c8c3758 commit 5bb9bb0

File tree

1 file changed

+7
-8
lines changed
  • jpa-hibernate-tutorial-util/src/main/java/com/bobocode/util

1 file changed

+7
-8
lines changed

jpa-hibernate-tutorial-util/src/main/java/com/bobocode/util/JpaUtil.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ public static void close() {
2525
}
2626

2727
public static void performWithinPersistenceContext(Consumer<EntityManager> operation) {
28-
EntityManager em = emf.createEntityManager();
29-
em.getTransaction().begin();
30-
28+
EntityManager entityManager = emf.createEntityManager();
29+
entityManager.getTransaction().begin();
3130
try {
32-
operation.accept(em);
33-
em.getTransaction().commit();
31+
operation.accept(entityManager);
32+
entityManager.getTransaction().commit();
3433
} catch (Exception e) {
35-
em.getTransaction().rollback();
34+
entityManager.getTransaction().rollback();
3635
throw new JpaUtilException("Error performing JPA operation. Transaction is rolled back", e);
3736
} finally {
38-
em.close();
37+
entityManager.close();
3938
}
4039
}
4140

@@ -48,7 +47,7 @@ public static <T> T performReturningWithinPersistenceContext(Function<EntityMana
4847
return result;
4948
} catch (Exception e) {
5049
entityManager.getTransaction().rollback();
51-
throw e;
50+
throw new JpaUtilException("Error performing JPA operation. Transaction is rolled back", e);
5251
} finally {
5352
entityManager.close();
5453
}

0 commit comments

Comments
 (0)