Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: optimize trigger long running read transactions (#35046)

hj24815c536e050f
diff --git a/api/services/credit_pool_service.py b/api/services/credit_pool_service.py
index 16788300d3a..2d210db1212 100644
--- a/api/services/credit_pool_service.py
+++ b/api/services/credit_pool_service.py
@@ -29,14 +29,15 @@ class CreditPoolService:
@classmethod
def get_pool(cls, tenant_id: str, pool_type: str = "trial") -> TenantCreditPool | None:
"""get tenant credit pool"""
- return db.session.scalar(
- select(TenantCreditPool)
- .where(
- TenantCreditPool.tenant_id == tenant_id,
- TenantCreditPool.pool_type == pool_type,
+ with sessionmaker(db.engine, expire_on_commit=False).begin() as session:
+ return session.scalar(
+ select(TenantCreditPool)
+ .where(
+ TenantCreditPool.tenant_id == tenant_id,
+ TenantCreditPool.pool_type == pool_type,
+ )
+ .limit(1)
)
- .limit(1)
- )
@classmethod
def check_credits_available(
diff --git a/api/tasks/async_workflow_tasks.py b/api/tasks/async_workflow_tasks.py
index 45e1f80e359..9ff34c7c48b 100644
--- a/api/tasks/async_workflow_tasks.py
+++ b/api/tasks/async_workflow_tasks.py
@@ -162,7 +162,12 @@ def _execute_workflow_common(
state_owner_user_id=workflow.created_by,
)
- # Execute the workflow with the trigger type
+ # NOTE (hj24)
+ # Release the transaction before the blocking generate() call,
+ # otherwise the connection stays "idle in transaction" for hours.
+ session.commit()
+ # NOTE END
+
generator.generate(
app_model=app_model,
workflow=workflow,