Odoo Help
Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM
|
e-Commerce
|
Accounting
|
Inventory
|
PoS
|
Project management
|
MRP
|
etc.
What is the difference between `db_connect` and `get_db` to obtain a Cursor?
When I need a new Cursor() for a database, I have at least 2 ways to initialize a new one.
Using the helpers functions in openerp.pooler
:
cr = openerp.pooler.get_db('name_of_my_database').cursor() # or
db, pool = openerp.pooler.get_db_and_pool('name_of_my_database')
cr = db.cursor()
Or using openerp.sql_db.db_connect
:
cr = openerp.sql_db.db_connect('name_of_my_database').cursor()
Note that both are used in OpenERP server or addons.
What is the difference between them and for which use case should I use one or the other function?
By a reading of the functions, I found that:
openerp.pooler.get_db
get the Registry
for the database and returns the db
attribute of the Registry
. Registry().db
is initialized using openerp.sql_db.db_connect
.
At end, they both return an instance of a openerp.sql_db.Connection
which respond to cursor()
.
It seems to me that we can use one or the other equally. openerp.pooler.get_db
appears to be more 'High-Level' and needs to already have a Registry
for the database.
My feeling is that we should use openerp.sql_db.db_connect
only when we are not sure that we have a Registry
for the database (during start or update of modules), otherwise we should prefer to use openerp.pooler.get_db
.
This is a standard design pattern. Applications usually pool all their database connections so that each module does not have to create its on database connection (as they are very costly)
You should always use the pooler.get_db, which underneath creates a connection if needed. But, if the pooler already has a connection, it saves you a lot of time and gives it to you.
Just do repeat, you should always avoid making direct database connections and use the pooler (think of like like a car pool)
Thanks for your answer. However the pool of connections is more low level than the methods I mentioned, both methods will reach the pooler. The pooler is actually used when you call cursor()
on the Connection
object. The API / documentation is not clear about that and I could find code using both methods, hence my question.
About This Community
This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.
RegisterOdoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.
Test it nowQuestion tools
Stats
Asked: 2/12/13, 1:20 PM |
Seen: 3490 times |
Last updated: 1/18/17, 4:54 AM |