3
votes

I tried the following steps

1.Using gatlin and arango java driver I tried creating one collection with concurrent users.

2.I am getting duplicate name error as multiple threads are trying to create the collection at the same time.

Is there any mechanism transaction mechanism in arango to allow the creation of a collection for a single thread and lock the other threads during that process.

3

3 Answers

2
votes

Creating collections in parallel with potentially the same names may indeed result in duplicate name errors.

Contrary to inserting, removing, updating and querying documents, the creation, deletion and renaming of collections cannot be made part of an ArangoDB transaction.

Thus there will be some races when collections are created in parallel, and the only proper way to handle them at the moment is to check the return code of the collection creation responses. The responses will clearly indicate whether collection creation has succeeded or if the was a duplicate name error. The application can then handle the error appropriately, either by aborting or choosing a different name.

0
votes

The problem is that you perform multiple database requests (like creating a collection) from different threads. ArangoDB doesn't know anything about your client threads. They are just other clients for the database. Database transaction wont help you. You would still get exceptions.

The easiest way to avoid your problem is by using a synchronized method in your threads for creating the collection. In that method you check whether the collection already exists before creating it.

Synchronized methods can only be executed in one thread at a time. Other threads calling it will be blocked. So the first thread calling your method will create the collection, following threads wont try to create it.

public synchronized void createCollection(ArangoDatabase database, String collection) {
  if (!database.getCollections().stream().map(c -> c.getName()).anyMatch(c -> c.equals(collection)) {
    database.createCollection(collection);
  }
}
0
votes

Fixed it using try catch in create collection method.If the creation of collection fails ArangoDB Exception is thrown.Then in the catch block check for the collection again and return that.