SQLAlchemy: When to initialise a session

Loris Bennett loris.bennett at fu-berlin.de
Tue Feb 8 09:12:45 EST 2022


"Loris Bennett" <loris.bennett at fu-berlin.de> writes:

> Hi,
>
> I am writing a fairly simple command-line application which will just
> add or delete an entry in a database and then generate a corresponding
> email.
>
> I am using SQLAlchemy to wrap a class around a database and have
>
>   class DatebaseWrapper():
>       """Encapsulation of the database"""
>
>       def __init__(self, url):
>           self.engine = create_engine(url)
>  
> Should I extend the initialisation to
>
>       def __init__(self, url):
>           self.engine = create_engine(url)
>           self.session = sessionmaker(self.engine)
>
> since each there will be only one session per call of the program?
>
> Or, since I am writing the database wrapper as its own module for
> possible reuse, should the program using the wrapper class
> initialise the session itself?

Turns out this is all explained here:

  https://docs.sqlalchemy.org/en/14/orm/session_basics.html#session-frequently-asked-questions

Sorry for the noise.

-- 
This signature is currently under construction.


More information about the Python-list mailing list