Connecting to an LTPDA Repository


Connection to a LTPDA Repository is normally carried out as part of other processes. For example, when submitting an object to the repository using the submit command, the user is prompted to login to a chosen repository.

It is also possible to connect to a LTPDA Repository through a MATLAB programming API which may be used in scripts and user functions. The obtained connection object can be passed to other methods requiring access to a LTPDA Repository (for example the submit method) thus allowing the construction of more automated procedures for interacting with LTPDA Repositories. This is done specifying a conn parameter in the method plist instead of the connection parameters.

Connections to a LTPDA Repository can be obtained using the LTPDA Database Connection Manager which is responsible for caching connection credentilas accordingly to the user preferences and for limiting the number of simultaneously open connections.

A connection is obtained with the method:

    
      % Connect to LTPDA Repository database
      LTPDADatabaseConnectionManager().connect(pl)
where pl is a plist with the parameters hostname, database, username, password. All those parameters are optional, if missing the database connection manager will prompt the user for them. It is responsibility of the caller of the connect method to close the connection, with the close of the returned connection object method, once it has finished to use it.

Alternatively pl may contain a conn parameter specifying a connection object to use (this feature is useful to chain multiple operations through a single connection). If the specified parameter is not an object of the proper kind an error is thrown. In this case the caller of the connect method should not close the connection.

The preferred way to close the connection, accordingly to the rules expressed above, is to use an onCleanup handler, in the following way:

    
      % Register onCleanup callback to close database connection
      if ~isempty(find(pl, 'conn'))
        cleanup = onCleanup(@()conn.close());
      end
where pl is the plist parameter of the connect method and conn is the returned connection.

The connection object returned by the LTPDA Database Connection Manager is a Java object implementing the java.sql.Connection interface, and therefore it may be used accordingly to the Java language documentation.

For convenience, the utility function utils.mysql.execute is provided. It may be used to issue SQL commands to the database, obtaining the results in a convenient format. For data manipulation SQL statements (such as INSERT, UPDATE, and DELETE statements) it returns the number of affected rows, for data query SQL statements (such as SELECT statements) it returns a 2D cell array with the query result.



©LTP Team