Skip to content

Commit 1f9e8aa

Browse files
authored
Merge pull request #44 from seriouszyx/dev
fix: close the (Prepared) Statement and ReusltSet with the try-catch-resources statement
2 parents a5480f2 + 4734af1 commit 1f9e8aa

File tree

2 files changed

+68
-67
lines changed

2 files changed

+68
-67
lines changed

src/main/java/org/casbin/adapter/JDBCAdapter.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,34 @@ private void loadFilteredPolicyFile(Model model, Filter filter, Helper.loadPolic
104104
if (ctx.isRetry()) {
105105
retry(ctx);
106106
}
107-
Statement stmt = conn.createStatement();
108-
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule");
109-
ResultSetMetaData rData = rSet.getMetaData();
110-
while (rSet.next()) {
111-
CasbinRule line = new CasbinRule();
112-
for (int i = 1; i <= rData.getColumnCount(); i++) {
113-
if (i == 2) {
114-
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
115-
} else if (i == 3) {
116-
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
117-
} else if (i == 4) {
118-
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
119-
} else if (i == 5) {
120-
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
121-
} else if (i == 6) {
122-
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
123-
} else if (i == 7) {
124-
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
125-
} else if (i == 8) {
126-
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
107+
try (Statement stmt = conn.createStatement();
108+
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule")) {
109+
ResultSetMetaData rData = rSet.getMetaData();
110+
while (rSet.next()) {
111+
CasbinRule line = new CasbinRule();
112+
for (int i = 1; i <= rData.getColumnCount(); i++) {
113+
if (i == 2) {
114+
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
115+
} else if (i == 3) {
116+
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
117+
} else if (i == 4) {
118+
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
119+
} else if (i == 5) {
120+
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
121+
} else if (i == 6) {
122+
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
123+
} else if (i == 7) {
124+
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
125+
} else if (i == 8) {
126+
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
127+
}
127128
}
129+
if (filterLine(line, filter)) {
130+
continue;
131+
}
132+
loadPolicyLine(line, model);
128133
}
129-
if (filterLine(line, filter)) {
130-
continue;
131-
}
132-
loadPolicyLine(line, model);
133134
}
134-
rSet.close();
135135
});
136136
}
137137

src/main/java/org/casbin/adapter/JDBCBaseAdapter.java

+43-42
Original file line numberDiff line numberDiff line change
@@ -182,31 +182,31 @@ public void loadPolicy(Model model) {
182182
if (ctx.isRetry()) {
183183
retry(ctx);
184184
}
185-
Statement stmt = conn.createStatement();
186-
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule");
187-
ResultSetMetaData rData = rSet.getMetaData();
188-
while (rSet.next()) {
189-
CasbinRule line = new CasbinRule();
190-
for (int i = 1; i <= rData.getColumnCount(); i++) {
191-
if (i == 2) {
192-
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
193-
} else if (i == 3) {
194-
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
195-
} else if (i == 4) {
196-
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
197-
} else if (i == 5) {
198-
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
199-
} else if (i == 6) {
200-
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
201-
} else if (i == 7) {
202-
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
203-
} else if (i == 8) {
204-
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
185+
try (Statement stmt = conn.createStatement();
186+
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule")) {
187+
ResultSetMetaData rData = rSet.getMetaData();
188+
while (rSet.next()) {
189+
CasbinRule line = new CasbinRule();
190+
for (int i = 1; i <= rData.getColumnCount(); i++) {
191+
if (i == 2) {
192+
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
193+
} else if (i == 3) {
194+
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
195+
} else if (i == 4) {
196+
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
197+
} else if (i == 5) {
198+
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
199+
} else if (i == 6) {
200+
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
201+
} else if (i == 7) {
202+
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
203+
} else if (i == 8) {
204+
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
205+
}
205206
}
207+
loadPolicyLine(line, model);
206208
}
207-
loadPolicyLine(line, model);
208209
}
209-
rSet.close();
210210
});
211211
}
212212

@@ -305,7 +305,7 @@ public void savePolicy(Model model) {
305305
conn.rollback();
306306

307307
e.printStackTrace();
308-
throw new Error(e);
308+
throw e;
309309
} finally {
310310
conn.setAutoCommit(true);
311311
}
@@ -325,18 +325,19 @@ public void addPolicy(String sec, String ptype, List<String> rule) {
325325
if (ctx.isRetry()) {
326326
retry(ctx);
327327
}
328-
PreparedStatement ps = conn.prepareStatement(sql);
329-
CasbinRule line = savePolicyLine(ptype, rule);
330-
331-
ps.setString(1, line.ptype);
332-
ps.setString(2, line.v0);
333-
ps.setString(3, line.v1);
334-
ps.setString(4, line.v2);
335-
ps.setString(5, line.v3);
336-
ps.setString(6, line.v4);
337-
ps.setString(7, line.v5);
338-
ps.addBatch();
339-
ps.executeBatch();
328+
try (PreparedStatement ps = conn.prepareStatement(sql)) {
329+
CasbinRule line = savePolicyLine(ptype, rule);
330+
331+
ps.setString(1, line.ptype);
332+
ps.setString(2, line.v0);
333+
ps.setString(3, line.v1);
334+
ps.setString(4, line.v2);
335+
ps.setString(5, line.v3);
336+
ps.setString(6, line.v4);
337+
ps.setString(7, line.v5);
338+
ps.addBatch();
339+
ps.executeBatch();
340+
}
340341
});
341342
}
342343

@@ -367,14 +368,14 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
367368
sql = String.format("%s%s%s%s", sql, " AND v", columnIndex, " = ?");
368369
columnIndex++;
369370
}
370-
PreparedStatement ps = conn.prepareStatement(sql);
371-
ps.setString(1, ptype);
372-
for (int j = 0; j < values.size(); j++) {
373-
ps.setString(j + 2, values.get(j));
371+
try (PreparedStatement ps = conn.prepareStatement(sql)) {
372+
ps.setString(1, ptype);
373+
for (int j = 0; j < values.size(); j++) {
374+
ps.setString(j + 2, values.get(j));
375+
}
376+
ps.addBatch();
377+
ps.executeBatch();
374378
}
375-
376-
ps.addBatch();
377-
ps.executeBatch();
378379
});
379380
}
380381

0 commit comments

Comments
 (0)