I am wondering, whether I should minimize usage of db_session or not? Let's consider these two equivalent examples:
A)
def do_stuff():
with db_session:
task = orm.make_proxy(Task.select().first())
task.mark_started()
...
this_function_might_take_a_long_time(task)
...
with db_session:
task.mark_done()
B)
@db_session
def do_stuff():
task = Task.select().first()
task.mark_started()
commit()
...
this_function_might_take_a_long_time(task)
...
task.mark_done()
From reading the documentation I can tell that Pony doesn't encourage micro-managing db_sessions
With this code each of view function you will define will be wrapped with db_session so you should not care about them.
However, here it suggests that there might be a cost of having it open (EDIT: it doesn't, read the answer)
Before sending the first query, Pony gets a database connection from the connection pool.
Am I correct to say that anything beyond B is premature optimization, and A should be considered only in limited DB connection count scenarios?