This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch tomee-10.x
in repository https://gitbox.apache.org/repos/asf/tomee.git


The following commit(s) were added to refs/heads/tomee-10.x by this push:
     new 7ca5b46c4f Make use of automatic resource mgmt via try-with-resources 
pattern (#2539)
7ca5b46c4f is described below

commit 7ca5b46c4f9b4d88abb6a8f84ba0b7f1cd1adb6f
Author: Martin Wiesner <[email protected]>
AuthorDate: Mon Mar 9 21:45:06 2026 +0100

    Make use of automatic resource mgmt via try-with-resources pattern (#2539)
    
    (cherry picked from commit cbbcd576423d146f28b9c0f2a3d12449a4173bb4)
---
 .../apache/openejb/arquillian/common/Setup.java    | 12 +----
 .../java/org/apache/ziplock/ResourceFinder.java    | 26 ++--------
 .../main/java/org/superbiz/injection/Movies.java   | 21 ++------
 .../apache/openejb/test/beans/CalculatorBean.java  |  5 +-
 .../apache/openejb/test/beans/DatabaseBean.java    | 15 ++----
 .../apache/openejb/test/beans/EmployeeBean.java    | 53 ++++----------------
 .../openejb/test/beans/ShoppingCartBean.java       |  5 +-
 .../test/entity/bmp/BasicBmp2DataSourcesBean.java  | 40 ++++------------
 .../openejb/test/entity/bmp/BasicBmpBean.java      | 56 +++++-----------------
 .../test/singleton/BeanTxSingletonBean.java        | 20 ++------
 .../test/singleton/ContainerTxSingletonBean.java   | 20 ++------
 .../openejb/test/stateful/BeanTxStatefulBean.java  | 20 ++------
 .../test/stateless/BeanTxStatelessBean.java        | 20 ++------
 .../test/stateless/ContainerTxStatelessBean.java   | 20 ++------
 .../apache/openejb/maven/jarstxt/JarsTxtMojo.java  | 12 +----
 .../maven/plugin/embedded/OpenEJBEmbeddedMojo.java |  7 +--
 .../openejb/maven/plugin/AbstractTomEEMojo.java    | 12 +----
 .../openejb/client/JaxWsProviderWrapper.java       | 26 ++--------
 .../openejb/client/MulticastPulseClient.java       | 10 +---
 .../org/apache/openejb/client/ResourceFinder.java  | 27 ++---------
 .../apache/openejb/client/SSLContextBuilder.java   | 10 +---
 .../server/cli/command/ScriptFileCommand.java      | 12 +----
 .../openejb/server/cxf/rs/CxfRsHttpListener.java   |  8 +---
 .../apache/openejb/server/admin/AdminDaemon.java   | 12 +----
 .../java/org/apache/openejb/server/admin/Stop.java | 25 +---------
 .../tomee/catalina/TomEEClassLoaderEnricher.java   | 21 ++------
 .../java/org/apache/tomee/installer/Installer.java | 12 +----
 .../org/apache/tomee/loader/OpenEJBListener.java   | 12 +----
 .../org/apache/tomee/loader/TomcatEmbedder.java    | 17 ++-----
 29 files changed, 90 insertions(+), 466 deletions(-)

diff --git 
a/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Setup.java
 
b/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Setup.java
index dae0e8c36b..8ca47a9231 100644
--- 
a/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Setup.java
+++ 
b/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Setup.java
@@ -184,21 +184,11 @@ public class Setup {
     }
 
     public static boolean isRunning(final String host, final int port) {
-        Socket socket = null;
-        try {
-            socket = new Socket(host, port);
+        try (Socket socket = new Socket(host, port)) {
             socket.getOutputStream().close();
             return true;
         } catch (final Exception e) {
             return false;
-        } finally {
-            if (socket != null) {
-                try {
-                    socket.close();
-                } catch (final IOException ignored) {
-                    // no-op
-                }
-            }
         }
     }
 
diff --git 
a/arquillian/ziplock/src/main/java/org/apache/ziplock/ResourceFinder.java 
b/arquillian/ziplock/src/main/java/org/apache/ziplock/ResourceFinder.java
index 4c8ca808b8..e6962591b2 100644
--- a/arquillian/ziplock/src/main/java/org/apache/ziplock/ResourceFinder.java
+++ b/arquillian/ziplock/src/main/java/org/apache/ziplock/ResourceFinder.java
@@ -887,32 +887,21 @@ public class ResourceFinder {
     }
 
     private Properties loadProperties(final URL resource) throws IOException {
-        final InputStream in = resource.openStream();
 
-        BufferedInputStream reader = null;
-        try {
-            reader = new BufferedInputStream(in);
+        try (InputStream in = resource.openStream();
+             BufferedInputStream reader = new BufferedInputStream(in)) {
             final Properties properties = new Properties();
             properties.load(reader);
 
             return properties;
-        } finally {
-            try {
-                in.close();
-                reader.close();
-            } catch (final Exception e) {
-                // no-op
-            }
         }
     }
 
     private String readContents(final URL resource) throws IOException {
-        final InputStream in = resource.openStream();
-        BufferedInputStream reader = null;
         final StringBuffer sb = new StringBuffer();
 
-        try {
-            reader = new BufferedInputStream(in);
+        try (InputStream in = resource.openStream();
+             BufferedInputStream reader = new BufferedInputStream(in)) {
 
             int b = reader.read();
             while (b != -1) {
@@ -921,13 +910,6 @@ public class ResourceFinder {
             }
 
             return sb.toString().trim();
-        } finally {
-            try {
-                in.close();
-                reader.close();
-            } catch (final Exception e) {
-                // no-op
-            }
         }
     }
 
diff --git 
a/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java
 
b/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java
index ae5e1efd0b..0de4c2ecd7 100644
--- 
a/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java
+++ 
b/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java
@@ -47,45 +47,35 @@ public class Movies {
 
     @PostConstruct
     private void construct() throws Exception {
-        Connection connection = movieDatabase.getConnection();
-        try {
+        try (Connection connection = movieDatabase.getConnection()) {
             PreparedStatement stmt = connection.prepareStatement("CREATE TABLE 
movie ( director VARCHAR(255), title VARCHAR(255), year integer)");
             stmt.execute();
-        } finally {
-            connection.close();
         }
     }
 
     public void addMovie(Movie movie) throws Exception {
-        Connection conn = movieDatabase.getConnection();
-        try {
+        try (Connection conn = movieDatabase.getConnection()) {
             PreparedStatement sql = conn.prepareStatement("INSERT into movie 
(director, title, year) values (?, ?, ?)");
             sql.setString(1, movie.getDirector());
             sql.setString(2, movie.getTitle());
             sql.setInt(3, movie.getYear());
             sql.execute();
-        } finally {
-            conn.close();
         }
     }
 
     public void deleteMovie(Movie movie) throws Exception {
-        Connection conn = movieDatabase.getConnection();
-        try {
+        try (Connection conn = movieDatabase.getConnection()) {
             PreparedStatement sql = conn.prepareStatement("DELETE from movie 
where director = ? AND title = ? AND year = ?");
             sql.setString(1, movie.getDirector());
             sql.setString(2, movie.getTitle());
             sql.setInt(3, movie.getYear());
             sql.execute();
-        } finally {
-            conn.close();
         }
     }
 
     public List<Movie> getMovies() throws Exception {
         ArrayList<Movie> movies = new ArrayList<>();
-        Connection conn = movieDatabase.getConnection();
-        try {
+        try (Connection conn = movieDatabase.getConnection()) {
             PreparedStatement sql = conn.prepareStatement("SELECT director, 
title, year from movie");
             ResultSet set = sql.executeQuery();
             while (set.next()) {
@@ -95,9 +85,6 @@ public class Movies {
                 movie.setYear(set.getInt("year"));
                 movies.add(movie);
             }
-
-        } finally {
-            conn.close();
         }
         return movies;
     }
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/CalculatorBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/CalculatorBean.java
index 4294221c37..d4a6c53013 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/CalculatorBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/CalculatorBean.java
@@ -46,13 +46,10 @@ public class CalculatorBean implements 
jakarta.ejb.SessionBean {
 
             con = ds.getConnection();
 
-            final Statement stmt = con.createStatement();
-            try {
+            try (Statement stmt = con.createStatement()) {
                 final ResultSet rs = stmt.executeQuery("select * from 
Employees");
                 while (rs.next())
                     System.out.println(rs.getString(2));
-            } finally {
-                stmt.close();
             }
 
         } catch (final javax.naming.NamingException re) {
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/DatabaseBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/DatabaseBean.java
index f304f16609..d3c7c926fc 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/DatabaseBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/DatabaseBean.java
@@ -51,17 +51,11 @@ public class DatabaseBean implements 
jakarta.ejb.SessionBean {
         try {
 
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement(statement);
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement(statement)) 
{
                     stmt.executeQuery();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             throw new EJBException("Cannot execute the statement: " + 
statement + e.getMessage());
@@ -76,11 +70,8 @@ public class DatabaseBean implements jakarta.ejb.SessionBean 
{
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
             con = ds.getConnection();
 
-            final Statement stmt = con.createStatement();
-            try {
+            try (Statement stmt = con.createStatement()) {
                 retval = stmt.execute(statement);
-            } finally {
-                stmt.close();
             }
 
         } catch (final javax.naming.NamingException e) {
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/EmployeeBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/EmployeeBean.java
index 854b5d0e59..110438fd14 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/EmployeeBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/EmployeeBean.java
@@ -43,19 +43,12 @@ public class EmployeeBean implements jakarta.ejb.EntityBean 
{
 
             final javax.sql.DataSource ds = (javax.sql.DataSource) 
jndiContext.lookup("java:comp/env/jdbc/orders");
 
-            final Connection con = ds.getConnection();
-
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from Employees where EmployeeID = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from Employees where EmployeeID = ?")) {
                     stmt.setInt(1, primaryKey);
                     final ResultSet rs = stmt.executeQuery();
                     found = rs.next();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             e.printStackTrace();
@@ -80,15 +73,12 @@ public class EmployeeBean implements jakarta.ejb.EntityBean 
{
 
             java.util.Vector keys;
             try {
-                final Statement stmt = con.createStatement();
-                try {
+                try (Statement stmt = con.createStatement()) {
                     final ResultSet rs = stmt.executeQuery("select EmployeeID 
from Employees");
                     keys = new java.util.Vector();
                     while (rs.next()) {
                         keys.addElement(rs.getInt("EmployeeID"));
                     }
-                } finally {
-                    stmt.close();
                 }
             } finally {
                 con.close();
@@ -110,9 +100,7 @@ public class EmployeeBean implements jakarta.ejb.EntityBean 
{
 
             final javax.sql.DataSource ds = (javax.sql.DataSource) 
jndiContext.lookup("java:comp/env/jdbc/orders");
 
-            final Connection con = ds.getConnection();
-
-            try {
+            try (Connection con = ds.getConnection()) {
                 PreparedStatement stmt = con.prepareStatement("insert into 
Employees (FirstName, LastName) values (?,?)");
                 try {
                     stmt.setString(1, firstName);
@@ -128,8 +116,6 @@ public class EmployeeBean implements jakarta.ejb.EntityBean 
{
                 } finally {
                     stmt.close();
                 }
-            } finally {
-                con.close();
             }
 
             return id;
@@ -162,10 +148,8 @@ public class EmployeeBean implements 
jakarta.ejb.EntityBean {
 
             final javax.sql.DataSource ds = (javax.sql.DataSource) 
jndiContext.lookup("java:comp/env/jdbc/orders");
 
-            final Connection con = ds.getConnection();
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from Employees where EmployeeID = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from Employees where EmployeeID = ?")) {
                     final Integer primaryKey = (Integer) 
ejbContext.getPrimaryKey();
                     stmt.setInt(1, primaryKey);
                     final ResultSet rs = stmt.executeQuery();
@@ -173,11 +157,7 @@ public class EmployeeBean implements 
jakarta.ejb.EntityBean {
                         lastName = rs.getString("LastName");
                         firstName = rs.getString("FirstName");
                     }
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
 
         } catch (final Exception e) {
@@ -191,20 +171,14 @@ public class EmployeeBean implements 
jakarta.ejb.EntityBean {
             final InitialContext jndiContext = new InitialContext();
 
             final javax.sql.DataSource ds = (javax.sql.DataSource) 
jndiContext.lookup("java:comp/env/jdbc/orders");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("update 
Employees set FirstName = ?, LastName = ? where EmployeeID = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("update 
Employees set FirstName = ?, LastName = ? where EmployeeID = ?")) {
                     stmt.setString(1, firstName);
                     stmt.setString(2, lastName);
                     stmt.setInt(3, id);
                     stmt.execute();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             e.printStackTrace();
@@ -226,19 +200,12 @@ public class EmployeeBean implements 
jakarta.ejb.EntityBean {
             final javax.sql.DataSource ds =
                 (javax.sql.DataSource) 
jndiContext.lookup("java:comp/env/jdbc/orders");
 
-            final Connection con = ds.getConnection();
-
-            try {
-                final PreparedStatement stmt = con.prepareStatement("delete 
from Employees where EmployeeID = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("delete 
from Employees where EmployeeID = ?")) {
                     final Integer primaryKey = (Integer) 
ejbContext.getPrimaryKey();
                     stmt.setInt(1, primaryKey);
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
 
         } catch (final Exception e) {
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/ShoppingCartBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/ShoppingCartBean.java
index 8024c9a789..4c79847415 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/ShoppingCartBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/ShoppingCartBean.java
@@ -79,8 +79,7 @@ public class ShoppingCartBean implements SessionBean, 
jakarta.ejb.SessionSynchro
 
             con = ds.getConnection();
 
-            final Statement stmt = con.createStatement();
-            try {
+            try (Statement stmt = con.createStatement()) {
                 final ResultSet rs = stmt.executeQuery("select * from 
Employees");
                 while (rs.next())
                     System.out.println(rs.getString(2));
@@ -90,8 +89,6 @@ public class ShoppingCartBean implements SessionBean, 
jakarta.ejb.SessionSynchro
                 calc.sub(1, 2);
 
                 final int i = 1;
-            } finally {
-                stmt.close();
             }
 
         } catch (final java.rmi.RemoteException re) {
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmp2DataSourcesBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmp2DataSourcesBean.java
index 2cdc6b35e9..4faa27ca91 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmp2DataSourcesBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmp2DataSourcesBean.java
@@ -84,18 +84,12 @@ public class BasicBmp2DataSourcesBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?")) {
                     stmt.setInt(1, primaryKey);
                     found = stmt.executeQuery().next();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             throw new FinderException("FindByPrimaryKey failed");
@@ -247,11 +241,9 @@ public class BasicBmp2DataSourcesBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?")) {
                     final Integer primaryKey = (Integer) 
ejbContext.getPrimaryKey();
                     stmt.setInt(1, primaryKey);
                     final ResultSet rs = stmt.executeQuery();
@@ -259,11 +251,7 @@ public class BasicBmp2DataSourcesBean implements 
jakarta.ejb.EntityBean {
                         lastName = rs.getString("last_name");
                         firstName = rs.getString("first_name");
                     }
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
 
         } catch (final Exception e) {
@@ -297,20 +285,14 @@ public class BasicBmp2DataSourcesBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("update 
entity set first_name = ?, last_name = ? where EmployeeID = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("update 
entity set first_name = ?, last_name = ? where EmployeeID = ?")) {
                     stmt.setString(1, firstName);
                     stmt.setString(2, lastName);
                     stmt.setInt(3, primaryKey);
                     stmt.execute();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             e.printStackTrace();
@@ -329,18 +311,12 @@ public class BasicBmp2DataSourcesBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
-            try {
-                final PreparedStatement stmt = con.prepareStatement("delete 
from entity where id = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("delete 
from entity where id = ?")) {
                     final Integer primaryKey = (Integer) 
ejbContext.getPrimaryKey();
                     stmt.setInt(1, primaryKey);
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
 
         } catch (final Exception e) {
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java
index 5e7b178aa8..e9a64c3cf7 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/entity/bmp/BasicBmpBean.java
@@ -104,18 +104,12 @@ public class BasicBmpBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?")) {
                     stmt.setInt(1, primaryKey);
                     found = stmt.executeQuery().next();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             throw new FinderException("FindByPrimaryKey failed");
@@ -139,19 +133,13 @@ public class BasicBmpBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("SELECT id 
FROM entity WHERE last_name = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("SELECT id 
FROM entity WHERE last_name = ?")) {
                     stmt.setString(1, lastName);
                     final ResultSet set = stmt.executeQuery();
                     while (set.next()) keys.add(set.getInt("id"));
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             throw new FinderException("FindByPrimaryKey failed");
@@ -180,9 +168,7 @@ public class BasicBmpBean implements jakarta.ejb.EntityBean 
{
 
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
 
-            final Connection con = ds.getConnection();
-
-            try {
+            try (Connection con = ds.getConnection()) {
                 // Support for Oracle because Oracle doesn't do auto increment
 //          PreparedStatement stmt = con.prepareStatement("insert into entity 
(id, first_name, last_name) values (?,?,?)");
 //          stmt.setInt(1, keys++);
@@ -207,8 +193,6 @@ public class BasicBmpBean implements jakarta.ejb.EntityBean 
{
                 } finally {
                     stmt.close();
                 }
-            } finally {
-                con.close();
             }
 
             return primaryKey;
@@ -310,11 +294,9 @@ public class BasicBmpBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from entity where id = ?")) {
                     stmt.setInt(1, primaryKey);
                     final ResultSet rs = stmt.executeQuery();
                     if (!rs.next()) {
@@ -325,11 +307,7 @@ public class BasicBmpBean implements 
jakarta.ejb.EntityBean {
                     if (rs.next()) {
                         throw new EJBException("Found more than one entity 
with id " + primaryKey);
                     }
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final NamingException | SQLException e) {
             throw new EJBException(e);
@@ -362,20 +340,14 @@ public class BasicBmpBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("update 
entity set first_name = ?, last_name = ? where id = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("update 
entity set first_name = ?, last_name = ? where id = ?")) {
                     stmt.setString(1, firstName);
                     stmt.setString(2, lastName);
                     stmt.setInt(3, primaryKey);
                     stmt.execute();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             e.printStackTrace();
@@ -394,18 +366,12 @@ public class BasicBmpBean implements 
jakarta.ejb.EntityBean {
         try {
             final InitialContext jndiContext = new InitialContext();
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("delete 
from entity where id = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("delete 
from entity where id = ?")) {
                     stmt.setInt(1, primaryKey);
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
 
         } catch (final Exception e) {
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/BeanTxSingletonBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/BeanTxSingletonBean.java
index b5200bbf0f..3ae1bfd0bd 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/BeanTxSingletonBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/BeanTxSingletonBean.java
@@ -80,23 +80,19 @@ public class BeanTxSingletonBean implements 
jakarta.ejb.SessionBean {
 
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
+            try (Connection con = ds.getConnection()) {
                 final UserTransaction ut = ejbContext.getUserTransaction();
                 /*[1] Begin the transaction */
                 ut.begin();
 
                 /*[2] Update the table */
-                final PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
-                try {
+                try (PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)")) {
                     stmt.setString(1, acct.getSsn());
                     stmt.setString(2, acct.getFirstName());
                     stmt.setString(3, acct.getLastName());
                     stmt.setInt(4, acct.getBalance());
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
 
                 /*[3] Commit or Rollback the transaction */
@@ -104,8 +100,6 @@ public class BeanTxSingletonBean implements 
jakarta.ejb.SessionBean {
 
                 /*[4] Commit or Rollback the transaction */
                 ut.commit();
-            } finally {
-                con.close();
             }
         } catch (final RollbackException re) {
             throw re;
@@ -119,11 +113,9 @@ public class BeanTxSingletonBean implements 
jakarta.ejb.SessionBean {
         final Account acct = new Account();
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?")) {
                     stmt.setString(1, ssn);
                     final ResultSet rs = stmt.executeQuery();
                     if (!rs.next()) return null;
@@ -132,11 +124,7 @@ public class BeanTxSingletonBean implements 
jakarta.ejb.SessionBean {
                     acct.setFirstName(rs.getString(2));
                     acct.setLastName(rs.getString(3));
                     acct.setBalance(rs.getInt(4));
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             e.printStackTrace();
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/ContainerTxSingletonBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/ContainerTxSingletonBean.java
index 027485d839..1137608356 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/ContainerTxSingletonBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/singleton/ContainerTxSingletonBean.java
@@ -80,22 +80,16 @@ public class ContainerTxSingletonBean implements 
jakarta.ejb.SessionBean {
 
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
+            try (Connection con = ds.getConnection()) {
                 /*[2] Update the table */
-                final PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
-                try {
+                try (PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)")) {
                     stmt.setString(1, acct.getSsn());
                     stmt.setString(2, acct.getFirstName());
                     stmt.setString(3, acct.getLastName());
                     stmt.setInt(4, acct.getBalance());
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             //throw new RemoteException("[Bean] "+e.getClass().getName()+" : 
"+e.getMessage());
@@ -106,11 +100,9 @@ public class ContainerTxSingletonBean implements 
jakarta.ejb.SessionBean {
         final Account acct = new Account();
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?")) {
                     stmt.setString(1, ssn);
                     final ResultSet rs = stmt.executeQuery();
                     if (!rs.next()) return null;
@@ -119,11 +111,7 @@ public class ContainerTxSingletonBean implements 
jakarta.ejb.SessionBean {
                     acct.setFirstName(rs.getString(2));
                     acct.setLastName(rs.getString(3));
                     acct.setBalance(rs.getInt(4));
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             //throw new RemoteException("[Bean] "+e.getClass().getName()+" : 
"+e.getMessage());
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateful/BeanTxStatefulBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateful/BeanTxStatefulBean.java
index 8467d13161..1167624621 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateful/BeanTxStatefulBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateful/BeanTxStatefulBean.java
@@ -103,23 +103,19 @@ public class BeanTxStatefulBean implements 
jakarta.ejb.SessionBean {
         try {
 
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/datasource");
-            final Connection con = ds.getConnection();
 
-            try {
+            try (Connection con = ds.getConnection()) {
                 final UserTransaction ut = ejbContext.getUserTransaction();
                 /*[1] Begin the transaction */
                 ut.begin();
 
                 /*[2] Update the table */
-                final PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
-                try {
+                try (PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)")) {
                     stmt.setString(1, acct.getSsn());
                     stmt.setString(2, acct.getFirstName());
                     stmt.setString(3, acct.getLastName());
                     stmt.setInt(4, acct.getBalance());
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
 
                 /*[3] Commit or Rollback the transaction */
@@ -128,8 +124,6 @@ public class BeanTxStatefulBean implements 
jakarta.ejb.SessionBean {
                 /*[4] Commit or Rollback the transaction */
                 ut.commit();
 
-            } finally {
-                con.close();
             }
         } catch (final RollbackException re) {
             throw re;
@@ -143,11 +137,9 @@ public class BeanTxStatefulBean implements 
jakarta.ejb.SessionBean {
         final Account acct = new Account();
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/datasource");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?")) {
                     stmt.setString(1, ssn);
                     final ResultSet rs = stmt.executeQuery();
                     if (!rs.next()) return null;
@@ -156,11 +148,7 @@ public class BeanTxStatefulBean implements 
jakarta.ejb.SessionBean {
                     acct.setFirstName(rs.getString(2));
                     acct.setLastName(rs.getString(3));
                     acct.setBalance(rs.getInt(4));
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             e.printStackTrace();
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/BeanTxStatelessBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/BeanTxStatelessBean.java
index 4c5013ec83..3a179e4f0f 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/BeanTxStatelessBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/BeanTxStatelessBean.java
@@ -80,23 +80,19 @@ public class BeanTxStatelessBean implements 
jakarta.ejb.SessionBean {
 
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
+            try (Connection con = ds.getConnection()) {
                 final UserTransaction ut = ejbContext.getUserTransaction();
                 /*[1] Begin the transaction */
                 ut.begin();
 
                 /*[2] Update the table */
-                final PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
-                try {
+                try (PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)")) {
                     stmt.setString(1, acct.getSsn());
                     stmt.setString(2, acct.getFirstName());
                     stmt.setString(3, acct.getLastName());
                     stmt.setInt(4, acct.getBalance());
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
 
                 /*[3] Commit or Rollback the transaction */
@@ -104,8 +100,6 @@ public class BeanTxStatelessBean implements 
jakarta.ejb.SessionBean {
 
                 /*[4] Commit or Rollback the transaction */
                 ut.commit();
-            } finally {
-                con.close();
             }
         } catch (final RollbackException re) {
             throw re;
@@ -119,11 +113,9 @@ public class BeanTxStatelessBean implements 
jakarta.ejb.SessionBean {
         final Account acct = new Account();
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?")) {
                     stmt.setString(1, ssn);
                     final ResultSet rs = stmt.executeQuery();
                     if (!rs.next()) return null;
@@ -132,11 +124,7 @@ public class BeanTxStatelessBean implements 
jakarta.ejb.SessionBean {
                     acct.setFirstName(rs.getString(2));
                     acct.setLastName(rs.getString(3));
                     acct.setBalance(rs.getInt(4));
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             e.printStackTrace();
diff --git 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/ContainerTxStatelessBean.java
 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/ContainerTxStatelessBean.java
index 6375ac76de..cfbbf8ba59 100644
--- 
a/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/ContainerTxStatelessBean.java
+++ 
b/itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/stateless/ContainerTxStatelessBean.java
@@ -80,22 +80,16 @@ public class ContainerTxStatelessBean implements 
jakarta.ejb.SessionBean {
 
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
+            try (Connection con = ds.getConnection()) {
                 /*[2] Update the table */
-                final PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)");
-                try {
+                try (PreparedStatement stmt = con.prepareStatement("insert 
into Account (SSN, First_name, Last_name, Balance) values (?,?,?,?)")) {
                     stmt.setString(1, acct.getSsn());
                     stmt.setString(2, acct.getFirstName());
                     stmt.setString(3, acct.getLastName());
                     stmt.setInt(4, acct.getBalance());
                     stmt.executeUpdate();
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             //throw new RemoteException("[Bean] "+e.getClass().getName()+" : 
"+e.getMessage());
@@ -106,11 +100,9 @@ public class ContainerTxStatelessBean implements 
jakarta.ejb.SessionBean {
         final Account acct = new Account();
         try {
             final DataSource ds = (DataSource) 
jndiContext.lookup("java:comp/env/database");
-            final Connection con = ds.getConnection();
 
-            try {
-                final PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?");
-                try {
+            try (Connection con = ds.getConnection()) {
+                try (PreparedStatement stmt = con.prepareStatement("select * 
from Account where SSN = ?")) {
                     stmt.setString(1, ssn);
                     final ResultSet rs = stmt.executeQuery();
                     if (!rs.next()) return null;
@@ -119,11 +111,7 @@ public class ContainerTxStatelessBean implements 
jakarta.ejb.SessionBean {
                     acct.setFirstName(rs.getString(2));
                     acct.setLastName(rs.getString(3));
                     acct.setBalance(rs.getInt(4));
-                } finally {
-                    stmt.close();
                 }
-            } finally {
-                con.close();
             }
         } catch (final Exception e) {
             //throw new RemoteException("[Bean] "+e.getClass().getName()+" : 
"+e.getMessage());
diff --git 
a/maven/jarstxt-maven-plugin/src/main/java/org/apache/openejb/maven/jarstxt/JarsTxtMojo.java
 
b/maven/jarstxt-maven-plugin/src/main/java/org/apache/openejb/maven/jarstxt/JarsTxtMojo.java
index 8d76499251..8d77ce182e 100644
--- 
a/maven/jarstxt-maven-plugin/src/main/java/org/apache/openejb/maven/jarstxt/JarsTxtMojo.java
+++ 
b/maven/jarstxt-maven-plugin/src/main/java/org/apache/openejb/maven/jarstxt/JarsTxtMojo.java
@@ -92,9 +92,7 @@ public class JarsTxtMojo extends AbstractMojo {
             FileUtils.mkdir(outputFile.getParentFile().getAbsolutePath());
         }
 
-        BufferedWriter writer = null;
-        try {
-            writer = new BufferedWriter(new FileWriter(outputFile));
+        try (BufferedWriter writer = new BufferedWriter(new 
FileWriter(outputFile))) {
 
             final TreeSet<String> set = new TreeSet<>();
 
@@ -163,14 +161,6 @@ public class JarsTxtMojo extends AbstractMojo {
             writer.flush();
         } catch (final IOException e) {
             getLog().error(e.getMessage(), e);
-        } finally {
-            if (writer != null) {
-                try {
-                    writer.close();
-                } catch (final IOException e) {
-                    // no-op
-                }
-            }
         }
     }
 
diff --git 
a/maven/openejb-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/embedded/OpenEJBEmbeddedMojo.java
 
b/maven/openejb-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/embedded/OpenEJBEmbeddedMojo.java
index 04ad451d53..f1475d7e28 100644
--- 
a/maven/openejb-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/embedded/OpenEJBEmbeddedMojo.java
+++ 
b/maven/openejb-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/embedded/OpenEJBEmbeddedMojo.java
@@ -67,9 +67,7 @@ public class OpenEJBEmbeddedMojo extends AbstractMojo {
         final ClassLoader oldCl = 
Thread.currentThread().getContextClassLoader();
         Thread.currentThread().setContextClassLoader(createClassLoader(oldCl));
 
-        EJBContainer container = null;
-        try {
-            container = EJBContainer.createEJBContainer(map());
+        try (EJBContainer container = EJBContainer.createEJBContainer(map())) {
             if (await) {
                 final CountDownLatch latch = new CountDownLatch(1);
                 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() 
{
@@ -85,9 +83,6 @@ public class OpenEJBEmbeddedMojo extends AbstractMojo {
                 }
             }
         } finally {
-            if (container != null) {
-                container.close();
-            }
             Thread.currentThread().setContextClassLoader(oldCl);
         }
     }
diff --git 
a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
 
b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
index d98cb82b12..2627f03e5a 100644
--- 
a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
+++ 
b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
@@ -1750,9 +1750,7 @@ public abstract class AbstractTomEEMojo extends 
AbstractAddressMojo {
     }
 
     private void unzip(final File mvnTomEE) {
-        ZipFile in = null;
-        try {
-            in = new ZipFile(mvnTomEE);
+        try (ZipFile in = new ZipFile(mvnTomEE)) {
 
             final Enumeration<? extends ZipEntry> entries = in.entries();
             while (entries.hasMoreElements()) {
@@ -1819,14 +1817,6 @@ public abstract class AbstractTomEEMojo extends 
AbstractAddressMojo {
             getLog().info(container + " was unzipped in '" + 
catalinaBase.getAbsolutePath() + "'");
         } catch (final Exception e) {
             throw new TomEEException(e.getMessage(), e);
-        } finally {
-            if (in != null) {
-                try {
-                    in.close();
-                } catch (final IOException e) {
-                    // no-op
-                }
-            }
         }
     }
 
diff --git 
a/server/openejb-client/src/main/java/org/apache/openejb/client/JaxWsProviderWrapper.java
 
b/server/openejb-client/src/main/java/org/apache/openejb/client/JaxWsProviderWrapper.java
index 7e9e049046..78f71c967f 100644
--- 
a/server/openejb-client/src/main/java/org/apache/openejb/client/JaxWsProviderWrapper.java
+++ 
b/server/openejb-client/src/main/java/org/apache/openejb/client/JaxWsProviderWrapper.java
@@ -381,24 +381,13 @@ public class JaxWsProviderWrapper extends Provider {
         // 1. META-INF/services/jakarta.xml.ws.spi.Provider
         try {
             for (final URL url : 
Collections.list(classLoader.getResources("META-INF/services/" + 
JAXWSPROVIDER_PROPERTY))) {
-                BufferedReader in = null;
-                try {
-                    in = new BufferedReader(new 
InputStreamReader(url.openStream()));
+                try (BufferedReader in = new BufferedReader(new 
InputStreamReader(url.openStream()))) {
 
                     providerClass = in.readLine();
                     provider = createProviderInstance(providerClass, 
classLoader);
                     if (provider != null) {
                         return provider;
                     }
-                } catch (Exception ignored) {
-                } finally {
-                    if (in != null) {
-                        try {
-                            in.close();
-                        } catch (Throwable e) {
-                            //ignore
-                        }
-                    }
                 }
             }
         } catch (Exception ignored) {
@@ -409,9 +398,7 @@ public class JaxWsProviderWrapper extends Provider {
         final String javaHome = System.getProperty("java.home");
         final File jaxrpcPropertiesFile = new File(new File(javaHome, "lib"), 
"jaxrpc.properties");
         if (jaxrpcPropertiesFile.exists()) {
-            InputStream in = null;
-            try {
-                in = new FileInputStream(jaxrpcPropertiesFile);
+            try (InputStream in = new FileInputStream(jaxrpcPropertiesFile)) {
                 final Properties properties = new Properties();
                 properties.load(in);
 
@@ -421,15 +408,8 @@ public class JaxWsProviderWrapper extends Provider {
                     return provider;
                 }
             } catch (Exception ignored) {
-            } finally {
-                if (in != null) {
-                    try {
-                        in.close();
-                    } catch (Throwable e) {
-                        //Ignore
-                    }
-                }
             }
+            //Ignore
         }
 
         // 3. System.getProperty("jakarta.xml.ws.spi.Provider")
diff --git 
a/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastPulseClient.java
 
b/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastPulseClient.java
index 207212067e..31a715198e 100644
--- 
a/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastPulseClient.java
+++ 
b/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastPulseClient.java
@@ -714,8 +714,7 @@ public class MulticastPulseClient extends 
MulticastConnectionFactory {
                             System.out.print(server + ":" + group + " - " + 
uriSub.toASCIIString() + " is reachable: ");
 
                             boolean b = false;
-                            final Socket s = new Socket();
-                            try {
+                            try (Socket s = new Socket()) {
                                 s.connect(new InetSocketAddress(host, port), 
st);
                                 b = true;
                             } catch (Exception e) {
@@ -723,14 +722,7 @@ public class MulticastPulseClient extends 
MulticastConnectionFactory {
                                     
MulticastPulseClient.broadcastBadUri(group, uriSub, mchost, mcport);
                                     System.out.print("" + e + " : ");
                                 }
-                            } finally {
-                                try {
-                                    s.close();
-                                } catch (Exception e) {
-                                    //Ignore
-                                }
                             }
-
                             System.out.println(b);
                         }
                     } else {
diff --git 
a/server/openejb-client/src/main/java/org/apache/openejb/client/ResourceFinder.java
 
b/server/openejb-client/src/main/java/org/apache/openejb/client/ResourceFinder.java
index 30a196b744..da27fe6563 100644
--- 
a/server/openejb-client/src/main/java/org/apache/openejb/client/ResourceFinder.java
+++ 
b/server/openejb-client/src/main/java/org/apache/openejb/client/ResourceFinder.java
@@ -898,31 +898,18 @@ public class ResourceFinder {
 
     private Properties loadProperties(final URL resource) throws IOException {
 
-        BufferedInputStream reader = null;
-
-        try {
-            reader = new BufferedInputStream(resource.openStream());
+        try (BufferedInputStream reader = new 
BufferedInputStream(resource.openStream())) {
             final Properties properties = new Properties();
             properties.load(reader);
 
             return properties;
-        } finally {
-            if (reader != null) {
-                try {
-                    reader.close();
-                } catch (Throwable e) {
-                    //Ignore
-                }
-            }
         }
     }
 
     private String readContents(final URL resource) throws IOException {
-        BufferedInputStream reader = null;
-        final StringBuilder sb = new StringBuilder();
 
-        try {
-            reader = new BufferedInputStream(resource.openStream());
+        final StringBuilder sb = new StringBuilder();
+        try (BufferedInputStream reader = new 
BufferedInputStream(resource.openStream())) {
 
             int b = reader.read();
             while (b != -1) {
@@ -931,14 +918,6 @@ public class ResourceFinder {
             }
 
             return sb.toString().trim();
-        } finally {
-            if (reader != null) {
-                try {
-                    reader.close();
-                } catch (Throwable e) {
-                    //Ignore
-                }
-            }
         }
     }
 
diff --git 
a/server/openejb-client/src/main/java/org/apache/openejb/client/SSLContextBuilder.java
 
b/server/openejb-client/src/main/java/org/apache/openejb/client/SSLContextBuilder.java
index 4e245085b9..50e85c1d2c 100644
--- 
a/server/openejb-client/src/main/java/org/apache/openejb/client/SSLContextBuilder.java
+++ 
b/server/openejb-client/src/main/java/org/apache/openejb/client/SSLContextBuilder.java
@@ -54,11 +54,8 @@ public class SSLContextBuilder {
             } else {
                 pwd = "changeit".toCharArray();
             }
-            FileInputStream fis = new FileInputStream(trustStore);
-            try {
+            try (FileInputStream fis = new FileInputStream(trustStore)) {
                 ks.load(fis, pwd);
-            } finally {
-                fis.close();
             }
             String sslTrustStoreProvider = params.get("sslTrustStoreProvider");
             TrustManagerFactory tmf = TrustManagerFactory.getInstance(null == 
sslTrustStoreProvider ? TrustManagerFactory.getDefaultAlgorithm() : 
sslTrustStoreProvider);
@@ -85,11 +82,8 @@ public class SSLContextBuilder {
             } else {
                 pwd = "changeit".toCharArray();
             }
-            FileInputStream fis = new FileInputStream(keyStore);
-            try {
+            try (FileInputStream fis = new FileInputStream(keyStore)) {
                 ks.load(fis, pwd);
-            } finally {
-                fis.close();
             }
             String sslKeyStoreProvider = params.get("sslKeyStoreProvider");
             KeyManagerFactory kmf = KeyManagerFactory.getInstance(null == 
sslKeyStoreProvider ? KeyManagerFactory.getDefaultAlgorithm() : 
sslKeyStoreProvider);
diff --git 
a/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/ScriptFileCommand.java
 
b/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/ScriptFileCommand.java
index 3f4ba7c9d5..14a4c6bc7b 100644
--- 
a/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/ScriptFileCommand.java
+++ 
b/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/command/ScriptFileCommand.java
@@ -41,9 +41,7 @@ public class ScriptFileCommand extends ScriptCommand {
         final StringBuilder builder = new StringBuilder(1024);
         builder.append("script ").append(language).append(" "); // we will run 
the parent command
 
-        BufferedReader reader = null;
-        try {
-            reader = new BufferedReader(new FileReader(file));
+        try (BufferedReader reader = new BufferedReader(new FileReader(file))) 
{
             char[] buf = new char[1024];
             int numRead;
             while ((numRead = reader.read(buf)) != -1) {
@@ -54,14 +52,6 @@ public class ScriptFileCommand extends ScriptCommand {
         } catch (Exception e) {
             streamManager.writeErr(e);
             return;
-        } finally {
-            if (reader != null) {
-                try {
-                    reader.close();
-                } catch (IOException e) {
-                    // ignored
-                }
-            }
         }
 
         super.execute(builder.toString());
diff --git 
a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
 
b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
index 5cdc586bc7..b0e82b19da 100644
--- 
a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
+++ 
b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
@@ -316,7 +316,7 @@ public class CxfRsHttpListener implements RsHttpListener {
         if (is == null) {
             return false;
         }
-        try {
+        try (is) {
             final int ind = pathInfo.lastIndexOf(".");
             if (ind != -1 && ind < pathInfo.length()) {
                 final String type = 
STATIC_CONTENT_TYPES.get(pathInfo.substring(ind + 1));
@@ -331,12 +331,6 @@ public class CxfRsHttpListener implements RsHttpListener {
             response.setStatus(HttpURLConnection.HTTP_OK);
         } catch (final IOException ex) {
             throw new ServletException("Static resource " + pathInfo + " can 
not be written to the output stream");
-        } finally {
-            try {
-                is.close();
-            } catch (final IOException e) {
-                // no-op
-            }
         }
         return true;
     }
diff --git 
a/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java
 
b/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java
index 7baac1de7c..53a9cd1173 100644
--- 
a/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java
+++ 
b/server/openejb-server/src/main/java/org/apache/openejb/server/admin/AdminDaemon.java
@@ -36,10 +36,8 @@ public class AdminDaemon implements ServerService {
 
     @Override
     public void service(Socket socket) throws ServiceException, IOException {
-        InputStream in = null;
 
-        try {
-            in = socket.getInputStream();
+        try (InputStream in = socket.getInputStream()) {
 
             byte requestTypeByte = (byte) in.read();
             try {
@@ -69,14 +67,6 @@ public class AdminDaemon implements ServerService {
         } catch (Throwable e) {
             Logger.getInstance(LogCategory.OPENEJB_SERVER, 
AdminDaemon.class).warning("Server Socket request failed", e);
         } finally {
-            if (null != in) {
-                try {
-                    in.close();
-                } catch (Throwable t) {
-                    //Ignore
-                }
-            }
-
             if (null != socket) {
                 try {
                     socket.close();
diff --git 
a/server/openejb-server/src/main/java/org/apache/openejb/server/admin/Stop.java 
b/server/openejb-server/src/main/java/org/apache/openejb/server/admin/Stop.java
index fda3b95c59..7d3329b07d 100644
--- 
a/server/openejb-server/src/main/java/org/apache/openejb/server/admin/Stop.java
+++ 
b/server/openejb-server/src/main/java/org/apache/openejb/server/admin/Stop.java
@@ -34,13 +34,8 @@ public class Stop {
 
     public static void stop(final String host, final int port) {
 
-        Socket socket = null;
-        OutputStream out = null;
-
-        try {
-
-            socket = new Socket(host, port);
-            out = socket.getOutputStream();
+        try (Socket socket = new Socket(host, port);
+             OutputStream out = socket.getOutputStream()) {
 
             out.write(RequestType.STOP_REQUEST_Stop.getCode());
 
@@ -48,22 +43,6 @@ public class Stop {
             System.out.println(":: server not running ::");
         } catch (Exception e) {
             e.printStackTrace();
-        } finally {
-            if (null != out) {
-                try {
-                    out.close();
-                } catch (Throwable e) {
-                    //Ignore
-                }
-            }
-
-            if (null != socket) {
-                try {
-                    socket.close();
-                } catch (Throwable e) {
-                    //Ignore
-                }
-            }
         }
     }
 
diff --git 
a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomEEClassLoaderEnricher.java
 
b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomEEClassLoaderEnricher.java
index d1d50aee89..75579b01cd 100644
--- 
a/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomEEClassLoaderEnricher.java
+++ 
b/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomEEClassLoaderEnricher.java
@@ -164,10 +164,7 @@ public final class TomEEClassLoaderEnricher implements 
WebAppEnricher {
 
         final ClassLoader parent = 
TomEEClassLoaderEnricher.class.getClassLoader();
 
-        JarFile jarFile = null;
-
-        try {
-            jarFile = new JarFile(file);
+        try (JarFile jarFile = new JarFile(file)) {
             for (final String[] name : FORBIDDEN_CLASSES) {
                 // if we can't load if from our classLoader we'll not impose 
anything on this class
                 boolean found = false;
@@ -200,14 +197,12 @@ public final class TomEEClassLoaderEnricher implements 
WebAppEnricher {
                             return true;
                         }
                     }
-                    final URLClassLoader tmpLoader = new URLClassLoader(new 
URL[]{ file.toURI().toURL() });
-                    final URL resource = tmpLoader.getResource(entry);
-                    try {
+                    final URLClassLoader tmpLoader = new URLClassLoader(new 
URL[]{file.toURI().toURL()});
+                    try (tmpLoader) {
+                        final URL resource = tmpLoader.getResource(entry);
                         if (resource != null && 
resource.equals(parent.getResource(entry))) {
                             continue;
                         }
-                    } finally {
-                        tmpLoader.close();
                     }
 
                     LOGGER.warning("jar '" + file.getAbsolutePath() + "' 
contains offending class: " + name[0]
@@ -216,14 +211,6 @@ public final class TomEEClassLoaderEnricher implements 
WebAppEnricher {
                 }
             }
             return true;
-        } finally {
-            if (jarFile != null) { // in java 6 JarFile is not Closeable so 
don't use IO.close()
-                try {
-                    jarFile.close();
-                } catch (final IOException ioe) {
-                    // Ignored
-                }
-            }
         }
     }
 }
diff --git 
a/tomee/tomee-common/src/main/java/org/apache/tomee/installer/Installer.java 
b/tomee/tomee-common/src/main/java/org/apache/tomee/installer/Installer.java
index 5b8eacffa3..89489b809a 100644
--- a/tomee/tomee-common/src/main/java/org/apache/tomee/installer/Installer.java
+++ b/tomee/tomee-common/src/main/java/org/apache/tomee/installer/Installer.java
@@ -820,9 +820,7 @@ public class Installer implements InstallerInterface {
 
         final File openejbSystemProperties = new File(confDir, 
"system.properties");
         if (!openejbSystemProperties.exists()) {
-            FileWriter systemPropertiesWriter = null;
-            try {
-                systemPropertiesWriter = new 
FileWriter(openejbSystemProperties);
+            try (FileWriter systemPropertiesWriter = new 
FileWriter(openejbSystemProperties)) {
 
                 systemPropertiesWriter.write("# Licensed to the Apache 
Software Foundation (ASF) under one or more\n" +
                         "# contributor license agreements.  See the NOTICE 
file distributed with\n" +
@@ -945,14 +943,6 @@ public class Installer implements InstallerInterface {
 
             } catch (final IOException e) {
                 // ignored, this file is far to be mandatory
-            } finally {
-                if (systemPropertiesWriter != null) {
-                    try {
-                        systemPropertiesWriter.close();
-                    } catch (final IOException e) {
-                        // no-op
-                    }
-                }
             }
         }
 
diff --git 
a/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/OpenEJBListener.java 
b/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/OpenEJBListener.java
index 3cd51d0b6f..140a8ba535 100644
--- 
a/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/OpenEJBListener.java
+++ 
b/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/OpenEJBListener.java
@@ -288,9 +288,7 @@ public class OpenEJBListener implements LifecycleListener {
                 input = jarFile.getInputStream(jarEntry);
 
                 final File file = new File(dest, name);
-                BufferedOutputStream output = null;
-                try {
-                    output = new BufferedOutputStream(new 
FileOutputStream(file));
+                try (BufferedOutputStream output = new 
BufferedOutputStream(new FileOutputStream(file))) {
                     final byte[] buffer = new byte[2048];
                     while (true) {
                         final int n = input.read(buffer);
@@ -299,14 +297,6 @@ public class OpenEJBListener implements LifecycleListener {
                         }
                         output.write(buffer, 0, n);
                     }
-                } finally {
-                    if (output != null) {
-                        try {
-                            output.close();
-                        } catch (final IOException e) {
-                            // Ignore
-                        }
-                    }
                 }
 
                 final long lastModified = jarEntry.getTime();
diff --git 
a/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/TomcatEmbedder.java 
b/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/TomcatEmbedder.java
index e837964fd4..af06186da3 100644
--- 
a/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/TomcatEmbedder.java
+++ 
b/tomee/tomee-loader/src/main/java/org/apache/tomee/loader/TomcatEmbedder.java
@@ -73,12 +73,10 @@ public class TomcatEmbedder {
         final ClassLoader oldCl = 
Thread.currentThread().getContextClassLoader();
         // set the ClassLoader to the one which loaded ServletConfig.class 
i.e. the parent ClassLoader
         Thread.currentThread().setContextClassLoader(catalinaCl);
-        URLClassLoader childCl = null;
-        try {
-            childCl = new URLClassLoader(new URL[]{
-                    getThisJar().toURI().toURL(),
-                    findOpenEJBJar(openejbWar, 
OPENEJB_LOADER_PREFIX).toURI().toURL()
-            });
+        try (URLClassLoader childCl = new URLClassLoader(new URL[]{
+                getThisJar().toURI().toURL(),
+                findOpenEJBJar(openejbWar, 
OPENEJB_LOADER_PREFIX).toURI().toURL()
+        })) {
 
             // TomcatHook.hook()
             //This is loaded by childCl and is defined in the tomee-loader
@@ -89,13 +87,6 @@ public class TomcatEmbedder {
         } catch (final Throwable e) {
             e.printStackTrace();
         } finally {
-            if (childCl != null) {
-                try {
-                    childCl.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
             Thread.currentThread().setContextClassLoader(oldCl);
         }
     }

Reply via email to