{"record":{"id":"0eaeb24151bf2821","repo":"pentaho/pentaho-kettle","slug":"the-shared-objects-meta-store-already-contains-an-element","errorCode":null,"errorMessage":"The shared objects meta store already contains an element with type name '{}' and element name '{}'","messagePattern":"The shared objects meta store already contains an element with type name '(.+?)' and element name '(.+?)'","errorType":"exception","errorClass":"MetaStoreException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/shared/SharedObjectsMetaStore.java","lineNumber":162,"sourceCode":"    return null;\n  }\n\n  @Override\n  public IMetaStoreElement getElementByName( String namespace, IMetaStoreElementType elementType, String name ) throws MetaStoreException {\n    for ( IMetaStoreElement element : getElements( namespace, elementType ) ) {\n      if ( ( element.getName().equalsIgnoreCase( name ) ) ) {\n        return element;\n      }\n    }\n    return null;\n  }\n\n  @Override\n  public void createElement( String namespace, IMetaStoreElementType elementType, IMetaStoreElement element ) throws MetaStoreException, MetaStoreElementExistException {\n    try {\n      IMetaStoreElement exists = getElementByName( namespace, elementType, element.getId() );\n      if ( exists != null ) {\n        throw new MetaStoreException( \"The shared objects meta store already contains an element with type name '\"\n          + elementType.getName() + \"' and element name '\" + element.getName() );\n      }\n\n      if ( elementType.getName().equals( databaseElementType.getName() ) ) {\n        // convert the element to DatabaseMeta and store it in the shared objects file, then save the file\n        //\n        sharedObjects.storeObject( DatabaseMetaStoreUtil.loadDatabaseMetaFromDatabaseElement( this, element ) );\n        sharedObjects.saveToFile();\n        return;\n      }\n      throw new MetaStoreException( \"Storing elements with element type name '\"\n        + elementType.getName() + \"' is not supported in the shared objects meta store\" );\n    } catch ( Exception e ) {\n      throw new MetaStoreException( \"Unexpected error creating an element in the shared objects meta store\", e );\n    }\n  }\n\n  @Override","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/shared/SharedObjectsMetaStore.java#L144-L180","documentation":"SharedObjectsMetaStore.createElement() first checks getElementByName() for an existing element and throws MetaStoreException if one with the same id/name already exists. Because shared objects are keyed by name, duplicates are not allowed. Note the message string omits the closing quote after the element name (formatting bug upstream).","triggerScenarios":"Calling createElement with an element whose id/name matches an element already present in the shared objects map, e.g. saving a DatabaseMeta with a name that already exists in shared objects.","commonSituations":"Re-importing database connections that already exist; scripts or UI flows that save connections without checking for name collisions; repeated test runs against the same shared.xml.","solutions":["Call getElementByName() first and update the existing element instead of creating a new one","Rename the new element to a unique name before creating","Delete the existing element via deleteElement, then re-create it"],"exampleFix":"// before\nmetaStore.createElement( ns, type, element );\n// after\nif ( metaStore.getElementByName( ns, type, element.getName() ) != null ) {\n  element.setName( element.getName() + \"-\" + System.currentTimeMillis() );\n}\nmetaStore.createElement( ns, type, element );","handlingStrategy":"validation","validationCode":"IMetaStoreElement existing = metaStore.getElementByName( ns, elementType, element.getName() );\nif ( existing != null ) {\n  throw new IllegalStateException( \"Element '\" + element.getName() + \"' already exists\" );\n}","typeGuard":null,"tryCatchPattern":"try {\n  metaStore.createElement( ns, type, element );\n} catch ( MetaStoreException e ) {\n  if ( e.getMessage() != null && e.getMessage().contains( \"already contains an element\" ) ) {\n    metaStore.deleteElement( ns, type, element.getName() );\n    metaStore.createElement( ns, type, element ); // upsert retry\n  } else { throw e; }\n}","preventionTips":["Always getElementByName() before createElement for an upsert workflow","Enforce unique naming for shared objects (connections etc.) in your UI/importer","Avoid re-running import scripts against the same shared.xml without idempotency checks"],"tags":["metastore","duplicate","pdi"],"backgroundTag":"file-already-exists","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}