Incorrect behavior when using multiple databases (OObjectDatabaseTx and Java proxy objects)
If an application connects to two different databases running in the same server, the OObjectDatabaseTx EntityManager and Java proxy objects do not correctly maintain separate associations with the different databases. Problems include: entity classes being registered to incorrect databases, and Java proxy objects attempting to lazy-load from the wrong database (resulting in Exceptions).
Similar problems occur when trying to use the Connection Pool instead of individually instantiating OObjectDatabaseTx objects.
OrientDB versions
1.6.3 1.3.0
Expected results
In the following example, the expected result is that database "localhost/foo" has the Foo class registered, and that the proxy object queried from the database will lazy-load from that database for the entire life-cycle of the object. The final println should display the name of the child object and the "stringified" parent object. Database "localhost/bar" should have only entity class Bar registered.
Actual results
The queryResult.getParent() call in the final println throws an exception, because the lazy-load attempts to resolve the parent field RID using the "localhost/bar" database, instead of the "localhost/foo" database. Since the RIDs happen to be the same, Orient attempts to put the Bar object into the Foo parent field - resulting in an Exception. In addition, database "localhost/bar" incorrectly has both Foo and Bar as registered entity classes.
Example
package orienttest;
import java.util.List;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
public class MultiDbTest
{
public static void main(String[] args)
{
OObjectDatabaseTx db1 = null;
OObjectDatabaseTx db2 = null;
try
{
db1 = new OObjectDatabaseTx("remote:localhost/foo");
db1.open("admin", "admin");
db1.getEntityManager().registerEntityClass(Foo.class);
Foo parent = new Foo();
parent.setName("parent");
parent = db1.save(parent); // Parent gets RID #9:0 in "localhost/foo"
Foo child = new Foo();
child.setParent(parent);
child.setName("child"); // Child gets RID #9:1 in "localhost/foo"
db1.save(child);
@SuppressWarnings("unchecked")
Foo queryResult = ((List<Foo>)db1.command(new OSQLSynchQuery<Foo>("select from Foo where name='child'")).execute()).get(0);
System.out.println("Retrieved Foo: " + queryResult.getName());
db2 = new OObjectDatabaseTx("remote:localhost/bar");
db2.open("admin", "admin");
db1.getEntityManager().registerEntityClass(Bar.class);
if( ! db2.existsCluster("Bar") )
{
System.out.println("Bar cluster not found, creating default");
db2.save(new Bar()); // new Bar() gets RID #9:0 in "localhost/bar"
}
// This fails because qr.getParent() tries to populate the parent field
// with RID #9:0 from "localhost/bar" NOT "localhost/foo", resulting in
// an Exception. Instead of getting the parent Foo object, it gets the
// Bar object created above, because the proxy is not connecting to the
// original db from which it was obtained.
System.out.println("Parent of " + queryResult.getName() + ": " + queryResult.getParent());
}
finally
{
if (db1 != null)
{
db1.close();
}
if (db2 != null)
{
db2.close();
}
}
}
static class Foo
{
private Foo parent;
private String name;
public Foo getParent()
{
return parent;
}
public void setParent(Foo parent)
{
this.parent = parent;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
static class Bar { }
}