Search code examples
javamongodbmorphia

Morphia: Create new Morphia instance for each database?


In my initialization code right now, I create 1 Morphia instance and use it to map all my classes across all my databases. I've noticed though that when I call ensureIndexes(), it will initialize an empty collection and index all the collections across all databases.

Is the correct pattern for this to instead create 1 Morphia instance per each database and then loop through each instance to ensureIndexes?


Solution

  • You can use the one Morphia instance and create one Datastore for each db so long as they all share the same mapping information.