Skip to content

Commit cbbcd57

Browse files
authored
Make use of automatic resource mgmt via try-with-resources pattern (#2539)
1 parent 02cd594 commit cbbcd57

File tree

29 files changed

+90
-466
lines changed

29 files changed

+90
-466
lines changed

arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/Setup.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,11 @@ public static File downloadFile(final String artifactName, final String altUrl,
184184
}
185185

186186
public static boolean isRunning(final String host, final int port) {
187-
Socket socket = null;
188-
try {
189-
socket = new Socket(host, port);
187+
try (Socket socket = new Socket(host, port)) {
190188
socket.getOutputStream().close();
191189
return true;
192190
} catch (final Exception e) {
193191
return false;
194-
} finally {
195-
if (socket != null) {
196-
try {
197-
socket.close();
198-
} catch (final IOException ignored) {
199-
// no-op
200-
}
201-
}
202192
}
203193
}
204194

arquillian/ziplock/src/main/java/org/apache/ziplock/ResourceFinder.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -887,32 +887,21 @@ private static void readJarEntries(final URL location, final String basePath, fi
887887
}
888888

889889
private Properties loadProperties(final URL resource) throws IOException {
890-
final InputStream in = resource.openStream();
891890

892-
BufferedInputStream reader = null;
893-
try {
894-
reader = new BufferedInputStream(in);
891+
try (InputStream in = resource.openStream();
892+
BufferedInputStream reader = new BufferedInputStream(in)) {
895893
final Properties properties = new Properties();
896894
properties.load(reader);
897895

898896
return properties;
899-
} finally {
900-
try {
901-
in.close();
902-
reader.close();
903-
} catch (final Exception e) {
904-
// no-op
905-
}
906897
}
907898
}
908899

909900
private String readContents(final URL resource) throws IOException {
910-
final InputStream in = resource.openStream();
911-
BufferedInputStream reader = null;
912901
final StringBuffer sb = new StringBuffer();
913902

914-
try {
915-
reader = new BufferedInputStream(in);
903+
try (InputStream in = resource.openStream();
904+
BufferedInputStream reader = new BufferedInputStream(in)) {
916905

917906
int b = reader.read();
918907
while (b != -1) {
@@ -921,13 +910,6 @@ private String readContents(final URL resource) throws IOException {
921910
}
922911

923912
return sb.toString().trim();
924-
} finally {
925-
try {
926-
in.close();
927-
reader.close();
928-
} catch (final Exception e) {
929-
// no-op
930-
}
931913
}
932914
}
933915

examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,45 +47,35 @@ public class Movies {
4747

4848
@PostConstruct
4949
private void construct() throws Exception {
50-
Connection connection = movieDatabase.getConnection();
51-
try {
50+
try (Connection connection = movieDatabase.getConnection()) {
5251
PreparedStatement stmt = connection.prepareStatement("CREATE TABLE movie ( director VARCHAR(255), title VARCHAR(255), year integer)");
5352
stmt.execute();
54-
} finally {
55-
connection.close();
5653
}
5754
}
5855

5956
public void addMovie(Movie movie) throws Exception {
60-
Connection conn = movieDatabase.getConnection();
61-
try {
57+
try (Connection conn = movieDatabase.getConnection()) {
6258
PreparedStatement sql = conn.prepareStatement("INSERT into movie (director, title, year) values (?, ?, ?)");
6359
sql.setString(1, movie.getDirector());
6460
sql.setString(2, movie.getTitle());
6561
sql.setInt(3, movie.getYear());
6662
sql.execute();
67-
} finally {
68-
conn.close();
6963
}
7064
}
7165

7266
public void deleteMovie(Movie movie) throws Exception {
73-
Connection conn = movieDatabase.getConnection();
74-
try {
67+
try (Connection conn = movieDatabase.getConnection()) {
7568
PreparedStatement sql = conn.prepareStatement("DELETE from movie where director = ? AND title = ? AND year = ?");
7669
sql.setString(1, movie.getDirector());
7770
sql.setString(2, movie.getTitle());
7871
sql.setInt(3, movie.getYear());
7972
sql.execute();
80-
} finally {
81-
conn.close();
8273
}
8374
}
8475

8576
public List<Movie> getMovies() throws Exception {
8677
ArrayList<Movie> movies = new ArrayList<>();
87-
Connection conn = movieDatabase.getConnection();
88-
try {
78+
try (Connection conn = movieDatabase.getConnection()) {
8979
PreparedStatement sql = conn.prepareStatement("SELECT director, title, year from movie");
9080
ResultSet set = sql.executeQuery();
9181
while (set.next()) {
@@ -95,9 +85,6 @@ public List<Movie> getMovies() throws Exception {
9585
movie.setYear(set.getInt("year"));
9686
movies.add(movie);
9787
}
98-
99-
} finally {
100-
conn.close();
10188
}
10289
return movies;
10390
}

itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/CalculatorBean.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ protected void doJdbcCall() {
4646

4747
con = ds.getConnection();
4848

49-
final Statement stmt = con.createStatement();
50-
try {
49+
try (Statement stmt = con.createStatement()) {
5150
final ResultSet rs = stmt.executeQuery("select * from Employees");
5251
while (rs.next())
5352
System.out.println(rs.getString(2));
54-
} finally {
55-
stmt.close();
5653
}
5754

5855
} catch (final javax.naming.NamingException re) {

itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/DatabaseBean.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,11 @@ public void executeQuery(final String statement) throws java.sql.SQLException {
5151
try {
5252

5353
final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/database");
54-
final Connection con = ds.getConnection();
5554

56-
try {
57-
final PreparedStatement stmt = con.prepareStatement(statement);
58-
try {
55+
try (Connection con = ds.getConnection()) {
56+
try (PreparedStatement stmt = con.prepareStatement(statement)) {
5957
stmt.executeQuery();
60-
} finally {
61-
stmt.close();
6258
}
63-
} finally {
64-
con.close();
6559
}
6660
} catch (final Exception e) {
6761
throw new EJBException("Cannot execute the statement: " + statement + e.getMessage());
@@ -76,11 +70,8 @@ public boolean execute(final String statement) throws java.sql.SQLException {
7670
final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/database");
7771
con = ds.getConnection();
7872

79-
final Statement stmt = con.createStatement();
80-
try {
73+
try (Statement stmt = con.createStatement()) {
8174
retval = stmt.execute(statement);
82-
} finally {
83-
stmt.close();
8475
}
8576

8677
} catch (final javax.naming.NamingException e) {

itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/EmployeeBean.java

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,12 @@ public Integer ejbFindByPrimaryKey(final Integer primaryKey)
4343

4444
final javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext.lookup("java:comp/env/jdbc/orders");
4545

46-
final Connection con = ds.getConnection();
47-
48-
try {
49-
final PreparedStatement stmt = con.prepareStatement("select * from Employees where EmployeeID = ?");
50-
try {
46+
try (Connection con = ds.getConnection()) {
47+
try (PreparedStatement stmt = con.prepareStatement("select * from Employees where EmployeeID = ?")) {
5148
stmt.setInt(1, primaryKey);
5249
final ResultSet rs = stmt.executeQuery();
5350
found = rs.next();
54-
} finally {
55-
stmt.close();
5651
}
57-
} finally {
58-
con.close();
5952
}
6053
} catch (final Exception e) {
6154
e.printStackTrace();
@@ -80,15 +73,12 @@ public java.util.Collection ejbFindAll() throws FinderException {
8073

8174
java.util.Vector keys;
8275
try {
83-
final Statement stmt = con.createStatement();
84-
try {
76+
try (Statement stmt = con.createStatement()) {
8577
final ResultSet rs = stmt.executeQuery("select EmployeeID from Employees");
8678
keys = new java.util.Vector();
8779
while (rs.next()) {
8880
keys.addElement(rs.getInt("EmployeeID"));
8981
}
90-
} finally {
91-
stmt.close();
9282
}
9383
} finally {
9484
con.close();
@@ -110,9 +100,7 @@ public Integer ejbCreate(final String fname, final String lname)
110100

111101
final javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext.lookup("java:comp/env/jdbc/orders");
112102

113-
final Connection con = ds.getConnection();
114-
115-
try {
103+
try (Connection con = ds.getConnection()) {
116104
PreparedStatement stmt = con.prepareStatement("insert into Employees (FirstName, LastName) values (?,?)");
117105
try {
118106
stmt.setString(1, firstName);
@@ -128,8 +116,6 @@ public Integer ejbCreate(final String fname, final String lname)
128116
} finally {
129117
stmt.close();
130118
}
131-
} finally {
132-
con.close();
133119
}
134120

135121
return id;
@@ -162,22 +148,16 @@ public void ejbLoad() {
162148

163149
final javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext.lookup("java:comp/env/jdbc/orders");
164150

165-
final Connection con = ds.getConnection();
166-
try {
167-
final PreparedStatement stmt = con.prepareStatement("select * from Employees where EmployeeID = ?");
168-
try {
151+
try (Connection con = ds.getConnection()) {
152+
try (PreparedStatement stmt = con.prepareStatement("select * from Employees where EmployeeID = ?")) {
169153
final Integer primaryKey = (Integer) ejbContext.getPrimaryKey();
170154
stmt.setInt(1, primaryKey);
171155
final ResultSet rs = stmt.executeQuery();
172156
while (rs.next()) {
173157
lastName = rs.getString("LastName");
174158
firstName = rs.getString("FirstName");
175159
}
176-
} finally {
177-
stmt.close();
178160
}
179-
} finally {
180-
con.close();
181161
}
182162

183163
} catch (final Exception e) {
@@ -191,20 +171,14 @@ public void ejbStore() {
191171
final InitialContext jndiContext = new InitialContext();
192172

193173
final javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext.lookup("java:comp/env/jdbc/orders");
194-
final Connection con = ds.getConnection();
195174

196-
try {
197-
final PreparedStatement stmt = con.prepareStatement("update Employees set FirstName = ?, LastName = ? where EmployeeID = ?");
198-
try {
175+
try (Connection con = ds.getConnection()) {
176+
try (PreparedStatement stmt = con.prepareStatement("update Employees set FirstName = ?, LastName = ? where EmployeeID = ?")) {
199177
stmt.setString(1, firstName);
200178
stmt.setString(2, lastName);
201179
stmt.setInt(3, id);
202180
stmt.execute();
203-
} finally {
204-
stmt.close();
205181
}
206-
} finally {
207-
con.close();
208182
}
209183
} catch (final Exception e) {
210184
e.printStackTrace();
@@ -226,19 +200,12 @@ public void ejbRemove() {
226200
final javax.sql.DataSource ds =
227201
(javax.sql.DataSource) jndiContext.lookup("java:comp/env/jdbc/orders");
228202

229-
final Connection con = ds.getConnection();
230-
231-
try {
232-
final PreparedStatement stmt = con.prepareStatement("delete from Employees where EmployeeID = ?");
233-
try {
203+
try (Connection con = ds.getConnection()) {
204+
try (PreparedStatement stmt = con.prepareStatement("delete from Employees where EmployeeID = ?")) {
234205
final Integer primaryKey = (Integer) ejbContext.getPrimaryKey();
235206
stmt.setInt(1, primaryKey);
236207
stmt.executeUpdate();
237-
} finally {
238-
stmt.close();
239208
}
240-
} finally {
241-
con.close();
242209
}
243210

244211
} catch (final Exception e) {

itests/openejb-itests-beans/src/main/java/org/apache/openejb/test/beans/ShoppingCartBean.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public void doJdbcCall() {
7979

8080
con = ds.getConnection();
8181

82-
final Statement stmt = con.createStatement();
83-
try {
82+
try (Statement stmt = con.createStatement()) {
8483
final ResultSet rs = stmt.executeQuery("select * from Employees");
8584
while (rs.next())
8685
System.out.println(rs.getString(2));
@@ -90,8 +89,6 @@ public void doJdbcCall() {
9089
calc.sub(1, 2);
9190

9291
final int i = 1;
93-
} finally {
94-
stmt.close();
9592
}
9693

9794
} catch (final java.rmi.RemoteException re) {

0 commit comments

Comments
 (0)