Skip to content

Commit c8eec84

Browse files
Add check if connection request to same or diff host.
1 parent e45b411 commit c8eec84

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

+dj/conn.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@
3333

3434
if isa(CONN, 'dj.Connection') && ~reset
3535
if nargin>0
36-
warning(sprintf(['Connection already instantiated.\n' ...
37-
'Will use existing connection to "' CONN.host '".\n' ...
38-
'To reconnect, set reset to true']));
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
3946
end
4047
else
4148
% invoke setupDJ

+tests/TestConnection.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ function testConnection(testCase)
1010
testCase.CONN_INFO.password,'',true).isConnected);
1111
end
1212
function testConnectionExists(testCase)
13-
% testConnectionExists tests that will not fail if connection open.
13+
% testConnectionExists tests that will not fail if connection open
14+
% to the same host.
1415
% Fix https://github.com/datajoint/datajoint-matlab/issues/160
1516
st = dbstack;
1617
disp(['---------------' st(1).name '---------------']);
1718
dj.conn(testCase.CONN_INFO.host, '', '', '', '', true)
1819
dj.conn(testCase.CONN_INFO.host, '', '', '', '', true)
1920
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
2033
end
2134
end

0 commit comments

Comments
 (0)