Skip to content

Commit

Permalink
Merge pull request #44 from seriouszyx/dev
Browse files Browse the repository at this point in the history
fix: close the (Prepared) Statement and ReusltSet with the try-catch-resources statement
  • Loading branch information
hsluoyz authored Nov 23, 2021
2 parents a5480f2 + 4734af1 commit 1f9e8aa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 67 deletions.
50 changes: 25 additions & 25 deletions src/main/java/org/casbin/adapter/JDBCAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,34 @@ private void loadFilteredPolicyFile(Model model, Filter filter, Helper.loadPolic
if (ctx.isRetry()) {
retry(ctx);
}
Statement stmt = conn.createStatement();
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule");
ResultSetMetaData rData = rSet.getMetaData();
while (rSet.next()) {
CasbinRule line = new CasbinRule();
for (int i = 1; i <= rData.getColumnCount(); i++) {
if (i == 2) {
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 3) {
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 4) {
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 5) {
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 6) {
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 7) {
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 8) {
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
try (Statement stmt = conn.createStatement();
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule")) {
ResultSetMetaData rData = rSet.getMetaData();
while (rSet.next()) {
CasbinRule line = new CasbinRule();
for (int i = 1; i <= rData.getColumnCount(); i++) {
if (i == 2) {
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 3) {
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 4) {
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 5) {
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 6) {
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 7) {
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 8) {
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
}
}
if (filterLine(line, filter)) {
continue;
}
loadPolicyLine(line, model);
}
if (filterLine(line, filter)) {
continue;
}
loadPolicyLine(line, model);
}
rSet.close();
});
}

Expand Down
85 changes: 43 additions & 42 deletions src/main/java/org/casbin/adapter/JDBCBaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,31 +182,31 @@ public void loadPolicy(Model model) {
if (ctx.isRetry()) {
retry(ctx);
}
Statement stmt = conn.createStatement();
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule");
ResultSetMetaData rData = rSet.getMetaData();
while (rSet.next()) {
CasbinRule line = new CasbinRule();
for (int i = 1; i <= rData.getColumnCount(); i++) {
if (i == 2) {
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 3) {
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 4) {
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 5) {
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 6) {
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 7) {
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 8) {
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
try (Statement stmt = conn.createStatement();
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule")) {
ResultSetMetaData rData = rSet.getMetaData();
while (rSet.next()) {
CasbinRule line = new CasbinRule();
for (int i = 1; i <= rData.getColumnCount(); i++) {
if (i == 2) {
line.ptype = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 3) {
line.v0 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 4) {
line.v1 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 5) {
line.v2 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 6) {
line.v3 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 7) {
line.v4 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
} else if (i == 8) {
line.v5 = rSet.getObject(i) == null ? "" : (String) rSet.getObject(i);
}
}
loadPolicyLine(line, model);
}
loadPolicyLine(line, model);
}
rSet.close();
});
}

Expand Down Expand Up @@ -305,7 +305,7 @@ public void savePolicy(Model model) {
conn.rollback();

e.printStackTrace();
throw new Error(e);
throw e;
} finally {
conn.setAutoCommit(true);
}
Expand All @@ -325,18 +325,19 @@ public void addPolicy(String sec, String ptype, List<String> rule) {
if (ctx.isRetry()) {
retry(ctx);
}
PreparedStatement ps = conn.prepareStatement(sql);
CasbinRule line = savePolicyLine(ptype, rule);

ps.setString(1, line.ptype);
ps.setString(2, line.v0);
ps.setString(3, line.v1);
ps.setString(4, line.v2);
ps.setString(5, line.v3);
ps.setString(6, line.v4);
ps.setString(7, line.v5);
ps.addBatch();
ps.executeBatch();
try (PreparedStatement ps = conn.prepareStatement(sql)) {
CasbinRule line = savePolicyLine(ptype, rule);

ps.setString(1, line.ptype);
ps.setString(2, line.v0);
ps.setString(3, line.v1);
ps.setString(4, line.v2);
ps.setString(5, line.v3);
ps.setString(6, line.v4);
ps.setString(7, line.v5);
ps.addBatch();
ps.executeBatch();
}
});
}

Expand Down Expand Up @@ -367,14 +368,14 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
sql = String.format("%s%s%s%s", sql, " AND v", columnIndex, " = ?");
columnIndex++;
}
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, ptype);
for (int j = 0; j < values.size(); j++) {
ps.setString(j + 2, values.get(j));
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, ptype);
for (int j = 0; j < values.size(); j++) {
ps.setString(j + 2, values.get(j));
}
ps.addBatch();
ps.executeBatch();
}

ps.addBatch();
ps.executeBatch();
});
}

Expand Down

0 comments on commit 1f9e8aa

Please sign in to comment.