From 5396590240cf8f3a0fb1f95264b46a7b46742acc Mon Sep 17 00:00:00 2001
From: Sven F <9976560+sven1103@users.noreply.github.com>
Date: Mon, 26 Sep 2022 14:16:35 +0200
Subject: [PATCH] Introduce retry to connect to db server after time-out (#113)
* Introduce retry count
* Hide data source details
* Add javadocs
* Bump mariadb-java-client 2.7.3 => 3.0.6
---
pom.xml | 2 +-
src/main/groovy/life/qbic/DataSource.groovy | 3 ++-
.../groovy/life/qbic/QBiCDataSource.groovy | 27 ++++++++++++++++---
.../groovy/life/qbic/TimeOutException.java | 23 ++++++++++++++++
.../groovy/life/qbic/db/MariaDBManager.groovy | 13 +++++++--
5 files changed, 61 insertions(+), 7 deletions(-)
create mode 100644 src/main/groovy/life/qbic/TimeOutException.java
diff --git a/pom.xml b/pom.xml
index ae5c57c..233bb51 100644
--- a/pom.xml
+++ b/pom.xml
@@ -217,7 +217,7 @@
+ * 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); + } + +} diff --git a/src/main/groovy/life/qbic/db/MariaDBManager.groovy b/src/main/groovy/life/qbic/db/MariaDBManager.groovy index c9f70e3..dab0383 100644 --- a/src/main/groovy/life/qbic/db/MariaDBManager.groovy +++ b/src/main/groovy/life/qbic/db/MariaDBManager.groovy @@ -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 @@ -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 @@ -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 {