Skip to content

Commit

Permalink
feat: nested session causes deadlock (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
yenchenLiu authored May 30, 2024
1 parent ba5e41d commit d4eedda
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions casbin_sqlalchemy_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,15 @@ def filter_query(self, querydb, filter):
)
return querydb.order_by(self._db_class.id)

def _save_policy_line(self, ptype, rule):
with self._session_scope() as session:
line = self._db_class(ptype=ptype)
for i, v in enumerate(rule):
setattr(line, "v{}".format(i), v)
def _save_policy_line(self, ptype, rule, session=None):
line = self._db_class(ptype=ptype)
for i, v in enumerate(rule):
setattr(line, "v{}".format(i), v)
if session:
session.add(line)
else:
with self._session_scope() as session:
session.add(line)

def save_policy(self, model):
"""saves all policy rules to the storage."""
Expand All @@ -144,7 +147,7 @@ def save_policy(self, model):
continue
for ptype, ast in model.model[sec].items():
for rule in ast.policy:
self._save_policy_line(ptype, rule)
self._save_policy_line(ptype, rule, session=session)
return True

def add_policy(self, sec, ptype, rule):
Expand Down

0 comments on commit d4eedda

Please # to comment.