UserAPI » History » Version 1
Paula Gearon, 05/15/2008 08:24 PM
1 | 1 | Paula Gearon | = Connections = |
---|---|---|---|
2 | Mulgara now supports a new interface called a `Connection`. This will be replacing the current `ItqlInterpreterBean` interface as the full suite of utilities becomes available. |
||
3 | |||
4 | The existing `ItqlInterpreterBean` interface provides a completely automated connection mechanism, based on the URLs of graphs referenced in TQL queries. While convenient, this has several undesirable effects: |
||
5 | * All queries issued to this interface must contain at least one URL for a graph on the server that will handle the query. |
||
6 | * Arbitrary graph names are not permitted. Graph names must follow a particular format, and will include the hostname of the server containing the graph. |
||
7 | * Queries against graphs must be changed when the server hosting the graph changes. |
||
8 | * Connections to a server cannot be cached. |
||
9 | * Distributed queries (queries where the requested data comes from more than one server) are sent to an arbitrary server and not necessarily the most desirable one. |
||
10 | * Query languages which use a default graph name (like [http://www.w3.org/TR/rdf-sparql-query/ SPARQL]) cannot identify a server if no graph names are given. |
||
11 | |||
12 | The `Connection` interface avoids these issues by establishing a link to a specified server. This link can be to a server running in the current process, over inter-process-communication (IPC) to a server running on the same machine, or across a network. Queries and commands can be sent along this link, and all operations will be performed by the server that has been connected to. |
||
13 | |||
14 | The `Connection` interface and its supporting classes are found in the `org.mulgara.connection` package. |
||
15 | |||
16 | == Connection Factory == |
||
17 | While Connections can be created directly, the `ConnectionFactory` class attempts to manage connections to ensure that the correct type is created, and reduce network overhead. The connections returned by a `ConnectionFactory` are only managed by that factory, and will not interfere with connections from a separate factory instance. |
||
18 | |||
19 | `Connection`s can be established based on the URL of a database (in the form ''protocol''://''host''/''service'') or by wrapping a `Session` instance. The `Session` interface has existed in Mulgara for some time, and it may be more appropriate for existing code to use its internal `Session` instances to create new `Connection`s. |
||
20 | |||
21 | For the moment, the only supported protocol is ''rmi''. Other protocols, including ''http'' are expected shortly. |
||
22 | |||
23 | == Using a Connection == |
||
24 | Once a `Connection` has been created, commands can be sent on it using the `execute` method. Alternatively, commands also have their own `execute` method that can be applied to a connection, with the same effect. |
||
25 | |||
26 | The following is an example of using a Connection to create a graph, and load a file into that graph: |
||
27 | {{{ |
||
28 | #!java |
||
29 | // define the server we want to connect to |
||
30 | URI serverURI = new URI("rmi://localhost/server1"); |
||
31 | // define the file we want to load |
||
32 | URI dataFile = new File("data.rdf").toURI(); |
||
33 | // define the name of the graph to load the data into |
||
34 | URI myData = new URI("test:data"); |
||
35 | |||
36 | // Create a factory, and connect to the server |
||
37 | ConnectionFactory factory = new ConnectionFactory(); |
||
38 | Connection connection = factory.newConnection(serverUri); |
||
39 | |||
40 | // execute a CREATE command |
||
41 | connection.execute(new CreateGraph(myData)); |
||
42 | // execute a LOAD command |
||
43 | connection.execute(new Load(dataFile, myData, true)); |
||
44 | |||
45 | // cleaning up the connection allows the network resources to be re-used |
||
46 | connection.close(); |
||
47 | }}} |