CodeCompass icon indicating copy to clipboard operation
CodeCompass copied to clipboard

Plugins should share their database connection pool

Open mcserep opened this issue 5 years ago • 1 comments

Connection to SQLite / PostgreSQL database is performed by the cc::util::connectDatabase() function. The function contains a simplistic database connection caching technique: existing connections are stored in the std::map<std::string, std::shared_ptr<odb::database>> databasePool static variable and can be reused later if the connection string matches precisely: https://github.com/Ericsson/CodeCompass/blob/173a3cf15e15be6de38b476fa7ff5eb258829e8c/util/src/dbutil.cpp#L291-L304

However the util module is statically linked to all plugin shared objects (and to the CodeCompass_webserver and CodeCompass_parser binaries), thus the databasePool map is not shared between them, but instead all plugins have their separate database connection pool.

While not documented properly, we also know that we don't need to deal with creating separate database connections per thread, because it is done by ODB automatically.

If you try to access the database from multiple threads simultaneously, then ODB will create and use several connections.

This is the reason why we experienced that the CodeCompass webserver maintains approximately threads * plugins amount of database connections, which can easily go over the default maximum limit of 100 in case of PostgreSQL. While we definitely need separate connections per threads, the database pool between shared objects and libraries shall be shared!

mcserep avatar Oct 24 '20 20:10 mcserep

I just looked into this issue, and have one small addition: the webserver maintains projects in the workspace * threads * plugins number of database connections, not threads * plugins.

This is the number of db connections when the webserver is not running: Screenshot from 2021-12-08 20-57-16

Webserver is running on 2 threads and there is 1 project in the workspace directory: Screenshot from 2021-12-08 20-59-07

Webserver is running on 2 threads and there are 2 projects in the workspace directory: Screenshot from 2021-12-08 21-00-22

intjftw avatar Dec 08 '21 18:12 intjftw