Hi guys,
Before OrientDB 2.1 managing multiple databases in the same thread was
something that "mostly" worked. Why "mostly"? Because most of the API
in ODatabase API (document an object) forced setting "this" as current in
ThreadLocal. This was done, even if the ODatabase API contract says
explicitly *it must not be used by multiple threads*: use multiple instance
instead.
Actually this caused many times with users *unpredictable behaviors* in
case the same ODatabase instance was called *by multiple threads at the
same time* with an almost impossible way to debug and understand what's
wrong with such simple test cases.
Instead, in OrientDB 2.1-SNAPSHOT, the ODatabase* APIs always check if the
current database is set in Thread Local, otherwise it throws an exception
of class *IllegalStateException* with a message like this:
"*Current database instance (ODatabaseDocumentTx@2323) is not active on
current thread (main). Current active database is:
ODatabaseDocumentTx@9912)*"
If you want to use another database different than the current one, call
*activateOnCurrentThread()* on the database before you use it. Example:
ODocument rec1 = database1.newInstance();
ODocument rec2 = database2.newInstance();
rec1.field("name", "Luca");
database1.activateOnCurrentThread(); // MANDATORY SINCE 2.1
database1.save(rec1); // force saving in database1 no matter where the
record came from
rec2.field("name", "Luke");
database2.activateOnCurrentThread(); // MANDATORY SINCE 2.1
database2.save(rec2); // force saving in database2 no matter where the
record came from
-[ Example extracted from
http://orientdb.com/docs/last/Java-Multi-Threading.html page ]-
So in case you have this exceptions after you switched to 2.1 GA, this
means you were using OrientDB in the wrong way with random issues on
concurrency. It's time to fix your code.
This exception is raised in he following cases:
1. you're switching between databases without calling
*db.activateOnCurrentThread()* before using "db" instance
2. you're passing a database instance across threads. Assure to call
*db.activateOnCurrentThread()* in the running thread before to access to
the database instance
3. you *forgot to properly close database instances* around in your
application. Assure you alway have this block: db = new
ODatabaseDocumentTx(...); try{ //your code } finally { db.close(); }
Don't hesitate to ask help in this Community Group to properly managed
multiple database instances.
Best Regards,
Luca Garulli
CEO at Orient Technologies LTD
the Company behind OrientDB <http://orientdb.com>
--
---
You received this message because you are subscribed to the Google Groups
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.