From a2ea87687d99c161866e2e4c9e459a3a617be3ad Mon Sep 17 00:00:00 2001 From: Loredana Liccardo Date: Fri, 1 Aug 2014 15:21:17 +0000 Subject: [PATCH] - exception management for SQLGrammarException adding a cause message - method removeQueryExecution implemented and commented in class ConnectionManager git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@99191 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../databases/utils/ConnectionManager.java | 98 ++++++++++++------- 1 file changed, 61 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/gcube/dataaccess/databases/utils/ConnectionManager.java b/src/main/java/org/gcube/dataaccess/databases/utils/ConnectionManager.java index 9fd14db..d76b901 100644 --- a/src/main/java/org/gcube/dataaccess/databases/utils/ConnectionManager.java +++ b/src/main/java/org/gcube/dataaccess/databases/utils/ConnectionManager.java @@ -51,7 +51,7 @@ public class ConnectionManager { } catch (Exception e) { System.out.println("ERROR IN DB INITIALIZATION : " + e.getLocalizedMessage()); - e.printStackTrace(); + // e.printStackTrace(); // AnalysisLogger.getLogger().trace(e); } return dbHibConnection; @@ -128,13 +128,12 @@ public class ConnectionManager { try { ss = DBSessionFactory.getCurrentSession(); - //print check added to measure the timeout -// AnalysisLogger.getLogger().debug( -// "In ConnectionManager-> beginTransaction: "); + // print check added to measure the timeout + // AnalysisLogger.getLogger().debug( + // "In ConnectionManager-> beginTransaction: "); ss.beginTransaction(); - - + Query qr = null; // statement to check if the query is a "show create table" @@ -176,6 +175,7 @@ public class ConnectionManager { "In ConnectionManager-> executing query: " + query); qr = ss.createSQLQuery(query); + qr.setResultTransformer(AliasToEntityOrderedMapResultTransformer.INSTANCE); @@ -225,23 +225,28 @@ public class ConnectionManager { // System.out.println(e.getClass().toString()); -// AnalysisLogger -// .getLogger() -// .debug("In ConnectionManager-> ERROR The query could not be executed: Error in retrieving a user defined type. Try to use a store procedure to convert the type"); -// -// throw new Exception( -// "The query could not be executed: Error in retrieving a user defined type. Try to use a store procedure to convert the type"); - + // AnalysisLogger + // .getLogger() + // .debug("In ConnectionManager-> ERROR The query could not be executed: Error in retrieving a user defined type. Try to use a store procedure to convert the type"); + // + // throw new Exception( + // "The query could not be executed: Error in retrieving a user defined type. Try to use a store procedure to convert the type"); + AnalysisLogger.getLogger().debug( - "In ConnectionManager-> " - + e.getLocalizedMessage()); - + "In ConnectionManager-> " + e.getLocalizedMessage()); + throw new Exception(e.getLocalizedMessage()); - + } if (e instanceof org.hibernate.exception.SQLGrammarException) { + String cause = ""; + if (e.getCause() != null) { + cause = e.getCause().getLocalizedMessage(); + // System.out.println(e.getCause().getLocalizedMessage()); + } + // System.out.println(e.getCause().getMessage()); // AnalysisLogger.getLogger().debug("In ConnectionManager-> ERROR The query could not be executed: SQL grammar error in the query"); @@ -249,32 +254,31 @@ public class ConnectionManager { // throw new // Exception("The query could not be executed: SQL grammar error in the query"); -// AnalysisLogger.getLogger().debug( -// "In ConnectionManager-> " -// + e.getCause().getLocalizedMessage()); -// -// throw new Exception(e.getCause().getMessage()); - + // AnalysisLogger.getLogger().debug( + // "In ConnectionManager-> " + // + e.getCause().getLocalizedMessage()); + // + // throw new Exception(e.getCause().getMessage()); + AnalysisLogger.getLogger().debug( - "In ConnectionManager-> " - + e.getLocalizedMessage()); - - throw new Exception(e.getLocalizedMessage()); + "In ConnectionManager-> " + e.getLocalizedMessage() + + "." + cause); + + throw new Exception(e.getLocalizedMessage() + "." + cause); } if (e instanceof org.hibernate.exception.GenericJDBCException) { - -// AnalysisLogger.getLogger().debug( -// "In ConnectionManager-> " -// + e.getCause().toString()); - -// e.printStackTrace(); - + + // AnalysisLogger.getLogger().debug( + // "In ConnectionManager-> " + // + e.getCause().toString()); + + // e.printStackTrace(); + AnalysisLogger.getLogger().debug( - "In ConnectionManager-> " - + e.getLocalizedMessage()); - + "In ConnectionManager-> " + e.getLocalizedMessage()); + throw new Exception(e.getLocalizedMessage()); } @@ -291,6 +295,26 @@ public class ConnectionManager { return obj; } + +// //to cancel the execution of the current query +// public void removeQueryExecution(SessionFactory DBSessionFactory) throws Exception{ +// +// try{ +// +// Session ss = DBSessionFactory.getCurrentSession(); +// +// ss.beginTransaction(); +// +// ss.cancelQuery(); +// +// ss.getTransaction().commit(); +// +// } +// catch (Exception e) { +// throw e; +// } +// +// } // /** Method that creates the connection */ // public SessionFactory createConnection(AlgorithmConfiguration config) {