annotate m-toolbox/classes/+utils/@mysql/connect.m @ 40:977eb37f31cb database-connection-manager

User friendlier errors from utils.mysql.connect
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 18:04:03 +0100
parents 18e956c96a1b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
1 function conn = connect(hostname, database, username, password)
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
2 % CONNECT Opens a connection to the given database.
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
3 %
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
4 % CALL:
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
5 %
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
6 % conn = utils.mysql.connect(hostname, database, username, password)
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
7 %
40
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
8 % This function returns a Java java.sql.Connection object.
2
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
9 %
40
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
10 % On authetication error an utils:mysql:connect:AccessDenied exception
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
11 % is thrown. On other errors an utils:mysql:connect:ConnectionError
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
12 % exception is thrown.
2
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
13
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
14 % informative message
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
15 import utils.const.*
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
16 utils.helper.msg(msg.PROC1, 'connection to mysql://%s/%s username=%s', hostname, database, username);
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
17
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
18 % connection credential
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
19 uri = sprintf('jdbc:mysql://%s/%s', hostname, database);
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
20 db = javaObject('com.mysql.jdbc.Driver');
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
21 pl = javaObject('java.util.Properties');
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
22 pl.setProperty(db.USER_PROPERTY_KEY, username);
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
23 pl.setProperty(db.PASSWORD_PROPERTY_KEY, password);
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
24
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
25 try
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
26 % connect
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
27 conn = db.connect(uri, pl);
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
28 catch ex
40
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
29 % exceptions handling in matlab sucks
2
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
30 if strcmp(ex.identifier, 'MATLAB:Java:GenericException')
40
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
31 % extract exception class and message
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
32 lines = regexp(ex.message, '\n', 'split');
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
33 p = strfind(lines{2}, ': ');
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
34 id = lines{2}(1:p(1)-1);
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
35 message = lines{2}(p(1)+2:end);
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
36 % some notable cases
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
37 switch id
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
38 case 'java.sql.SQLException'
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
39 throwAsCaller(MException('utils:mysql:connect:AccessDenied', '### access denied: %s', message))
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
40 case 'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException'
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
41 throwAsCaller(MException('utils:mysql:connect:ConnectionError', '### connection error: %s. check database name', message))
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
42 case 'com.mysql.jdbc.exceptions.jdbc4.CommunicationsException'
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
43 throwAsCaller(MException('utils:mysql:connect:ConnectionError', '### connection error: %s. check hostname', message))
2
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
44 end
40
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
45 % user friendlier exception
977eb37f31cb User friendlier errors from utils.mysql.connect
Daniele Nicolodi <nicolodi@science.unitn.it>
parents: 2
diff changeset
46 throwAsCaller(MException('utils:mysql:connect:ConnectionError', '### connection error: %s: %s', id, message))
2
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
47 end
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
48 rethrow(ex);
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
49 end
18e956c96a1b Add LTPDADatabaseConnectionManager implementation. Matlab code
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff changeset
50 end