- Added reconnection attempts to DB on DB connection failure [#26322]
This commit is contained in:
parent
9e0a2582e2
commit
b8d54fea0b
|
@ -2,6 +2,7 @@
|
|||
|
||||
## [v1.1.3]
|
||||
- Added apply regex business logic [#26322]
|
||||
- Added reconnection attempts to DB on DB connection failure [#26322]
|
||||
|
||||
## [v1.1.2]
|
||||
- Using parent version range [#25572]
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.gcube.application.cms.sdi.plugins;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
@ -41,6 +43,7 @@ import org.gcube.application.geoportal.common.model.document.identification.Spat
|
|||
import org.gcube.application.geoportal.common.model.plugins.IndexerPluginDescriptor;
|
||||
import org.gcube.application.geoportal.common.model.plugins.PluginDescriptor;
|
||||
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
|
||||
import org.gcube.application.geoportal.common.model.rest.DatabaseConnection;
|
||||
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
|
||||
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
|
||||
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder;
|
||||
|
@ -66,6 +69,8 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Slf4j
|
||||
public class SDIIndexerPlugin extends SDIAbstractPlugin implements IndexerPluginInterface {
|
||||
|
||||
private static final int CONNECTION_TIMEOUT = 30;
|
||||
|
||||
static final PluginDescriptor DESCRIPTOR = new PluginDescriptor(Constants.INDEXER_PLUGIN_ID,
|
||||
IndexerPluginDescriptor.INDEXER);
|
||||
|
||||
|
@ -328,7 +333,7 @@ public class SDIIndexerPlugin extends SDIAbstractPlugin implements IndexerPlugin
|
|||
|
||||
if (applyRegex != null) {
|
||||
try {
|
||||
String valueString = toSetValue==null?"":toSetValue.toString();
|
||||
String valueString = toSetValue == null ? "" : toSetValue.toString();
|
||||
String type = applyRegex.getType();
|
||||
REGEX_TYPES theRegexType = ApplyRegex.REGEX_TYPES.valueOf(type);
|
||||
Pattern p = Pattern.compile(applyRegex.getRegex());
|
||||
|
@ -438,6 +443,10 @@ public class SDIIndexerPlugin extends SDIAbstractPlugin implements IndexerPlugin
|
|||
// TODO CACHE
|
||||
private PostgisIndexer getIndexer(UseCaseDescriptor ucd, Document params)
|
||||
throws ConfigurationException, SQLException, InvalidProfileException, SDIInteractionException {
|
||||
|
||||
//Added by Francesco
|
||||
databaseConnectionValidator();
|
||||
|
||||
PostgisIndexer indexer = new PostgisIndexer(sdiCache.getObject(), ucd, postgisCache.getObject());
|
||||
|
||||
List<MappingObject> mappingObjects = getMappings(ucd);
|
||||
|
@ -449,4 +458,48 @@ public class SDIIndexerPlugin extends SDIAbstractPlugin implements IndexerPlugin
|
|||
return indexer;
|
||||
}
|
||||
|
||||
private boolean checkDatabaseConnection() {
|
||||
|
||||
DatabaseConnection connectionParameters = null;
|
||||
try {
|
||||
connectionParameters = postgisCache.getObject();
|
||||
// Getting connection
|
||||
Connection connection = DriverManager.getConnection(connectionParameters.getUrl(),
|
||||
connectionParameters.getUser(), connectionParameters.getPwd());
|
||||
// Check if the connection is valid (timeout 30 seconds)
|
||||
if (connection != null && connection.isValid(CONNECTION_TIMEOUT)) {
|
||||
log.debug("Connection to DB {} is OK!", connectionParameters.getUrl());
|
||||
return true;
|
||||
} else {
|
||||
log.debug("Connection to DB {} is KO!", connectionParameters.getUrl());
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error on connecting to DB: " + connectionParameters, e);
|
||||
return false;
|
||||
} catch (ConfigurationException e1) {
|
||||
log.warn("Error on reading connection configuration: " + connectionParameters, e1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void databaseConnectionValidator() {
|
||||
|
||||
int maxRetry = 5;
|
||||
int attempt = 0;
|
||||
while (!checkDatabaseConnection() && attempt <= maxRetry) {
|
||||
attempt++;
|
||||
log.info("Attempt number: {}", attempt);
|
||||
log.info("Disposing the PostGis Cache");
|
||||
postgisCache.shutdown();
|
||||
log.info("Loading the PostGis Cache for context");
|
||||
try {
|
||||
postgisCache.getObject();
|
||||
} catch (ConfigurationException e) {
|
||||
log.warn("DtabaseConnectionValidatorError on reading connection configuration: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue