Skip to content

Commit

Permalink
Fix mysql connection timeout issue
Browse files Browse the repository at this point in the history
  • Loading branch information
junghyun397 committed Jan 31, 2021
1 parent 0f4f077 commit 2e1e697
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/main/java/do1phin/mine2021/data/db/MysqlDatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public boolean connect() {
@SuppressWarnings("SqlResolve")
@Override
public void initDatabase() {
Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(this::validateConnection, 1, 1, TimeUnit.MINUTES);

try {
final Statement stmt = this.connection.createStatement();

Expand Down Expand Up @@ -70,17 +73,14 @@ public void initDatabase() {
} catch (SQLException e) {
e.printStackTrace();
}

Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(this::validateConnection, 1, 1, TimeUnit.MINUTES);
}

@SuppressWarnings("SqlResolve")
@Override
public int getNextAutoIncrement() {
try {
final PreparedStatement pstmt = this.connection.prepareStatement("SHOW TABLE STATUS LIKE 'user_info'");
ResultSet resultSet = pstmt.executeQuery();
final ResultSet resultSet = pstmt.executeQuery();

if (resultSet.next()) return resultSet.getInt("Auto_increment");
} catch (SQLException e) {
Expand All @@ -94,7 +94,8 @@ private void validateConnection() {
try {
final PreparedStatement pstmt = this.connection.prepareStatement("SELECT 1");
if (pstmt.executeQuery().next()) return;
} catch (SQLException ignored) {}
} catch (Exception ignored) {}
this.disconnect();
this.connect();
}

Expand Down

0 comments on commit 2e1e697

Please sign in to comment.