File tree Expand file tree Collapse file tree 1 file changed +3
-21
lines changed Expand file tree Collapse file tree 1 file changed +3
-21
lines changed Original file line number Diff line number Diff line change @@ -6132,17 +6132,11 @@ that uses the `@PersistenceUnit` annotation:
6132
6132
}
6133
6133
6134
6134
public Collection loadProductsByCategory(String category) {
6135
- EntityManager em = this.emf.createEntityManager();
6136
- try {
6135
+ try (EntityManager em = this.emf.createEntityManager()) {
6137
6136
Query query = em.createQuery("from Product as p where p.category = ?1");
6138
6137
query.setParameter(1, category);
6139
6138
return query.getResultList();
6140
6139
}
6141
- finally {
6142
- if (em != null) {
6143
- em.close();
6144
- }
6145
- }
6146
6140
}
6147
6141
}
6148
6142
----
@@ -6610,26 +6604,14 @@ constructs a Spring application context and calls these two methods:
6610
6604
}
6611
6605
6612
6606
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)) {
6616
6608
this.marshaller.marshal(settings, new StreamResult(os));
6617
- } finally {
6618
- if (os != null) {
6619
- os.close();
6620
- }
6621
6609
}
6622
6610
}
6623
6611
6624
6612
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)) {
6628
6614
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
6629
- } finally {
6630
- if (is != null) {
6631
- is.close();
6632
- }
6633
6615
}
6634
6616
}
6635
6617
You can’t perform that action at this time.
0 commit comments