@@ -39,6 +39,9 @@ public class ConfigurableRetryLogic {
3939 private static final String FORWARD_SLASH = "/" ;
4040 private static final String EQUALS_SIGN = "=" ;
4141 private static final String RETRY_EXEC = "retryExec" ;
42+ private static final String RETRY_CONN = "retryConn" ;
43+ private static final String STATEMENT = "statement" ;
44+ private static boolean replaceFlag = false ; // Are we replacing the list of transient errors?
4245 /**
4346 * The time the properties file was last modified.
4447 */
@@ -52,14 +55,23 @@ public class ConfigurableRetryLogic {
5255 */
5356 private static final AtomicReference <String > lastQuery = new AtomicReference <>("" );
5457 /**
55- * The previously read rules from the connection string.
58+ * The previously read statement rules from the connection string.
5659 */
57- private static final AtomicReference <String > prevRulesFromConnectionString = new AtomicReference <>("" );
60+ private static final AtomicReference <String > prevStmtRulesFromConnString = new AtomicReference <>("" );
61+ /**
62+ * The previously read connection rules from the connection string.
63+ */
64+ private static final AtomicReference <String > prevConnRulesFromConnString = new AtomicReference <>("" );
5865 /**
5966 * The list of statement retry rules.
6067 */
6168 private static final AtomicReference <HashMap <Integer , ConfigurableRetryRule >> stmtRules = new AtomicReference <>(
6269 new HashMap <>());
70+ /**
71+ * The list of connection retry rules.
72+ */
73+ private static final AtomicReference <HashMap <Integer , ConfigurableRetryRule >> connRules = new AtomicReference <>(
74+ new HashMap <>());
6375 private static ConfigurableRetryLogic singleInstance ;
6476
6577 /**
@@ -70,7 +82,8 @@ public class ConfigurableRetryLogic {
7082 */
7183 private ConfigurableRetryLogic () throws SQLServerException {
7284 timeLastRead .compareAndSet (0 , new Date ().getTime ());
73- setUpRules (null );
85+ setUpStatementRules (null );
86+ setUpConnectionRules (null );
7487 }
7588
7689 /**
@@ -102,7 +115,8 @@ public static ConfigurableRetryLogic getInstance() throws SQLServerException {
102115
103116 /**
104117 * If it has been INTERVAL_BETWEEN_READS_IN_MS (30 secs) since last read, see if we last did a file read, if so
105- * only reread if the file has been modified. If no file read, set up rules using the prev. connection string rules.
118+ * only reread if the file has been modified. If no file read, set up rules using the previous connection
119+ * string (statement and connection) rules
106120 *
107121 * @throws SQLServerException
108122 * when an exception occurs
@@ -116,25 +130,40 @@ private static void refreshRuleSet() throws SQLServerException {
116130 // If timeLastModified is set, we previously read from file, so we setUpRules also reading from file
117131 File f = new File (getCurrentClassPath ());
118132 if (f .lastModified () != timeLastModified .get ()) {
119- setUpRules (null );
133+ setUpStatementRules (null );
134+ setUpConnectionRules (null );
120135 }
121136 } else {
122- setUpRules (prevRulesFromConnectionString .get ());
137+ setUpStatementRules (prevStmtRulesFromConnString .get ());
138+ setUpConnectionRules (prevConnRulesFromConnString .get ());
123139 }
124140 }
125141 }
126142
127143 /**
128- * Sets rules given from connection string.
144+ * Sets statement rules given from connection string.
145+ *
146+ * @param newRules
147+ * the new rules to use
148+ * @throws SQLServerException
149+ * when an exception occurs
150+ */
151+ void setStatementRulesFromConnectionString (String newRules ) throws SQLServerException {
152+ prevStmtRulesFromConnString .set (newRules );
153+ setUpStatementRules (prevStmtRulesFromConnString .get ());
154+ }
155+
156+ /**
157+ * Sets connection rules given from connection string.
129158 *
130159 * @param newRules
131160 * the new rules to use
132161 * @throws SQLServerException
133162 * when an exception occurs
134163 */
135- void setFromConnectionString (String newRules ) throws SQLServerException {
136- prevRulesFromConnectionString .set (newRules );
137- setUpRules ( prevRulesFromConnectionString .get ());
164+ void setConnectionRulesFromConnectionString (String newRules ) throws SQLServerException {
165+ prevConnRulesFromConnString .set (newRules );
166+ setUpConnectionRules ( prevConnRulesFromConnString .get ());
138167 }
139168
140169 /**
@@ -164,19 +193,34 @@ String getLastQuery() {
164193 * @throws SQLServerException
165194 * if an exception occurs
166195 */
167- private static void setUpRules (String cxnStrRules ) throws SQLServerException {
196+ private static void setUpStatementRules (String cxnStrRules ) throws SQLServerException {
168197 LinkedList <String > temp ;
169198
170199 stmtRules .set (new HashMap <>());
171200 lastQuery .set ("" );
172201
173202 if (cxnStrRules == null || cxnStrRules .isEmpty ()) {
174- temp = readFromFile ();
203+ temp = readFromFile (RETRY_EXEC );
175204 } else {
176205 temp = new LinkedList <>();
177206 Collections .addAll (temp , cxnStrRules .split (SEMI_COLON ));
178207 }
179- createRules (temp );
208+ createStatementRules (temp );
209+ }
210+
211+ private static void setUpConnectionRules (String cxnStrRules ) throws SQLServerException {
212+ LinkedList <String > temp ;
213+
214+ connRules .set (new HashMap <>());
215+ lastQuery .set ("" );
216+
217+ if (cxnStrRules == null || cxnStrRules .isEmpty ()) {
218+ temp = readFromFile (RETRY_CONN );
219+ } else {
220+ temp = new LinkedList <>();
221+ Collections .addAll (temp , cxnStrRules .split (SEMI_COLON ));
222+ }
223+ createConnectionRules (temp );
180224 }
181225
182226 /**
@@ -187,7 +231,7 @@ private static void setUpRules(String cxnStrRules) throws SQLServerException {
187231 * @throws SQLServerException
188232 * if unable to create rules from the inputted list
189233 */
190- private static void createRules (LinkedList <String > listOfRules ) throws SQLServerException {
234+ private static void createStatementRules (LinkedList <String > listOfRules ) throws SQLServerException {
191235 stmtRules .set (new HashMap <>());
192236
193237 for (String potentialRule : listOfRules ) {
@@ -206,6 +250,29 @@ private static void createRules(LinkedList<String> listOfRules) throws SQLServer
206250 }
207251 }
208252
253+ private static void createConnectionRules (LinkedList <String > listOfRules ) throws SQLServerException {
254+ connRules .set (new HashMap <>());
255+ replaceFlag = false ;
256+
257+ for (String potentialRule : listOfRules ) {
258+ ConfigurableRetryRule rule = new ConfigurableRetryRule (potentialRule );
259+ if (rule .replaceExisting ) {
260+ replaceFlag = true ;
261+ }
262+
263+ if (rule .getError ().contains (COMMA )) {
264+ String [] arr = rule .getError ().split (COMMA );
265+
266+ for (String retryError : arr ) {
267+ ConfigurableRetryRule splitRule = new ConfigurableRetryRule (retryError , rule );
268+ connRules .get ().put (Integer .parseInt (splitRule .getError ()), splitRule );
269+ }
270+ } else {
271+ connRules .get ().put (Integer .parseInt (rule .getError ()), rule );
272+ }
273+ }
274+ }
275+
209276 /**
210277 * Gets the current class path (for use in file reading).
211278 *
@@ -241,7 +308,7 @@ private static String getCurrentClassPath() throws SQLServerException {
241308 * @throws SQLServerException
242309 * if unable to read from the file
243310 */
244- private static LinkedList <String > readFromFile () throws SQLServerException {
311+ private static LinkedList <String > readFromFile (String connectionStringProperty ) throws SQLServerException {
245312 String filePath = getCurrentClassPath ();
246313 LinkedList <String > list = new LinkedList <>();
247314
@@ -250,7 +317,7 @@ private static LinkedList<String> readFromFile() throws SQLServerException {
250317 try (BufferedReader buffer = new BufferedReader (new FileReader (f ))) {
251318 String readLine ;
252319 while ((readLine = buffer .readLine ()) != null ) {
253- if (readLine .startsWith (RETRY_EXEC )) {
320+ if (readLine .startsWith (connectionStringProperty )) { // Either "retryExec" or "retryConn"
254321 String value = readLine .split (EQUALS_SIGN )[1 ];
255322 Collections .addAll (list , value .split (SEMI_COLON ));
256323 }
@@ -280,13 +347,25 @@ private static LinkedList<String> readFromFile() throws SQLServerException {
280347 * @throws SQLServerException
281348 * when an exception occurs
282349 */
283- ConfigurableRetryRule searchRuleSet (int ruleToSearchFor ) throws SQLServerException {
350+ ConfigurableRetryRule searchRuleSet (int ruleToSearchFor , String ruleSet ) throws SQLServerException {
284351 refreshRuleSet ();
285- for (Map .Entry <Integer , ConfigurableRetryRule > entry : stmtRules .get ().entrySet ()) {
286- if (entry .getKey () == ruleToSearchFor ) {
287- return entry .getValue ();
352+ if (ruleSet .equals (STATEMENT )) {
353+ for (Map .Entry <Integer , ConfigurableRetryRule > entry : stmtRules .get ().entrySet ()) {
354+ if (entry .getKey () == ruleToSearchFor ) {
355+ return entry .getValue ();
356+ }
357+ }
358+ } else {
359+ for (Map .Entry <Integer , ConfigurableRetryRule > entry : connRules .get ().entrySet ()) {
360+ if (entry .getKey () == ruleToSearchFor ) {
361+ return entry .getValue ();
362+ }
288363 }
289364 }
290365 return null ;
291366 }
367+
368+ boolean getReplaceFlag () {
369+ return replaceFlag ;
370+ }
292371}
0 commit comments