# HG changeset patch # User Daniele Nicolodi # Date 1323098406 -3600 # Node ID 8be9deffe989f0e3999be71a349bbd99a2907d79 # Parent d58813ab1b92e077b860d3a4ca93c87d8878ce8c Update ltpda_uo.update diff -r d58813ab1b92 -r 8be9deffe989 m-toolbox/classes/@ltpda_uo/update.m --- 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