Skip to content

Commit 189e1af

Browse files
committed
Use try-with-resources in documentation samples
Fixes #22269
1 parent d1862a2 commit 189e1af

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

src/docs/asciidoc/data-access.adoc

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6132,17 +6132,11 @@ that uses the `@PersistenceUnit` annotation:
61326132
}
61336133
61346134
public Collection loadProductsByCategory(String category) {
6135-
EntityManager em = this.emf.createEntityManager();
6136-
try {
6135+
try (EntityManager em = this.emf.createEntityManager()) {
61376136
Query query = em.createQuery("from Product as p where p.category = ?1");
61386137
query.setParameter(1, category);
61396138
return query.getResultList();
61406139
}
6141-
finally {
6142-
if (em != null) {
6143-
em.close();
6144-
}
6145-
}
61466140
}
61476141
}
61486142
----
@@ -6610,26 +6604,14 @@ constructs a Spring application context and calls these two methods:
66106604
}
66116605
66126606
public void saveSettings() throws IOException {
6613-
FileOutputStream os = null;
6614-
try {
6615-
os = new FileOutputStream(FILE_NAME);
6607+
try (FileOutputStream os = new FileOutputStream(FILE_NAME)) {
66166608
this.marshaller.marshal(settings, new StreamResult(os));
6617-
} finally {
6618-
if (os != null) {
6619-
os.close();
6620-
}
66216609
}
66226610
}
66236611
66246612
public void loadSettings() throws IOException {
6625-
FileInputStream is = null;
6626-
try {
6627-
is = new FileInputStream(FILE_NAME);
6613+
try (FileInputStream is = new FileInputStream(FILE_NAME)) {
66286614
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
6629-
} finally {
6630-
if (is != null) {
6631-
is.close();
6632-
}
66336615
}
66346616
}
66356617

0 commit comments

Comments
 (0)