From: Marco Zanon Date: Thu, 16 Nov 2017 23:12:55 +0000 (+0000) Subject: Fixed a bug which prevented idle connections to work properly. X-Git-Tag: 6.2.2~1 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=8be7d0d06612a0c033dac98fe59275ff3407677b;p=Macaco Fixed a bug which prevented idle connections to work properly. --- diff --git a/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java b/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java index 5c05e2a..07f04c6 100644 --- a/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java +++ b/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++) {