diff --git a/pom.xml b/pom.xml
index ba2a61a..a87d7ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -217,6 +217,11 @@
mssql-jdbc
8.2.2.jre8
+
+ dev.failsafe
+ failsafe
+ 3.0.0
+
diff --git a/src/main/java/org/casbin/adapter/JDBCAdapter.java b/src/main/java/org/casbin/adapter/JDBCAdapter.java
index cd80be5..86af2ae 100644
--- a/src/main/java/org/casbin/adapter/JDBCAdapter.java
+++ b/src/main/java/org/casbin/adapter/JDBCAdapter.java
@@ -14,6 +14,7 @@
package org.casbin.adapter;
+import dev.failsafe.Failsafe;
import org.casbin.jcasbin.exception.CasbinAdapterException;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.FilteredAdapter;
@@ -99,7 +100,11 @@ public boolean isFiltered() {
* loadFilteredPolicyFile loads only policy rules that match the filter from file.
*/
private void loadFilteredPolicyFile(Model model, Filter filter, Helper.loadPolicyLineHandler handler) throws CasbinAdapterException {
- try (Statement stmt = conn.createStatement()) {
+ Failsafe.with(retryPolicy).run(ctx -> {
+ if (ctx.isRetry()) {
+ retry(ctx);
+ }
+ Statement stmt = conn.createStatement();
ResultSet rSet = stmt.executeQuery("SELECT * FROM casbin_rule");
ResultSetMetaData rData = rSet.getMetaData();
while (rSet.next()) {
@@ -127,10 +132,7 @@ private void loadFilteredPolicyFile(Model model, Filter filter, Helper.loadPolic
loadPolicyLine(line, model);
}
rSet.close();
- } catch (SQLException e) {
- e.printStackTrace();
- throw new Error(e);
- }
+ });
}
/**
diff --git a/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java b/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java
index 1f33abd..61415c8 100644
--- a/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java
+++ b/src/main/java/org/casbin/adapter/JDBCBaseAdapter.java
@@ -14,8 +14,10 @@
package org.casbin.adapter;
+import dev.failsafe.ExecutionContext;
+import dev.failsafe.Failsafe;
+import dev.failsafe.RetryPolicy;
import org.apache.commons.collections.CollectionUtils;
-import org.casbin.jcasbin.main.CoreEnforcer;
import org.casbin.jcasbin.model.Assertion;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.Adapter;
@@ -23,6 +25,7 @@
import javax.sql.DataSource;
import java.sql.*;
+import java.time.Duration;
import java.util.*;
class CasbinRule {
@@ -45,9 +48,11 @@ public String[] toStringArray() {
* It can load policy from JDBC supported database or save policy to it.
*/
abstract class JDBCBaseAdapter implements Adapter {
- private DataSource dataSource;
+ protected static final int _DEFAULT_CONNECTION_TRIES = 3;
+ protected DataSource dataSource;
private final int batchSize = 1000;
protected Connection conn;
+ protected RetryPolicy