From 235b90098474ec3624122cd1458344676f041ef8 Mon Sep 17 00:00:00 2001 From: Loredana Liccardo Date: Tue, 2 Sep 2014 13:24:20 +0000 Subject: [PATCH] - hibernate bug fixed and related to a join tables that have some columns with identical name. Bug resolved using JDBC. GetConnection method changed in the SubmitQuery algorithm to create the connection with JDBC. - timer added in the SubmitQuery algorithm that stops the query execution after 30 minutes. git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManagerAlgorithms@99396 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../algorithms/drmalgorithms/SubmitQuery.java | 73 ++++++++++++++----- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java index 851b2a2..8feca9c 100644 --- a/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java +++ b/src/main/java/org/gcube/dataaccess/algorithms/drmalgorithms/SubmitQuery.java @@ -2,13 +2,18 @@ package org.gcube.dataaccess.algorithms.drmalgorithms; import java.io.File; import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; import java.util.UUID; +import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; import org.gcube.dataaccess.databases.access.DatabasesDiscoverer; @@ -33,14 +38,15 @@ import com.adventnet.swissqlapi.sql.parser.ParseException; * map. */ public class SubmitQuery extends StandardLocalExternalAlgorithm { - + + static long maximum_execution_time = 30*60*1000; //time to stop execution query private LinkedHashMap map = new LinkedHashMap(); // object that allows to manage some operations on a database private DatabaseManagement mgt; // variable that keeps track of the database's type private String driverInfo; - private SessionFactory sf; + private Connection dbconnection; // database's parameters specified by the user private String resourceName = null; private String databaseName = null; @@ -59,6 +65,16 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // variable used to filter the disallowed queries private boolean NotAllowedQuery = false; + //class for the timer to stop execution query + private class ExecutionStopper extends TimerTask { + @Override + public void run() { + AnalysisLogger.getLogger().debug("ExecutionStopper: Stopping execution"); + shutdown(); + } + } + + @Override public void init() throws Exception { @@ -92,19 +108,22 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { HibernateException { AnalysisLogger.getLogger().debug("In SubmitQuery->Processing"); - + + Timer stopper = new Timer(); + stopper.schedule(new ExecutionStopper(),maximum_execution_time); + try { // retrieve information List Info = retrieveInfo(); // create the connection - sf = getConnection(Info); + dbconnection = getConnection(Info); // submit a query map = submitQuery(); // close the connection - sf.close(); + dbconnection.close(); } catch (HibernateException h) { @@ -162,8 +181,18 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { throw e4; } finally { - if (sf.isClosed() == false) { - mgt.closeConnection(); + if (dbconnection!=null && dbconnection.isClosed() == false) { + dbconnection.close(); + } + //remove the timer if the execution query has already terminated + if (stopper!=null){ + try{ + stopper.cancel(); + stopper.purge(); + AnalysisLogger.getLogger().debug("In SubmitQuery-> Execution stopper terminated"); + }catch(Exception e){ + AnalysisLogger.getLogger().debug("In SubmitQuery-> Could not stop execution stopper "+e.getMessage() ); + } } } } @@ -265,9 +294,12 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { public void shutdown() { AnalysisLogger.getLogger().debug("In SubmitQuery->Shutdown"); - - if (sf.isClosed() == false) { - mgt.closeConnection(); + try{ + if (dbconnection!=null && dbconnection.isClosed() == false) { + dbconnection.close(); + } + }catch(Exception e){ + AnalysisLogger.getLogger().debug("In SubmitQuery->Unable to close connection "+e.getMessage()); } } @@ -363,7 +395,7 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { } // create the database's connection - private SessionFactory getConnection(List Info) throws IOException { + private Connection getConnection(List Info) throws Exception { // create the connection Iterator iterator = Info.iterator(); @@ -375,12 +407,15 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { String DatabaseURL = iterator.next(); String DatabaseName = iterator.next(); - SessionFactory sf = mgt.createConnection(DatabaseUserName, - DatabasePassword, DatabaseDriver, DatabaseDialect, DatabaseURL, - DatabaseName); - AnalysisLogger.getLogger().debug( - "In SubmitQuery->database " + DatabaseName + ": connected"); - return sf; + // Load the database driver + Class.forName(DatabaseDriver) ; + // Get a connection to the database + Connection conn = DriverManager.getConnection(DatabaseURL,DatabaseUserName,DatabasePassword) ; + if (conn!=null){ + AnalysisLogger.getLogger().debug( + "In SubmitQuery->database " + DatabaseName + ": connected"); + } + return conn; } // Method that allows to submit a query @@ -504,7 +539,7 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // sf, config.getPersistencePath()); result = mgt - .submitQuery(query, sf, config.getPersistencePath()); + .submitQuery(query, dbconnection, config.getPersistencePath()); } if (driverInfo.toLowerCase().contains("mysql")) { @@ -515,7 +550,7 @@ public class SubmitQuery extends StandardLocalExternalAlgorithm { // sf, config.getPersistencePath()); result = mgt - .submitQuery(query, sf, config.getPersistencePath()); + .submitQuery(query, dbconnection, config.getPersistencePath()); } if (result == null) {