Skip to content

dev.morphia.Datastore Thread Safety #2748

@AmitKuGarg

Description

@AmitKuGarg

Describe the bug
When dev.morphia.Datastore shared across thread, I get Two entities have been mapped using the same discriminator value.

To Reproduce
Steps to reproduce the behavior:

  1. Create a sample collection and create the datastore.
    Datastore datastore = Morphia.createDatastore(mongo, dbName);
    datastore.ensureIndexes();
    if(this.packages != null){
    for (String packageString : packages) {
    datastore.getMapper().map(packageString);
    }
    }

  2. Create a runnable class and pass the data store and id parameter. In run method load the data from the collection using id and datastore.
    class Task implements Runnable {
    private String id;
    private Datastore mongoDatastore;

    Task(String id, Datastore mongoDatastore) {
    this.id = id;
    this.mongoDatastore = mongoDatastore;
    }

    @OverRide
    public void run() {

     Sample result = mongoDatastore.find(Sample.class).filter(Filters.eq("_id", id)).first();        
     if(result == null){
         System.out.println("Result is null");
     }else {
         System.out.println(result.toString());
     }
    

    }
    }

  3. Write main method, create ExecutorService and try to load different documents.
    ExecutorService executor = Executors.newFixedThreadPool(10);
    executor.execute(new Task("ABC12345", mongoDatastore));
    executor.execute(new Task("ABC123456", mongoDatastore));

I get Two entities have been mapped using the same discriminator value.

Try two scenario

  1. When ids are valid id from collection
  2. Try some invalid ids where collection return null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions