added a timeout about 25-20 seconds.To add the timeout the following classes are been modified:

- class DatabaseFactory modified adding the initDBConnection method that overrides the properties contained in the hibernate.cfg.xml file.
- method initDBSession in class ConnectionManager modified in order to use the DatabaseFactory (in package utils) 

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@98967 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Loredana Liccardo 2014-07-25 08:13:25 +00:00
parent 229f351f7d
commit 5f795e85d0
2 changed files with 162 additions and 13 deletions

View File

@ -1,23 +1,20 @@
package org.gcube.dataaccess.databases.utils;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.FileTools;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.FileTools;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Class that allows to manage a database selected from a user. It performs to
* set the database configuration, to connect to the database and finally to
@ -36,11 +33,32 @@ public class ConnectionManager {
}
public static SessionFactory initDBSession(AlgorithmConfiguration config) {
SessionFactory dbHibConnection = null;
try {
if ((config != null) && (config.getConfigPath() != null)) {
String defaultDatabaseFile = config.getConfigPath() + AlgorithmConfiguration.defaultConnectionFile;
config.setDatabaseDriver(config.getParam("DatabaseDriver"));
config.setDatabaseUserName(config.getParam("DatabaseUserName"));
config.setDatabasePassword(config.getParam("DatabasePassword"));
config.setDatabaseURL(config.getParam("DatabaseURL"));
dbHibConnection = org.gcube.dataaccess.databases.utils.DatabaseFactory.initDBConnection(defaultDatabaseFile, config);
}
} catch (Exception e) {
System.out.println("ERROR IN DB INITIALIZATION : " + e.getLocalizedMessage());
e.printStackTrace();
// AnalysisLogger.getLogger().trace(e);
}
return dbHibConnection;
}
// create the database's connection without using the configuration file but
// using the data input
public SessionFactory initDBConnection(AlgorithmConfiguration config) {
SessionFactory dbconnection = DatabaseUtils.initDBSession(config);
SessionFactory dbconnection = initDBSession(config);
return dbconnection;

View File

@ -1,10 +1,17 @@
package org.gcube.dataaccess.databases.utils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.FileTools;
import org.hibernate.MappingException;
@ -29,6 +36,131 @@ public class DatabaseFactory {
return DBSessionFactory;
}
@SuppressWarnings({ "unchecked" })
public static SessionFactory initDBConnection(String configurationFile,
LexicalEngineConfiguration config)
throws Exception {
if (config == null)
return initDBConnection(configurationFile);
// take the configuration file
File fl = new File(configurationFile);
FileInputStream stream = new FileInputStream(fl);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(stream);
List<Node> nodes = document
.selectNodes("//hibernate-configuration/session-factory/property");
Iterator<Node> nodesIterator = nodes.iterator();
while (nodesIterator.hasNext()) {
Node currentnode = nodesIterator.next();
String element = currentnode.valueOf("@name");
if (element.equals("connection.driver_class")) {
if ((config.getDatabaseDriver() != null)
&& (config.getDatabaseDriver().length() > 0))
currentnode.setText(config.getDatabaseDriver());
else
config.setDatabaseDriver(currentnode.getText());
}
if (element.equals("connection.url")) {
if ((config.getDatabaseURL() != null)
&& (config.getDatabaseURL().length() > 0))
currentnode.setText(config.getDatabaseURL());
else
config.setDatabaseURL(currentnode.getText());
}
if (element.equals("connection.username")) {
if ((config.getDatabaseUserName() != null)
&& (config.getDatabaseUserName().length() > 0))
currentnode.setText(config.getDatabaseUserName());
else
config.setDatabaseUserName(currentnode.getText());
}
if (element.equals("connection.password")) {
if ((config.getDatabasePassword() != null)
&& (config.getDatabasePassword().length() > 0))
currentnode.setText(config.getDatabasePassword());
else
config.setDatabasePassword(currentnode.getText());
}
if (element.equals("dialect")) {
if ((config.getDatabaseDialect() != null)
&& (config.getDatabaseDialect().length() > 0))
currentnode.setText(config.getDatabaseDialect());
else
config.setDatabaseDialect(currentnode.getText());
}
if (element.equals("c3p0.idleConnectionTestPeriod")) {
if (config.getDatabaseIdleConnectionTestPeriod() != null)
currentnode.setText(config
.getDatabaseIdleConnectionTestPeriod());
else
config.setDatabaseIdleConnectionTestPeriod(currentnode
.getText());
}
if (element.equals("c3p0.automaticTestTable")) {
if (config.getDatabaseAutomaticTestTable() != null)
currentnode.setText(config.getDatabaseAutomaticTestTable());
else
config.setDatabaseAutomaticTestTable(currentnode.getText());
}
// if (element.equals("c3p0.timeout")) {
// currentnode.setText("" + 30);
// }
/*
if (element.equals("hibernate.c3p0.testConnectionOnCheckout")) {
currentnode.setText("" + true);
}
if (element.equals("hibernate.c3p0.idle_test_period")) {
currentnode.setText("" + 1);
}
if (element.equals("hibernate.c3p0.timeout")) {
currentnode.setText("" + 1);
}
if (element.equals("connection.pool_size")) {
currentnode.setText("" + 1);
}
if (element.equals("c3p0.idleConnectionTestPeriod")) {
currentnode.setText("" + 1);
}
*/
if (element.equals("c3p0.checkoutTimeout")) {
currentnode.setText("" + (20000/4)); //added a timeout about 25-30 seconds
}
// if (element.equals("c3p0.acquireRetryAttempts")) {
//
// currentnode.setText("" + 10);
// }
}
Configuration cfg = new Configuration();
cfg = cfg.configure(DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(new ByteArrayInputStream(document.asXML().getBytes())));
cfg.setProperty("hibernate.hbm2ddl.auto", "create");
SessionFactory DBSessionFactory = null;
DBSessionFactory = cfg.buildSessionFactory();
// close stream
stream.close();
return DBSessionFactory;
}
// Method that execute the query
public static List<Object> executeSQLQuery(String query,
SessionFactory DBSessionFactory) throws Exception {
@ -86,7 +218,6 @@ public class DatabaseFactory {
return obj;
} catch (Exception e) {
if (e.getClass()
.toString()