SQLAlchemy session handling in desktop application

Viewed 33

I'd like some advice about SQLAlchemy session handling in a single user desktop (PyQt5) application.

The application is a music player where track data is stored in the database. Key is that the application has a concept of "next track" and will need to retrieve data (path, length, title, etc) when that track is played. The next track may be changed at any time by the user.

Currently, the application wraps functions using the ORM in a with Session() as session: context manager. That means that if the application stores the "next track" object, that object is detached when it is needed later.

Options:

  • wrap object access in an if object not in session: and add() it or get() it if necessary.

  • set expire_on_commit to False

  • start a session when the application starts and hold that session open indefinitely

Is any one of those alternatives better/worse that the others? Have I missed a better way of handling this?

0 Answers
Related