Skip to content

Commit

Permalink
Introduce retry to connect to db server after time-out (#113)
Browse files Browse the repository at this point in the history
* Introduce retry count

* Hide data source details

* Add javadocs

* Bump mariadb-java-client 2.7.3 => 3.0.6
  • Loading branch information
sven1103 authored Sep 26, 2022
1 parent 827f611 commit 5396590
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.3</version>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/groovy/life/qbic/DataSource.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface DataSource {
* execution of the connection object with a try-with-resource statement or call the
* {@link AutoCloseable#close()} method explicitly.</p>
* @return a connection to the datasource
* @throws {@link TimeOutException} when a connection cannot be acquired from the data source
*/
Connection getConnection()
Connection getConnection() throws TimeOutException
}
27 changes: 24 additions & 3 deletions src/main/groovy/life/qbic/QBiCDataSource.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package life.qbic

import groovy.util.logging.Log4j2

import javax.inject.Inject
import javax.inject.Singleton
import java.sql.Connection
import java.sql.SQLTimeoutException

/**
* <b>QBiCDataSource Class</b>
Expand All @@ -12,19 +15,37 @@ import java.sql.Connection
* @since 1.2.1
*/
@Singleton
@Log4j2
class QBiCDataSource implements DataSource {

javax.sql.DataSource source
private final static int MAX_RETRY_COUNT = 3

private final javax.sql.DataSource source

@Inject QBiCDataSource (javax.sql.DataSource source) {
@Inject
QBiCDataSource(javax.sql.DataSource source) {
this.source = source
}

/**
* {@InheritDocs}
*/
@Override
Connection getConnection() {
Connection getConnection() throws TimeOutException {
Connection connection = null
int turn = 1
while (!connection) {
if (turn == MAX_RETRY_COUNT) {
throw new TimeOutException("Maximum number of tries reached ($MAX_RETRY_COUNT), connection to database server timed out repeatedly.")
}
try {
connection = this.source.getConnection()
} catch (SQLTimeoutException e) {
log.error("Turn $turn to get connection failed.")
log.error("Connection to database server timed out.", e)
}
turn++;
}
return this.source.connection
}
}
23 changes: 23 additions & 0 deletions src/main/groovy/life/qbic/TimeOutException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package life.qbic;

/**
* Timeout Exception
* <p>
* Is thrown when connections to a data source cannot be established.
*
* @since 2.2.3
*/
public class TimeOutException extends RuntimeException {

public TimeOutException() {
}

public TimeOutException(String message) {
super(message);
}

public TimeOutException(String message, Throwable cause) {
super(message, cause);
}

}
13 changes: 11 additions & 2 deletions src/main/groovy/life/qbic/db/MariaDBManager.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package life.qbic.db
import groovy.sql.GroovyRowResult
import groovy.sql.Sql
import groovy.util.logging.Log4j2
import life.qbic.DataSource
import life.qbic.QBiCDataSource
import life.qbic.TimeOutException
import life.qbic.api.rest.v2.samples.SampleStatusDto
import life.qbic.datamodel.identifiers.SampleCodeFunctions
import life.qbic.datamodel.people.Address
Expand All @@ -27,7 +29,6 @@ import org.codehaus.groovy.runtime.DefaultGroovyMethods

import javax.inject.Inject
import javax.inject.Singleton
import javax.sql.DataSource
import java.sql.Connection
import java.sql.SQLException
import java.sql.Timestamp
Expand All @@ -49,7 +50,15 @@ class MariaDBManager implements IQueryService, INotificationService, SampleEvent

@Inject
MariaDBManager(QBiCDataSource dataSource) {
this.dataSource = dataSource.getSource()
this.dataSource = dataSource
}

private Connection getConnection() throws UnrecoverableException {
try {
return dataSource.getConnection()
} catch (TimeOutException exception) {
throw new UnrecoverableException("Connection to data source timed out", exception)
}
}

void addNewLocation(String sampleId, Location location) throws IllegalArgumentException {
Expand Down

0 comments on commit 5396590

Please sign in to comment.