Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve session usage #21

Open
alecandido opened this issue Feb 27, 2022 · 0 comments
Open

Improve session usage #21

alecandido opened this issue Feb 27, 2022 · 0 comments
Labels
bug Something isn't working database Data persistence model

Comments

@alecandido
Copy link
Member

alecandido commented Feb 27, 2022

From SQLAlchemy:

Is the session thread-safe?

The Session is very much intended to be used in a non-concurrent fashion, which usually means in only one thread at a time.
The Session should be used in such a way that one instance exists for a single series of operations within a single transaction. One expedient way to get this effect is by associating a Session with the current thread (see Contextual/Thread-local Sessions for background). Another is to use a pattern where the Session is passed between functions and is otherwise not shared with other threads.

I.e.: instead of passing around an instantiated session, we should pass around a class Session, created by an initial sessionmaker call, and use it in this way:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# an Engine, which the Session will use for connection
# resources
engine = create_engine('postgresql://scott:tiger@localhost/')

# a sessionmaker(), also in the same scope as the engine
Session = sessionmaker(engine)

# we can now construct a Session() and include begin()/commit()/rollback()
# at once
with Session.begin() as session:
    session.add(some_object)
    session.add(some_other_object)
# commits the transaction, closes the session

This is not that complicated, and essentially it boils down to change this call:
https://github.com/N3PDF/banana/blob/c33d339fb5ce6bc483154a6a5589711d58e7a35b/src/banana/benchmark/runner.py#L209
removing the last call, to get just the session class, and then call Session.begin() every time that is need, directly inside the functions of the data/sql.py module.

(The overall idea, after changing the call, is to use the same "channels" that are currently used to propagate the session object, to propagate instead the Session class)

@alecandido alecandido added database Data persistence model bug Something isn't working labels Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working database Data persistence model
Projects
None yet
Development

No branches or pull requests

1 participant