From 6b509fd9dbaa96f5921c9e020956b5478866ea19 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 1 Oct 2020 12:57:52 -0500 Subject: [PATCH] Prevent sqlalchemy from refetching the old_state_id as it will never change (#40982) Disable expire_on_commit for the event writer. Since we never expect the old_state_id to change in the database, it was never worth the expense of refetching the id after the commit. --- homeassistant/components/recorder/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index 3bf95b4303a..02a07bab628 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -573,7 +573,9 @@ class Recorder(threading.Thread): sqlalchemy_event.listen(self.engine, "connect", setup_recorder_connection) Base.metadata.create_all(self.engine) - self.get_session = scoped_session(sessionmaker(bind=self.engine)) + self.get_session = scoped_session( + sessionmaker(bind=self.engine, expire_on_commit=False) + ) def _close_connection(self): """Close the connection."""