From d98c0e9596f6c2746ffd3fc8eec074bc4cfdbc73 Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Thu, 16 Nov 2017 23:12:55 +0000 Subject: [PATCH] Fixed a bug which prevented idle connections to work properly. --- .../macaco/database/MDatabaseConnection.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java b/6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java index 5c05e2a..07f04c6 100644 --- a/6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java +++ b/6.x/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java @@ -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++) { -- 2.30.2