From f59b3b550fc8b8a7da74d45f88573dd2e780ca3c Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Mon, 6 Nov 2017 13:56:36 +0000 Subject: [PATCH] Refs #10221: Reopen DB connection if closed Task-Url: https://support.d4science.org/issues/10221 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158228 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../context/ContextManagementImpl.java | 21 ++++--- .../context/ContextUtility.java | 35 +++--------- .../context/SecurityContextMapper.java | 56 ++++++++++++++++--- .../er/relation/RelationManagement.java | 4 +- .../schema/SchemaContextManagement.java | 10 +--- .../schema/SchemaManagementImpl.java | 28 +++------- 6 files changed, 78 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java index f0196fc..cd1f311 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java @@ -116,9 +116,9 @@ public class ContextManagementImpl implements ContextManagement { "Trying to create {} with name {} and parent {} UUID {}", Context.NAME, name, Context.NAME, parentContext); - orientGraph = SecurityContextMapper.getSecurityContextFactory( + orientGraph = SecurityContextMapper.getSecurityContextGraph( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER).getTx(); + PermissionMode.WRITER); Vertex parent; try { @@ -173,10 +173,9 @@ public class ContextManagementImpl implements ContextManagement { @Override public String read(UUID contextUUID) throws ContextNotFoundException, ContextException { - OrientGraph orientGraph = SecurityContextMapper - .getSecurityContextFactory( + OrientGraph orientGraph = SecurityContextMapper.getSecurityContextGraph( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.READER).getTx(); + PermissionMode.READER); Vertex context = getContext(orientGraph, contextUUID); return Utility.toJsonString((OrientVertex) context, false); } @@ -191,9 +190,9 @@ public class ContextManagementImpl implements ContextManagement { logger.info("Trying to rename {} with UUID {} to {}", Context.NAME, contextUUID, newName); - orientGraph = SecurityContextMapper.getSecurityContextFactory( + orientGraph = SecurityContextMapper.getSecurityContextGraph( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER).getTx(); + PermissionMode.WRITER); Vertex context = getContext(orientGraph, contextUUID); @@ -253,9 +252,9 @@ public class ContextManagementImpl implements ContextManagement { "Trying to move {} with UUID {} as child of {} with UUID {}", Context.NAME, contextToMoveUUID, Context.NAME, newParentUUID); - orientGraph = SecurityContextMapper.getSecurityContextFactory( + orientGraph = SecurityContextMapper.getSecurityContextGraph( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER).getTx(); + PermissionMode.WRITER); Vertex context = getContext(orientGraph, contextToMoveUUID); logger.trace("Context to move {}", @@ -323,9 +322,9 @@ public class ContextManagementImpl implements ContextManagement { - orientGraph = SecurityContextMapper.getSecurityContextFactory( + orientGraph = SecurityContextMapper.getSecurityContextGraph( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER).getTx(); + PermissionMode.WRITER); Vertex context = getContext(orientGraph, uuid); logger.trace("Context to be delete {}", diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java index 637ac26..2e66b26 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java @@ -28,7 +28,6 @@ import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.tinkerpop.blueprints.Element; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; -import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; /** @@ -104,9 +103,7 @@ public class ContextUtility { public static UUID getActualContextUUID() throws ContextException { - OrientGraphFactory factory = null; OrientGraph orientGraph = null; - try { String scope = getCurrentContext(); if(scope==null){ @@ -118,11 +115,10 @@ public class ContextUtility { if(uuid == null){ logger.trace("UUID for scope {} is not in cache. Going to query it", scope); - factory = SecurityContextMapper - .getSecurityContextFactory( + orientGraph = SecurityContextMapper + .getSecurityContextGraph( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.READER); - orientGraph = factory.getTx(); Vertex context = ContextUtility.getContextVertexByFullName( orientGraph, scope); uuid = Utility.getUUID(context); @@ -143,26 +139,11 @@ public class ContextUtility { } } - public static OrientGraphFactory getFactory(PermissionMode permissionMode) - throws ResourceRegistryException { - try { - UUID contextUUID = getActualContextUUID(); - return SecurityContextMapper.getSecurityContextFactory(contextUUID, - permissionMode); - } catch (ContextException ce) { - logger.error("Unable to retrieve context.", ce); - throw ce; - } catch (Exception e) { - logger.error("Unable to retrieve context.", e); - throw new ResourceRegistryException(e); - } - } - public static OrientGraph getActualSecurityContextGraph( PermissionMode permissionMode) throws ResourceRegistryException { try { - OrientGraphFactory factory = getFactory(permissionMode); - return factory.getTx(); + UUID contextUUID = getActualContextUUID(); + return SecurityContextMapper.getSecurityContextGraph(contextUUID, permissionMode); } catch (ContextException ce) { logger.error("Unable to retrieve context.", ce); throw ce; @@ -175,8 +156,8 @@ public class ContextUtility { public static OrientGraphNoTx getActualSecurityContextGraphNoTx( PermissionMode permissionMode) throws ResourceRegistryException { try { - OrientGraphFactory factory = getFactory(permissionMode); - return factory.getNoTx(); + UUID contextUUID = getActualContextUUID(); + return SecurityContextMapper.getSecurityContextGraphNoTx(contextUUID, permissionMode); } catch (ContextException ce) { logger.error("Unable to retrieve context.", ce); throw ce; @@ -189,8 +170,8 @@ public class ContextUtility { public static ODatabaseDocumentTx getActualSecurityContextDatabaseTx( PermissionMode permissionMode) throws ResourceRegistryException { try { - OrientGraphFactory factory = getFactory(permissionMode); - return factory.getDatabase(); + UUID contextUUID = getActualContextUUID(); + return SecurityContextMapper.getSecurityContextDatabaseDocumentTx(contextUUID, permissionMode); } catch (ContextException ce) { logger.error("Unable to retrieve context.", ce); throw ce; diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java index 09ed7b8..11e3112 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java @@ -12,8 +12,10 @@ import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseInt import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory; +import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; /** * @author Luca Frosini (ISTI - CNR) @@ -72,13 +74,13 @@ public abstract class SecurityContextMapper { MANAGEMENT_SECURITY_CONTEXT_UUID); getSecurityContextFactory(MANAGEMENT_SECURITY_CONTEXT_UUID, - PermissionMode.READER); + PermissionMode.READER, false); getSecurityContextFactory(MANAGEMENT_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); + PermissionMode.WRITER, false); DatabaseIntializator.createEntitiesAndRelations(); } - + } catch (Exception e) { logger.error("Error initializing database connection", e); throw new RuntimeException( @@ -115,19 +117,22 @@ public abstract class SecurityContextMapper { } /** - * @param contextID - * use null for no context (used for admin operations) + * @param context Context UUID. For ADMIN operation uses SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID * @return */ - public static OrientGraphFactory getSecurityContextFactory( - UUID context, PermissionMode permissionMode) { + private static OrientGraphFactory getSecurityContextFactory( + UUID context, PermissionMode permissionMode, boolean recreate) { OrientGraphFactory factory = null; Map permissionSecurityContextFactories = securityContextFactories .get(permissionMode); - factory = permissionSecurityContextFactories.get(context); - + if(recreate) { + permissionSecurityContextFactories.remove(context); + }else { + factory = permissionSecurityContextFactories.get(context); + } + if (factory == null) { String username = getSecurityRoleOrUserName(permissionMode, @@ -145,6 +150,39 @@ public abstract class SecurityContextMapper { return factory; } + public static OrientGraph getSecurityContextGraph( + UUID context, PermissionMode permissionMode) { + OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); + OrientGraph orientGraph = factory.getTx(); + if(orientGraph.isClosed()) { + factory = getSecurityContextFactory(context, permissionMode, true); + orientGraph = factory.getTx(); + } + return orientGraph; + } + + public static OrientGraphNoTx getSecurityContextGraphNoTx( + UUID context, PermissionMode permissionMode) { + OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); + OrientGraphNoTx orientGraphNoTx = factory.getNoTx(); + if(orientGraphNoTx.isClosed()) { + factory = getSecurityContextFactory(context, permissionMode, true); + orientGraphNoTx = factory.getNoTx(); + } + return orientGraphNoTx; + } + + public static ODatabaseDocumentTx getSecurityContextDatabaseDocumentTx( + UUID context, PermissionMode permissionMode) { + OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, true); + ODatabaseDocumentTx databaseDocumentTx = factory.getDatabase(); + if(databaseDocumentTx.isClosed()) { + factory = getSecurityContextFactory(context, permissionMode, true); + databaseDocumentTx = factory.getDatabase(); + } + return databaseDocumentTx; + } + public static String getSecurityRoleOrUserName( PermissionMode permissionMode, SecurityType securityType, UUID context) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java index 4e1f33d..c7cb1b9 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java @@ -724,9 +724,9 @@ public abstract class RelationManagement extends accessType.getName(), uuid); try { - orientGraph = SecurityContextMapper.getSecurityContextFactory( + orientGraph = SecurityContextMapper.getSecurityContextGraph( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER).getTx(); + PermissionMode.WRITER); boolean added = forcedAddToContext(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java index ba3330c..f24feb5 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java @@ -19,7 +19,6 @@ import com.orientechnologies.orient.core.record.impl.ODocument; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientEdge; import com.tinkerpop.blueprints.impls.orient.OrientGraph; -import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory; import com.tinkerpop.blueprints.impls.orient.OrientVertex; public class SchemaContextManagement implements SchemaManagement { @@ -53,16 +52,11 @@ public class SchemaContextManagement implements SchemaManagement { @Override public String create(String json, AccessType baseType) throws SchemaException { - - OrientGraphFactory orientGraphFactory = SecurityContextMapper - .getSecurityContextFactory( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); - OrientGraph orientGraph = null; try { - orientGraph = orientGraphFactory.getTx(); + orientGraph = SecurityContextMapper + .getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER); ObjectMapper mapper = new ObjectMapper(); TypeDefinition typeDefinition = mapper.readValue(json, TypeDefinition.class); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java index 2f8027e..fc9702e 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java @@ -38,7 +38,6 @@ import com.orientechnologies.orient.core.metadata.schema.OType; import com.orientechnologies.orient.core.record.impl.ODocument; import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph; import com.tinkerpop.blueprints.impls.orient.OrientElementType; -import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory; import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx; /** @@ -88,17 +87,14 @@ public class SchemaManagementImpl implements SchemaManagement { public static OClass getTypeSchema(String type, AccessType accessType) throws SchemaException { - OrientGraphFactory orientGraphFactory = SecurityContextMapper - .getSecurityContextFactory( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.READER); - OrientGraphNoTx orientGraphNoTx = null; try { logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type); - orientGraphNoTx = orientGraphFactory.getNoTx(); + orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( + SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, + PermissionMode.READER); return getTypeSchema(orientGraphNoTx, type, accessType); @@ -173,11 +169,6 @@ public class SchemaManagementImpl implements SchemaManagement { protected String registerTypeSchema(String jsonSchema, AccessType baseType) throws SchemaException { - OrientGraphFactory orientGraphFactory = SecurityContextMapper - .getSecurityContextFactory( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); - OrientGraphNoTx orientGraphNoTx = null; try { logger.info("Trying to register {} {}", baseType.getName(), @@ -187,7 +178,9 @@ public class SchemaManagementImpl implements SchemaManagement { TypeDefinition typeDefinition = mapper.readValue(jsonSchema, TypeDefinition.class); - orientGraphNoTx = orientGraphFactory.getNoTx(); + orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( + SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, + PermissionMode.WRITER); OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); OSchema oSchema = oMetadata.getSchema(); @@ -342,14 +335,11 @@ public class SchemaManagementImpl implements SchemaManagement { protected String getSchema(String type, boolean includeSubtypes) throws SchemaNotFoundException, SchemaException { - OrientGraphFactory orientGraphFactory = SecurityContextMapper - .getSecurityContextFactory( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); - OrientGraphNoTx orientGraphNoTx = null; try { - orientGraphNoTx = orientGraphFactory.getNoTx(); + orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( + SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, + PermissionMode.WRITER); OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); OSchema oSchema = oMetadata.getSchema();