Fixed a bug which prevented idle connections to work properly.
authorMarco Zanon <info@marcozanon.com>
Thu, 16 Nov 2017 23:12:55 +0000 (23:12 +0000)
committerMarco Zanon <info@marcozanon.com>
Thu, 16 Nov 2017 23:12:55 +0000 (23:12 +0000)
6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java

index 5c05e2a575df4a53b742cee92a6ef832473f4def..07f04c62fbdbbce2aa7c9a0f0ac7ab3cf064bc49 100644 (file)
@@ -94,6 +94,19 @@ public class MDatabaseConnection extends MObject {
         }
     }
 
+    protected void check() {
+        try {
+            this.getConnection().prepareStatement("/* ping */ SELECT 1", PreparedStatement.RETURN_GENERATED_KEYS);
+        }
+        catch (SQLException exception) {
+            try {
+                this.initialize();
+            }
+            catch (MDatabaseConnectionFailureException exception2) { // should not happen
+            }
+        }
+    }
+
     public void close() throws MDatabaseConnectionFailureException {
         try {
             this.getConnection().close();
@@ -175,6 +188,9 @@ public class MDatabaseConnection extends MObject {
             throw new MSqlTransactionException("Nested transactions not allowed.");
         }
         try {
+            // Check the connection.
+            this.check();
+            // Start the transaction.
             this.getConnection().setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
             this.getConnection().setAutoCommit(false);
             this.setTransactionStatus(MDatabaseConnection.TransactionStatus.SUCCESSFUL);
@@ -190,6 +206,11 @@ public class MDatabaseConnection extends MObject {
             throw new MSqlTransactionException("Transaction not started.");
         }
         try {
+/*
+            // Check the connection.
+            this.check();
+*/
+            // Cancel the transaction.
             this.getConnection().rollback();
             this.getConnection().setAutoCommit(true);
             this.setTransactionStatus(MDatabaseConnection.TransactionStatus.CLOSED);
@@ -205,6 +226,11 @@ public class MDatabaseConnection extends MObject {
         switch (this.getTransactionStatus()) {
             case SUCCESSFUL:
                 try {
+/*
+                    // Check the connection.
+                    this.check();
+*/
+                    // Commit the transaction.
                     this.getConnection().commit();
                     this.getConnection().setAutoCommit(true);
                     this.setTransactionStatus(MDatabaseConnection.TransactionStatus.CLOSED);
@@ -245,12 +271,7 @@ public class MDatabaseConnection extends MObject {
         PreparedStatement preparedStatement = null;
         try {
             // Check the connection.
-            try {
-                preparedStatement = this.getConnection().prepareStatement("/* ping */ SELECT 1", PreparedStatement.RETURN_GENERATED_KEYS);
-            }
-            catch (SQLException exception) {
-                this.initialize();
-            }
+            this.check();
             // Prepare the statement.
             preparedStatement = this.getConnection().prepareStatement(statement, PreparedStatement.RETURN_GENERATED_KEYS);
             for (int p = 0; parameters.size() > p; p++) {