From f3bb13279eaafe0a5ac922022dba25ae5aaf6c3f Mon Sep 17 00:00:00 2001 From: Alessandro Pieve Date: Tue, 17 Jan 2017 11:04:47 +0000 Subject: [PATCH] Add a openConnection and disconnect git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@141600 82a268e6-3cf1-43bd-a215-b396298e98cf --- .project | 13 ++++++++++ .../FallbackPersistenceBackend.java | 8 ++++++ .../persistence/PersistenceBackend.java | 26 ++++++++++++++++++- .../PersistenceBackendFactory.java | 4 +-- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/.project b/.project index 56f8b28..de8ff78 100644 --- a/.project +++ b/.project @@ -5,11 +5,21 @@ + + org.eclipse.wst.common.project.facet.core.builder + + + org.eclipse.jdt.core.javabuilder + + org.eclipse.wst.validation.validationbuilder + + + org.eclipse.m2e.core.maven2Builder @@ -17,7 +27,10 @@ + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature diff --git a/src/main/java/org/gcube/documentstore/persistence/FallbackPersistenceBackend.java b/src/main/java/org/gcube/documentstore/persistence/FallbackPersistenceBackend.java index 64b6e96..519dc5b 100644 --- a/src/main/java/org/gcube/documentstore/persistence/FallbackPersistenceBackend.java +++ b/src/main/java/org/gcube/documentstore/persistence/FallbackPersistenceBackend.java @@ -38,6 +38,12 @@ public class FallbackPersistenceBackend extends PersistenceBackend { // Nothing TO DO } + @Override + protected void openConnection() throws Exception { + // Nothing TO DO + } + + /** * {@inheritDoc} * This method is synchronized on {@link File} used, so any actions which @@ -70,5 +76,7 @@ public class FallbackPersistenceBackend extends PersistenceBackend { public void close() throws Exception { // Nothing TO DO } + + } diff --git a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java index 2b28be8..1e846a7 100644 --- a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java +++ b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java @@ -89,6 +89,21 @@ public abstract class PersistenceBackend { */ protected abstract void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception; + /** + * This method is for open connection + * @throws Exception + */ + protected abstract void openConnection() throws Exception ; + + + /** + * This method is for disconnect + * @throws Exception + */ + protected void disconnect() throws Exception { + //NOTHING + }; + /** * This method contains the code to save the {@link Record} * @@ -99,9 +114,13 @@ public abstract class PersistenceBackend { /*** * * @param records + * @throws Exception */ - protected void accountWithFallback(Record... records) { + protected void accountWithFallback(Record... records) throws Exception { String persistenceName = this.getClass().getSimpleName(); + + this.openConnection(); + for(Record record : records){ try { //old code @@ -135,6 +154,8 @@ public abstract class PersistenceBackend { //old code //PersistenceBackendFactory.renew(this); countFallback=0; + //disconnect + this.disconnect(); } } else{ @@ -152,6 +173,7 @@ public abstract class PersistenceBackend { } } } + this.close(); } /** @@ -217,6 +239,8 @@ public abstract class PersistenceBackend { public abstract void close() throws Exception; + + } diff --git a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java index d723391..6e3a92c 100644 --- a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java +++ b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java @@ -166,9 +166,9 @@ public abstract class PersistenceBackendFactory { logger.trace("Going to synchronized block in getPersistenceBackend"); synchronized (persistenceBackends) { persistence = persistenceBackends.get(context); - logger.trace("[getPersistenceBackend]{} {} in context {}", PersistenceBackend.class.getSimpleName(), persistence,context); + //logger.trace("[getPersistenceBackend]{} {} in context {}", PersistenceBackend.class.getSimpleName(), persistence,context); if(persistence==null){ - + logger.trace("[getPersistenceBackend]{} {} in context {}", PersistenceBackend.class.getSimpleName(), persistence,context); /* * Setting FallbackPersistence and unlocking. * There will be another thread which will try to discover the