@@ -103,17 +103,18 @@ def conn(
103103 """
104104 Return a persistent connection object.
105105
106- When called without arguments, returns the singleton connection.
107- When connection parameters are provided, creates a new Connection.
106+ When called without arguments, returns the singleton connection using
107+ credentials from dj.config. When connection parameters are provided,
108+ updates the singleton connection with the new credentials.
108109
109110 Parameters
110111 ----------
111112 host : str, optional
112- Database hostname.
113+ Database hostname. If provided, updates singleton.
113114 user : str, optional
114- Database username.
115+ Database username. If provided, updates singleton.
115116 password : str, optional
116- Database password.
117+ Database password. If provided, updates singleton.
117118 reset : bool, optional
118119 If True, reset existing connection. Default False.
119120 use_tls : bool or dict, optional
@@ -127,15 +128,38 @@ def conn(
127128 Raises
128129 ------
129130 ThreadSafetyError
130- If thread_safe mode is enabled and using singleton .
131+ If thread_safe mode is enabled.
131132 """
132- # If any connection params provided, use legacy behavior
133- if host is not None or user is not None or password is not None or reset :
134- from .connection import conn as _legacy_conn
133+ from .instance import _singleton_connection , _check_thread_safe , _global_config
134+ import datajoint .instance as instance_module
135135
136- return _legacy_conn (host , user , password , reset = reset , use_tls = use_tls )
136+ _check_thread_safe ()
137+
138+ # If credentials provided or reset requested, (re)create the singleton
139+ if host is not None or user is not None or password is not None or reset :
140+ # Use provided values or fall back to config
141+ host = host if host is not None else _global_config .database .host
142+ user = user if user is not None else _global_config .database .user
143+ password = password if password is not None else _global_config .database .password
144+ if password is not None and hasattr (password , 'get_secret_value' ):
145+ password = password .get_secret_value ()
146+ port = _global_config .database .port
147+ use_tls = use_tls if use_tls is not None else _global_config .database .use_tls
148+
149+ if user is None :
150+ from .errors import DataJointError
151+ raise DataJointError (
152+ "Database user not configured. Set dj.config['database.user'] or pass user= argument."
153+ )
154+ if password is None :
155+ from .errors import DataJointError
156+ raise DataJointError (
157+ "Database password not configured. Set dj.config['database.password'] or pass password= argument."
158+ )
159+
160+ instance_module ._singleton_connection = Connection (host , user , password , port , use_tls )
161+ instance_module ._singleton_connection ._config = _global_config
137162
138- # Otherwise use singleton connection
139163 return _get_singleton_connection ()
140164
141165
0 commit comments