Skip to content

Commit

Permalink
Reconnect when connection times out
Browse files Browse the repository at this point in the history
- Closes #5
  • Loading branch information
BrainStone committed Apr 24, 2017
1 parent ec3d3e5 commit 00ae88d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/world/jnc/invsync/util/DatabaseConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ private void connect(StringBuilder connectionURL) throws SQLException {
connection = getDataSource(connectionURLStr).getConnection();
}

private void reconnect() throws SQLException {
InventorySync.getLogger()
.debug("Reconnecting to: " + connectionURLStr.toString().replaceFirst(":[^:]*@", ":*****@"));

connection = getDataSource(connectionURLStr).getConnection();
}

public boolean isConnectionActive() {
try {
return (connection != null) && connection.isValid(0);
Expand All @@ -92,6 +99,12 @@ public boolean isConnectionActive() {
}
}

public void verifyConnection() throws SQLException {
if (!isConnectionActive()) {
reconnect();
}
}

public Statement getStatement() throws SQLException {
if (!isConnectionActive())
throw new SQLException("MySQL-connection is not active!");
Expand Down

0 comments on commit 00ae88d

Please sign in to comment.