Skip to content

Commit

Permalink
Use try-with-resources in documentation samples
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jan 17, 2019
1 parent d1862a2 commit 189e1af
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions src/docs/asciidoc/data-access.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6132,17 +6132,11 @@ that uses the `@PersistenceUnit` annotation:
}
public Collection loadProductsByCategory(String category) {
EntityManager em = this.emf.createEntityManager();
try {
try (EntityManager em = this.emf.createEntityManager()) {
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();
}
}
}
}
----
Expand Down Expand Up @@ -6610,26 +6604,14 @@ constructs a Spring application context and calls these two methods:
}
public void saveSettings() throws IOException {
FileOutputStream os = null;
try {
os = new FileOutputStream(FILE_NAME);
try (FileOutputStream os = new FileOutputStream(FILE_NAME)) {
this.marshaller.marshal(settings, new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
}
public void loadSettings() throws IOException {
FileInputStream is = null;
try {
is = new FileInputStream(FILE_NAME);
try (FileInputStream is = new FileInputStream(FILE_NAME)) {
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}
Expand Down

0 comments on commit 189e1af

Please sign in to comment.