Skip to content

Commit 02de566

Browse files
authored
Merge pull request #166 from datajoint/master-stage
Fix error that triggers when a connection is already established.
2 parents f5aa5c1 + f5b348f commit 02de566

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

+dj/conn.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@
3232

3333

3434
if isa(CONN, 'dj.Connection') && ~reset
35-
assert(nargin==0, ...
36-
'connection already instantiated. To reconnect, clear functions')
35+
if nargin>0
36+
if strcmp(CONN.host,host)
37+
warning('DataJoint:Connection:AlreadyInstantiated', sprintf([...
38+
'Connection already instantiated.\n' ...
39+
'Will use existing connection to "' CONN.host '".\n' ...
40+
'To reconnect, set reset to true']));
41+
else
42+
error('DataJoint:Connection:AlreadyInstantiated', sprintf([...
43+
'Connection already instantiated to "' CONN.host '".\n' ...
44+
'To reconnect, set reset to true']));
45+
end
46+
end
3747
else
3848
% invoke setupDJ
3949
% optional environment variables specifying the connection.

+tests/TestConnection.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,26 @@ function testConnection(testCase)
99
testCase.CONN_INFO.user,...
1010
testCase.CONN_INFO.password,'',true).isConnected);
1111
end
12+
function testConnectionExists(testCase)
13+
% testConnectionExists tests that will not fail if connection open
14+
% to the same host.
15+
% Fix https://github.com/datajoint/datajoint-matlab/issues/160
16+
st = dbstack;
17+
disp(['---------------' st(1).name '---------------']);
18+
dj.conn(testCase.CONN_INFO.host, '', '', '', '', true)
19+
dj.conn(testCase.CONN_INFO.host, '', '', '', '', true)
20+
end
21+
function testConnectionDiffHost(testCase)
22+
% testConnectionDiffHost tests that will fail if connection open
23+
% to a different host.
24+
% Fix https://github.com/datajoint/datajoint-matlab/issues/160
25+
st = dbstack;
26+
disp(['---------------' st(1).name '---------------']);
27+
dj.conn(testCase.CONN_INFO.host, '', '', '', '', true)
28+
29+
testCase.verifyError(@() dj.conn(...
30+
'anything', '', '', '', '', true), ...
31+
'DataJoint:Connection:AlreadyInstantiated');
32+
end
1233
end
1334
end

0 commit comments

Comments
 (0)