Only commit if need. (#21848)
parent
4d9cf15c45
commit
226be65910
|
@ -20,12 +20,16 @@ def session_scope(*, hass=None, session=None):
|
|||
if session is None:
|
||||
raise RuntimeError('Session required')
|
||||
|
||||
need_rollback = False
|
||||
try:
|
||||
yield session
|
||||
session.commit()
|
||||
if session.transaction:
|
||||
need_rollback = True
|
||||
session.commit()
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
_LOGGER.error("Error executing query: %s", err)
|
||||
session.rollback()
|
||||
if need_rollback:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
|
|
Loading…
Reference in New Issue