Skip to content

Commit 33ef072

Browse files
fix: pass config and backend explicitly to Connection constructor
Connection.__init__ now accepts `backend` and `config_override` kwargs instead of reading from the module-level global config. This ensures Instance creates connections using its own config, not the global one. Also removes set_thread_safe() — thread-safe mode is an infrastructure decision set via DJ_THREAD_SAFE env var, not a runtime toggle. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4d16df6 commit 33ef072

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

src/datajoint/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ def conn(
160160
"Database password not configured. Set dj.config['database.password'] or pass password= argument."
161161
)
162162

163-
instance_module._singleton_connection = Connection(host, user, password, port, use_tls)
164-
instance_module._singleton_connection._config = _global_config
163+
instance_module._singleton_connection = Connection(host, user, password, port, use_tls, config_override=_global_config)
165164

166165
return _get_singleton_connection()
167166

src/datajoint/connection.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
import re
1212
import warnings
1313
from contextlib import contextmanager
14+
from typing import TYPE_CHECKING
1415

1516
from . import errors
1617
from .adapters import get_adapter
1718
from .blob import pack, unpack
1819
from .dependencies import Dependencies
1920
from .settings import config
21+
22+
if TYPE_CHECKING:
23+
from .settings import Config
2024
from .version import __version__
2125

2226
logger = logging.getLogger(__name__.split(".")[0])
@@ -163,13 +167,19 @@ def __init__(
163167
password: str,
164168
port: int | None = None,
165169
use_tls: bool | dict | None = None,
170+
*,
171+
backend: str | None = None,
172+
config_override: "Config | None" = None,
166173
) -> None:
174+
# Config reference — use override if provided, else global config
175+
self._config = config_override if config_override is not None else config
176+
167177
if ":" in host:
168178
# the port in the hostname overrides the port argument
169179
host, port = host.split(":")
170180
port = int(port)
171181
elif port is None:
172-
port = config["database.port"]
182+
port = self._config["database.port"]
173183
self.conn_info = dict(host=host, port=port, user=user, passwd=password)
174184
if use_tls is not False:
175185
# use_tls can be: None (auto-detect), True (enable), False (disable), or dict (custom config)
@@ -186,11 +196,9 @@ def __init__(
186196
self._query_cache = None
187197
self._is_closed = True # Mark as closed until connect() succeeds
188198

189-
# Config reference - defaults to global config, but Instance sets its own
190-
self._config = config
191-
192-
# Select adapter based on configured backend
193-
backend = self._config["database.backend"]
199+
# Select adapter: explicit backend > config backend
200+
if backend is None:
201+
backend = self._config["database.backend"]
194202
self.adapter = get_adapter(backend)
195203

196204
self.connect()

src/datajoint/instance.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@
2121

2222
def _load_thread_safe() -> bool:
2323
"""
24-
Load thread_safe setting from environment or config file.
24+
Check if thread-safe mode is enabled.
25+
26+
Thread-safe mode is controlled by the ``DJ_THREAD_SAFE`` environment
27+
variable, which must be set before the process starts.
2528
2629
Returns
2730
-------
2831
bool
2932
True if thread-safe mode is enabled.
3033
"""
31-
# Check environment variable first
3234
env_val = os.environ.get("DJ_THREAD_SAFE", "").lower()
3335
if env_val in ("true", "1", "yes"):
3436
return True
35-
if env_val in ("false", "0", "no"):
36-
return False
37-
38-
# Default: thread-safe mode is off
3937
return False
4038

4139

@@ -104,11 +102,16 @@ def __init__(
104102
if port is None:
105103
port = self.config.database.port
106104

107-
# Create connection
108-
self.connection = Connection(host, user, password, port, use_tls)
109-
110-
# Attach config to connection so tables can access it
111-
self.connection._config = self.config
105+
# Create connection with this instance's config and backend
106+
self.connection = Connection(
107+
host,
108+
user,
109+
password,
110+
port,
111+
use_tls,
112+
backend=self.config.database.backend,
113+
config_override=self.config,
114+
)
112115

113116
def Schema(
114117
self,
@@ -235,9 +238,7 @@ def _get_singleton_connection() -> Connection:
235238
"Database password not configured. Set dj.config['database.password'] or DJ_PASS environment variable."
236239
)
237240

238-
_singleton_connection = Connection(host, user, password, port, use_tls)
239-
# Attach global config to connection
240-
_singleton_connection._config = _global_config
241+
_singleton_connection = Connection(host, user, password, port, use_tls, config_override=_global_config)
241242

242243
return _singleton_connection
243244

0 commit comments

Comments
 (0)