Reputation: 14379
I have an embedded document store setup with OrientDB; using the Object API. My application is multithreaded (e.g. part of my application is a REST) and needs to access the database.
According to the documentation (http://www.orientechnologies.com/docs/last/orientdb.wiki/Object-Database.html), I should be using a ConnectionPool in an application similar to mine. They provide the following code example:
// OPEN THE DATABASE
OObjectDatabaseTx db= OObjectDatabasePool.global().acquire("remote:localhost/petshop", "admin", "admin");
// REGISTER THE CLASS ONLY ONCE AFTER THE DB IS OPEN/CREATED
db.getEntityManager().registerEntityClass("org.petshop.domain");
try {
...
} finally {
db.close();
}
However, it "appears" that this is doing a fair bit of work. As far as I can see it, for each REST request I need to make a connection to the database (admittedly the code might be caching the connections). For this I need the username and password everytime which seems strange to me - maybe because I am used to having all this managed for me.
My question is essentially - have I got this right? Do I have to acquire a connection for each REST request using the username and password and then close it afterwards to release the connection back to the pool?
Upvotes: 0
Views: 854
Reputation: 1949
From the code i see that OObjectDatabasePool.global() return a Singleton instance with no username and password setted in the constructor of course. I think to avoid using username and password every request do not use OObjectDatabasePool.global() But create your own pool singleton using the OObjectDatabasePool
like
OObjectDatabasePool singleton = new OObjectDatabasePool("remote:localhost/petshop", "admin", "admin").setup(1,10);;
Then get the db from this instance
just use
OObjectDatabaseTx db = singleton.acquire();
// REGISTER THE CLASS ONLY ONCE AFTER THE DB IS OPEN/CREATED
db.getEntityManager().registerEntityClass("org.petshop.domain");
try {
...
} finally {
db.close();
}
Hope it helps
Upvotes: 1