From fdd6e50721dd7b9655792134719960f05b75cf97 Mon Sep 17 00:00:00 2001 From: Johannes Wengert Date: Tue, 4 Jan 2022 15:47:54 +0100 Subject: [PATCH] Correct not compiling example code in Data Access docs The EntityManager interface does not implement AutoCloseable until JPA 3.1. This commit therefore partially reverts 189e1afc6ee6bd40b so that the example code compiles with the supported JPA version. See gh-22269 Closes gh-27886 --- src/docs/asciidoc/data-access.adoc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/docs/asciidoc/data-access.adoc b/src/docs/asciidoc/data-access.adoc index 55d6be79646..a1cdec9d9ef 100644 --- a/src/docs/asciidoc/data-access.adoc +++ b/src/docs/asciidoc/data-access.adoc @@ -2444,10 +2444,10 @@ the `TransactionOperator` resembles the next example: } public Mono someServiceMethod() { - + // the code in this method runs in a transactional context - - Mono update = updateOperation1(); + + Mono update = updateOperation1(); return update.then(resultOfUpdateOperation2).as(transactionalOperator::transactional); } @@ -8221,11 +8221,17 @@ that uses the `@PersistenceUnit` annotation: } public Collection loadProductsByCategory(String category) { - try (EntityManager em = this.emf.createEntityManager()) { + EntityManager em = this.emf.createEntityManager(); + try { Query query = em.createQuery("from Product as p where p.category = ?1"); query.setParameter(1, category); return query.getResultList(); } + finally { + if (em != null) { + em.close(); + } + } } } ----