Multithreaded application

Hi, I am new to OrientDB, and I am working on a multithread application in java running on TomCat.
When I use
database.activateOnCurrentThread()
what happens to the other database instance created in the previous thread? Could it cause Memory Leak?

Thank you in advance

Hi @Jack1

When you open a DB connection, you have to make sure you close it, regardless of the thread it’s using it (so if you close it in a different thread, that’s still closed and it’s not a memory leak).

This said, OrientDB database sessions are not designed to be thread-safe, so I strongly suggest you to avoid sharing them between threads (using activateOnCurrentThread() is a hack you can use in very critical situations, but it’s very easy to lose control and incur in errors).
What I’d suggest to do instead is to use a connection pool and aquire/release connections from it without sharing them between threads (share the pool instead, of course). Please consider that acquiring a connection from the pool is an extremely cheap operation, so you won’t have performance problems

Thanks

Luigi

Thank you very much for the reply.
I wanted to use that hack because my multithread application is using a method that returns a stream. When I close that connection like you said it returns this error:

The database instance is not set in the current thread. Be sure to set it with: ODatabaseRecordThreadLocal.instance().set(db);

Here is the code:

public Stream mymethod(String name){

  String statement = "SELECT * FROM MyClass WHERE name = ?";
  try (database = pool.acquire()){
  	OResultSet set = database.query(statement, name);
  	Stream<MyClass> models = set.stream().map(d -> MyClass.of(d));
  	return models;
  } catch (Exception e) {
  	throw new Exception(e);
  }

}

Thanks