From 279537b9b489527d1dfac587785046db68bf6429 Mon Sep 17 00:00:00 2001 From: Marco Zanon Date: Mon, 10 Jul 2017 12:48:03 +0000 Subject: [PATCH] Implemented connection initialization on timeouts. --- .../macaco/database/MDatabaseConnection.java | 18 +++++++++++++++--- .../marcozanon/macaco/database/MSqlTable.java | 12 ++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java b/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java index 3445def..027ae74 100644 --- a/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java +++ b/src/java/com/marcozanon/macaco/database/MDatabaseConnection.java @@ -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()); } - public MSqlStatementResults executePreparedStatement(String statement, LinkedList parameters) throws MSqlStatementException { + public MSqlStatementResults executePreparedStatement(String statement, LinkedList 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> resultList = results.getRecords(); // diff --git a/src/java/com/marcozanon/macaco/database/MSqlTable.java b/src/java/com/marcozanon/macaco/database/MSqlTable.java index 9cbefe3..fdc9441 100644 --- a/src/java/com/marcozanon/macaco/database/MSqlTable.java +++ b/src/java/com/marcozanon/macaco/database/MSqlTable.java @@ -58,7 +58,7 @@ public class MSqlTable extends MObject { /* Statements. */ - protected MSqlStatementResults insertRecord(LinkedHashMap map) throws MSqlStatementException { + protected MSqlStatementResults insertRecord(LinkedHashMap 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 map, Object id) throws MSqlStatementException { + protected MSqlStatementResults updateRecord(LinkedHashMap 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 map, Object id) throws MSqlStatementException { + public MSqlStatementResults setRecord(LinkedHashMap 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."); } -- 2.30.2