google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Problem with using many DataStores #230

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

package com.nexuus.jobs.data.repository;

import java.net.UnknownHostException;

import org.junit.Before;
import org.junit.Test;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.utils.Assert;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.nexuus.jobs.data.domain.Category;

public class CategoryUweTest {
    private Category phoneCategory = new Category();
    private Category iphoneCategory = new Category();
    private Category appleCategory = new Category();
    private CategoryRepositoryDAO2 categoryDAO;

    protected Mongo mongo;
    protected Datastore ds;
    protected Morphia morphia = new Morphia();

    @Before
    public void setUp() {
        try {
            this.mongo = new Mongo();
        } catch (Exception e) {

        }

        this.ds = this.morphia.createDatastore(this.mongo, "morphia_test");
        this.morphia.createDatastore(this.mongo, "morphia_test2")
        this.categoryDAO = new CategoryRepositoryDAO2(ds);
    }

    @Test
    public void testCategoryTreeSavedAndRecovered() {
        phoneCategory.addChildrenCategory(appleCategory);
        appleCategory.setParentCategory(phoneCategory);

        appleCategory.addChildrenCategory(iphoneCategory);
        iphoneCategory.setParentCategory(appleCategory);

        categoryDAO.save(phoneCategory);
        categoryDAO.save(appleCategory);
        categoryDAO.save(iphoneCategory);

        phoneCategory = categoryDAO.get(phoneCategory.getId());
        appleCategory = categoryDAO.get(appleCategory.getId());
        iphoneCategory = categoryDAO.get(iphoneCategory.getId());

        Assert.isTrue(
                phoneCategory.getChildrenCategories().contains(appleCategory),
                "Parent does not contain child");

        Assert.isTrue(appleCategory.getParentCategory().equals(phoneCategory),
                "Child has not expected parent");

        Assert.isTrue(
                appleCategory.getChildrenCategories().contains(iphoneCategory),
                "Parent does not contain child");

        Assert.isTrue(iphoneCategory.getParentCategory().equals(appleCategory),
                "Child has not expected parent");

    }
}

I have highlighted in red the code that is causing the problem. As you can see 
what I am doing is:

   1. Creating an instance of Mongo
   2. Calling Morphia.createDataStore for one DB (morphia_test), which is the one I use for create the DAO.
   3. Calling Morphia.createDataStore of one DB (morphia_test2), which is not used.
   4. When the test run, I get the error.

No mater if I create the DAO before the the second call to 
Morphia.createDataStore, the error will happen.

In my code, I have this problem because Spring is calling a factory class for 
create the datastore via Morphia.createDataStore two times, one for a main 
database, and other for a "historic" db. This is the root of my problem.

My question is: this is a bug? or I have understood something wrong?

I have not reviewed the code of Morphia deeply, but browsing the source, it 
seems that DataStoreHolder class can only keep one copy of a datastore which 
makes me think why I am only getting the error with this class (which has 
references to it selfs) and not with other classes (without references).
I guess it is happengin something  like this:

   1. On save, the DAO is using the datastore assigned to the DAO.
   2. On read, and with @Reference, Morphia is recovering somehow the DAO stored in DataStoreHolder, and is not finding the entities, simply because they are stored in the datastore assigned to the DAO.

Original issue reported on code.google.com by google-a...@codesmell.de on 24 Feb 2011 at 7:54

GoogleCodeExporter commented 9 years ago
not too easy to resolve, because DBRef does not contain a notion of a 
databaseName of similar.

Effectively, CustomMapper would have to get another parameter (Datastore, 
probably).
Everyone ok with that?

Original comment by google-a...@codesmell.de on 24 Feb 2011 at 11:04