I am trying to use an orientdb in-memory instance in a multi-threaded app. I've looked at lots of code examples but none of the seem to cover this case. Here's my test code:
DbTest.java
public class DbTest {
public static void main(String [] args) {
ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:MyDb").create();
Runnable myRunnable = new MyRunnable(db);
Thread thread = new Thread(myRunnable);
thread.start();
}
}
MyRunnable.java
public class MyRunnable implements Runnable {
private ODatabaseDocumentTx db;
MyRunnable(ODatabaseDocumentTx db) {
this.db = db;
}
public void run() {
ODocument animal = new ODocument("Animal");
animal.field( "name", "Gaudi" );
animal.field( "location", "Madrid" );
animal.save();
}
}
This gives the error
Exception in thread "Thread-3" com.orientechnologies.orient.core.exception.ODatabaseException: Database instance is not set in current thread. Assure to set it with: ODatabaseRecordThreadLocal.INSTANCE.set(db);
I read that I should try to use the connection pool, but the connection pool does not seem to work with memory databases
ODatabaseDocumentTx db = ODatabaseDocumentPool.global().acquire("memory:MyDb", "admin", "admin");
Exception in thread "main" com.orientechnologies.orient.core.exception.OStorageException: Cannot open local storage 'MyDb' with mode=rw
Any ideas how this is actually supposed to work?