Troubleshooting using Java API

OConcurrentModificationException: Cannot update record #X:Y in storage ‘Z’ because the version is not the latest. Probably you are updating an old record or it has been modified by another user (db=vA your=vB)

This exception happens because you’re running in a Multi Version Control Check (MVCC) system and another thread/user has updated the record you’re saving. For more information about this topic look at Concurrency. To fix this problem you can:

  • Change the Graph consistency level to don’t use transactions.
  • Or write code concurrency proof.

Example:

  1. for (int retry = 0; retry < maxRetries; ++retry) {
  2. try {
  3. // APPLY CHANGES
  4. document.field(name, "Luca");
  5. document.save();
  6. break;
  7. } catch(ONeedRetryException e) {
  8. // RELOAD IT TO GET LAST VERSION
  9. document.reload();
  10. }
  11. }

The same in transactions:

  1. for (int retry = 0; retry < maxRetries; ++retry) {
  2. db.begin();
  3. try {
  4. // CREATE A NEW ITEM
  5. ODocument invoiceItem = new ODocument("InvoiceItem");
  6. invoiceItem.field(price, 213231);
  7. invoiceItem.save();
  8. // ADD IT TO THE INVOICE
  9. Collection<ODocument> items = invoice.field(items);
  10. items.add(invoiceItem);
  11. invoice.save();
  12. db.commit();
  13. break;
  14. } catch (OTransactionException e) {
  15. // RELOAD IT TO GET LAST VERSION
  16. invoice.reload();
  17. }
  18. }

Where maxRetries is the maximum number of attempt of reloading.

Run in OSGi context

(by Raman Gupta) OrientDB uses ServiceRegistry to load OIndexFactory and some OSGi containers might not work with it.

One solution is to set the TCCL so that the ServiceRegistry lookup works inside of OSGi:

  1. ODatabaseObjectTx db = null;
  2. ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
  3. try {
  4. ClassLoader orientClassLoader = OIndexes.class.getClassLoader();
  5. Thread.currentThread().setContextClassLoader(orientClassLoader);
  6. db = objectConnectionPool.acquire(dbUrl, username, password);
  7. } finally {
  8. Thread.currentThread().setContextClassLoader(origClassLoader);
  9. }

Because the ServiceLoader uses the thread context classloader, you can configure it to use the classloader of the OrientDB bundle so that it finds the entries in META-INF/services.

Another way is to embed the dependencies in configuration in the Maven pom.xml file under plugin(maven-bundle-plugin)/configuration/instructions:

  1. <Embed-Dependency>
  2. orientdb-client,
  3. orient-commons,
  4. orientdb-core,
  5. orientdb-enterprise,
  6. orientdb-object,
  7. javassist
  8. </Embed-Dependency>

Including only the jars you need. Look at Which library do I use?

Database instance has been released to the pool. Get another database instance from the pool with the right username and password

This is a generic error telling that the database has been found closed while using it.

Check the stack trace to find the reason of it:

OLazyObjectIterator

This is the case when you’re working with Object Database API and a field contains a collection or a map loaded in lazy. On iteration it needs an open database to fetch linked records.

Solutions:

  • assure to leave the database open while browsing the field
  • or early load all the instances (just iterate the items)
  • define a fetch-plan to load the entire object tree in one shoot and then work offline. If you need to save the object back to the database then reopen the database and call db.save( object ).

Stack Overflow on saving objects

This could be due to the high deep of the graph, usually when you create many records. To fix it save the records more often.