@@ -182,31 +182,31 @@ public void loadPolicy(Model model) {
182
182
if (ctx .isRetry ()) {
183
183
retry (ctx );
184
184
}
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
+ }
205
206
}
207
+ loadPolicyLine (line , model );
206
208
}
207
- loadPolicyLine (line , model );
208
209
}
209
- rSet .close ();
210
210
});
211
211
}
212
212
@@ -305,7 +305,7 @@ public void savePolicy(Model model) {
305
305
conn .rollback ();
306
306
307
307
e .printStackTrace ();
308
- throw new Error ( e ) ;
308
+ throw e ;
309
309
} finally {
310
310
conn .setAutoCommit (true );
311
311
}
@@ -325,18 +325,19 @@ public void addPolicy(String sec, String ptype, List<String> rule) {
325
325
if (ctx .isRetry ()) {
326
326
retry (ctx );
327
327
}
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
+ }
340
341
});
341
342
}
342
343
@@ -367,14 +368,14 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin
367
368
sql = String .format ("%s%s%s%s" , sql , " AND v" , columnIndex , " = ?" );
368
369
columnIndex ++;
369
370
}
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 ();
374
378
}
375
-
376
- ps .addBatch ();
377
- ps .executeBatch ();
378
379
});
379
380
}
380
381
0 commit comments