Mercurial > hg > ltpda
diff m-toolbox/classes/@ltpda_uo/update.m @ 21:8be9deffe989 database-connection-manager
Update ltpda_uo.update
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | f0afece42f48 |
children |
line wrap: on
line diff
--- a/m-toolbox/classes/@ltpda_uo/update.m Mon Dec 05 16:20:06 2011 +0100 +++ b/m-toolbox/classes/@ltpda_uo/update.m Mon Dec 05 16:20:06 2011 +0100 @@ -74,60 +74,14 @@ else sinfo = []; end - - % user supplied connection - conn = find(pls, 'conn'); + + % database connection + c = LTPDADatabaseConnectionManager().connect(pls); - if isempty(conn) - % obtain a connection from the connection manager - rm = LTPDARepositoryManager(); - connections = rm.findConnections(pls); - if isempty(connections) - % no connection found. create a new one - conn = rm.newConnection(pls); - if isempty(conn) - [varargout{1}, varargout{2}] = userCanceled(); - return - end - elseif numel(connections) == 1 && ~utils.prog.yes2true(pls.find('use selector')) - % found only one connection and we are not forcing the use of the selector - conn = connections(1); - else - % make the user select the desired database connection - conn = rm.manager.selectConnection([]); - if isempty(conn) - [varargout{1}, varargout{2}] = userCanceled(); - return - end - end - end - - % avoid to ask for a password if one was specified in the plist - if isjava(conn) && ~conn.isConnected() - passwd = pls.find('password'); - if ~isempty(passwd) - conn.setPassword(passwd); - end - end - - % connect to the database - if isempty(conn.openConnection()) - error('### unable to connect to the database') - end - - try - % lock connection to avoid expire during processing - conn.setLocked(true); - + try % look-up user id - userid = utils.jmysql.getUserID(conn); - username = conn.getUsername(); - - utils.helper.msg(msg.PROC1, 'got user id %d for user: %s', userid, char(username)); - if userid < 1 || isnan(userid) || strcmp(userid, 'No Data') || ischar(userid) - error('### Unknown username.'); - end - + [username, userid] = utils.repository.getUser(c); + % author of the data: let's take the username author = username; @@ -137,13 +91,7 @@ % machine details prov = provenance(); - - utils.helper.msg(msg.IMPORTANT, 'submitting to %s@%s:%s', ... - char(conn.getUsername), char(conn.getHostname), char(conn.getDatabase)); - - % obtain the underlying connection - c = conn.getConn(); - + % start a transaction. either we submitt all objects or we roll back all changes c.setAutoCommit(false); @@ -252,32 +200,31 @@ end % update other meta-data tables - cols = utils.jmysql.execute(c, 'SHOW COLUMNS FROM tsdata'); + cols = utils.mysql.execute(c, 'SHOW COLUMNS FROM tsdata'); if utils.helper.ismember('obj_id', cols(:,1)) % the tsdata table contains an obj id column. use the new database schema - utils.jmysql.updateObjMetadata(c, obj, objid); + utils.repository.updateObjMetadata(c, obj, objid); else % otherwise use the old one utils.helper.msg(msg.PROC2, 'using back-compatibility code'); - utils.jmysql.updateObjMetadataV1(c, obj, objid); + utils.repository.updateObjMetadataV1(c, obj, objid); end catch ex - utils.helper.msg(msg.IMPORTANT, 'update error. no object updated') - c.close() - conn.setLocked(false); + % close connection if we own it + if isempty(find(pls, 'conn')) + c.close(); + end rethrow(ex) end % commit the transaction c.commit(); - conn.setLocked(false); - - % reset timer - rm = LTPDARepositoryManager(); - LTPDARepositoryManager.resetTimer(rm.timerClearPass, conn); - LTPDARepositoryManager.resetTimer(rm.timerDisconnect, conn); + % close connection if we own it + if isempty(find(pls, 'conn')) + c.close(); + end end