Skip to content

Improve session usage #21

@alecandido

Description

@alecandido

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdatabaseData persistence model

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions