Implemented connection initialization on timeouts.
authorMarco Zanon <info@marcozanon.com>
Mon, 10 Jul 2017 12:48:03 +0000 (12:48 +0000)
committerMarco Zanon <info@marcozanon.com>
Mon, 10 Jul 2017 12:48:03 +0000 (12:48 +0000)
5.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java
5.x/src/java/com/marcozanon/macaco/database/MSqlTable.java

index 3445deff12f0b54099eef2f5be8030919a60f849..027ae7424f831bdb4ce71f4bfe690ffe79e3ae4c 100644 (file)
@@ -70,6 +70,11 @@ public class MDatabaseConnection extends MObject {
         catch (InstantiationException exception) {
             throw new MDatabaseConnectionFailureException("Could not load driver.", exception);
         }
+        // Initialize the connection.
+        this.initialize();
+    }
+
+    public void initialize() throws MDatabaseConnectionFailureException {
         // Establish a connection.
         try {
             this.connection = DriverManager.getConnection(this.getUrl(), this.getUsername(), this.getPassword());
@@ -212,11 +217,11 @@ public class MDatabaseConnection extends MObject {
 
     /* Statements. */
 
-    public MSqlStatementResults executePreparedStatement(String statement) throws MSqlStatementException {
+    public MSqlStatementResults executePreparedStatement(String statement) throws MDatabaseConnectionFailureException, MSqlStatementException {
         return this.executePreparedStatement(statement, new LinkedList<Object>());
     }
 
-    public MSqlStatementResults executePreparedStatement(String statement, LinkedList<Object> parameters) throws MSqlStatementException {
+    public MSqlStatementResults executePreparedStatement(String statement, LinkedList<Object> parameters) throws MDatabaseConnectionFailureException, MSqlStatementException {
         if (MText.isBlank(statement)) {
             throw new IllegalArgumentException("Invalid 'statement': null or empty.");
         }
@@ -227,6 +232,13 @@ public class MDatabaseConnection extends MObject {
         MSqlStatementResults results = null;
         PreparedStatement preparedStatement = null;
         try {
+            // Check the connection.
+            try {
+                preparedStatement = this.getConnection().prepareStatement("/* ping */ SELECT 1", PreparedStatement.RETURN_GENERATED_KEYS);
+            }
+            catch (SQLException exception) {
+                this.initialize();
+            }
             // Prepare the statement.
             preparedStatement = this.getConnection().prepareStatement(statement, PreparedStatement.RETURN_GENERATED_KEYS);
             for (int p = 0; parameters.size() > p; p++) {
@@ -271,7 +283,7 @@ public class MDatabaseConnection extends MObject {
 
     /* Engine version. */
 
-    public String getEngineVersion() throws MSqlStatementException {
+    public String getEngineVersion() throws MDatabaseConnectionFailureException, MSqlStatementException {
         MSqlStatementResults results = this.executePreparedStatement("SELECT VERSION()");
         LinkedList<LinkedHashMap<String, Object>> resultList = results.getRecords();
         //
index 9cbefe3aae971fd708755d380654fb44901ff4dc..fdc9441b6578e8a93d8d564c28ce98441531c76f 100644 (file)
@@ -58,7 +58,7 @@ public class MSqlTable extends MObject {
 
     /* Statements. */
 
-    protected MSqlStatementResults insertRecord(LinkedHashMap<String, Object> map) throws MSqlStatementException {
+    protected MSqlStatementResults insertRecord(LinkedHashMap<String, Object> map) throws MDatabaseConnectionFailureException, MSqlStatementException {
         if (null == map) {
             throw new IllegalArgumentException("Invalid 'map': null.");
         }
@@ -79,7 +79,7 @@ public class MSqlTable extends MObject {
                                                                      p);
     }
 
-    protected MSqlStatementResults updateRecord(LinkedHashMap<String, Object> map, Object id) throws MSqlStatementException {
+    protected MSqlStatementResults updateRecord(LinkedHashMap<String, Object> map, Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
         if (null == map) {
             throw new IllegalArgumentException("Invalid 'map': null.");
         }
@@ -101,7 +101,7 @@ public class MSqlTable extends MObject {
                                                                      p);
     }
 
-    public MSqlStatementResults setRecord(LinkedHashMap<String, Object> map, Object id) throws MSqlStatementException {
+    public MSqlStatementResults setRecord(LinkedHashMap<String, Object> map, Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
         if (null == id) {
             return this.insertRecord(map);
         }
@@ -110,7 +110,7 @@ public class MSqlTable extends MObject {
         }
     }
 
-    public MSqlStatementResults getRecord(Object id) throws MSqlStatementException {
+    public MSqlStatementResults getRecord(Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
         if (null == id) {
             throw new IllegalArgumentException("Invalid 'id': null.");
         }
@@ -123,7 +123,7 @@ public class MSqlTable extends MObject {
                                                                      p);
     }
 
-    public MSqlStatementResults deleteRecord(Object id) throws MSqlStatementException {
+    public MSqlStatementResults deleteRecord(Object id) throws MDatabaseConnectionFailureException, MSqlStatementException {
         if (null == id) {
             throw new IllegalArgumentException("Invalid 'id': null.");
         }
@@ -135,7 +135,7 @@ public class MSqlTable extends MObject {
                                                                      p);
     }
 
-    public MSqlStatementResults deleteRecords(LinkedHashSet idSet) throws MSqlStatementException {
+    public MSqlStatementResults deleteRecords(LinkedHashSet idSet) throws MDatabaseConnectionFailureException, MSqlStatementException {
         if (null == idSet) {
             throw new IllegalArgumentException("Invalid 'idSet': null.");
         }