Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
fixed caching issue in sql connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Toan Quach authored and Toan Quach committed Nov 14, 2023
1 parent e12c666 commit ae80b16
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
17 changes: 8 additions & 9 deletions src/taipy/core/_repository/db/_sql_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
from ...exceptions import MissingRequiredProperty


def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d


class _SQLConnection:
_connection = None

@classmethod
def dict_factory(cls, cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d

@classmethod
def init_db(cls):
if cls._connection:
return cls._connection

cls._connection = _build_connection()
cls._connection.row_factory = cls.dict_factory
cls._connection.row_factory = dict_factory

from ..._version._version_model import _VersionModel
from ...cycle._cycle_model import _CycleModel
Expand Down Expand Up @@ -68,7 +68,6 @@ def init_db(cls):
return cls._connection


@lru_cache
def _build_connection() -> Connection:
# Set SQLite threading mode to Serialized, means that threads may share the module, connections and cursors
sqlite3.threadsafety = 3
Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from src.taipy.core._core import Core
from src.taipy.core._orchestrator._orchestrator_factory import _OrchestratorFactory
from src.taipy.core._repository.db._sql_connection import _build_connection
from src.taipy.core._repository.db._sql_connection import _build_connection, _SQLConnection
from src.taipy.core._version._version import _Version
from src.taipy.core._version._version_manager_factory import _VersionManagerFactory
from src.taipy.core._version._version_model import _VersionModel
Expand Down Expand Up @@ -445,7 +445,8 @@ def init_sql_repo(tmp_sqlite):
Config.configure_core(repository_type="sql", repository_properties={"db_location": tmp_sqlite})

# Clean SQLite database
connection = _build_connection()
_SQLConnection._connection = None
connection = _SQLConnection.init_db()
connection.execute(str(DropTable(_CycleModel.__table__, if_exists=True).compile(dialect=sqlite.dialect())))
connection.execute(str(DropTable(_DataNodeModel.__table__, if_exists=True).compile(dialect=sqlite.dialect())))
connection.execute(str(DropTable(_JobModel.__table__, if_exists=True).compile(dialect=sqlite.dialect())))
Expand Down
9 changes: 0 additions & 9 deletions tests/core/job/test_job_manager_with_sql_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ def init_managers():
_JobManagerFactory._build_manager()._delete_all()


def clear_sql_session():
_build_connection.cache_clear()
_SQLConnection._connection = None


def test_create_jobs(init_sql_repo):
Config.configure_job_executions(mode=JobConfig._DEVELOPMENT_MODE)
init_managers()
Expand Down Expand Up @@ -165,8 +160,6 @@ def test_delete_job(init_sql_repo):


def test_raise_when_trying_to_delete_unfinished_job(init_sql_repo):
clear_sql_session()

Config.configure_job_executions(mode=JobConfig._STANDALONE_MODE, max_nb_of_workers=2)
init_managers()

Expand Down Expand Up @@ -196,8 +189,6 @@ def test_raise_when_trying_to_delete_unfinished_job(init_sql_repo):


def test_force_deleting_unfinished_job(init_sql_repo):
clear_sql_session()

Config.configure_job_executions(mode=JobConfig._STANDALONE_MODE, max_nb_of_workers=2)
init_managers()

Expand Down

0 comments on commit ae80b16

Please sign in to comment.