Difference between revisions of "Tutorial: Quickstart"
Line 62: | Line 62: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== Class Diagrams === | === Class Diagrams === |
Revision as of 09:16, 14 October 2010
Contents
Quickstart
Client opens a database session
To open a client session to a Simantics database server you need to fetch a Driver (javadoc) from the Driver manager (javadoc).
The basic idea is much the same as in JDBC Driver and DriverManager.
<syntaxhighlight lang="java">
// Try to load and register the Driver instance with the driver manager Class.forName("fi.vtt.simantics.procore.ProCoreDriver");
// Connect to the server at the specified host and credentials. Session session = Manager.getSession("procore", InetSocketAddress.createUnresolved("localhost", port), username, password);
</syntaxhighlight>
Reading the database
- Synchronous primitive read
<syntaxhighlight lang="java">
Boolean readBooleanProperty(RequestProcessor processor, final Resource resource, final Resource property) { return processor.syncRequest(new Read<Boolean>() { public Boolean perform(ReadGraph graph) throws DatabaseException { return graph.getRelatedValue(resource, property); } }); }
</syntaxhighlight>
Writing the database
- Synchronous write requests
<syntaxhighlight lang="java">
Resource createLibrary(WriteGraph graph) throws DatabaseException { Builtins b = graph.getBuiltins();
// Create new Library instance. Use claim(s,p,inv,o) // instead of claim(s,p,o) for defining (s, InstanceOf, o) // to not create the inverse statement (o, HasInstance, s). Resource library = graph.newResource(); graph.claim(library, b.InstanceOf, null, b.Library);
// Give the library a name graph.claimValue(library, b.HasName, "New Library");
// Attach the library to the specified container graph.claim(container, b.ConsistsOf, library); }
class CreateLibrary extends AbstractWriteResult<Resource> { Resource container; public CreateLibrary(Resource container) { this.container = container; } public Resource perform(WriteGraph graph) throws DatabaseException { return createLibrary(graph); } }
void createLibrary(RequestProcessor processor, Resource container) { processor.syncRequest(new CreateLibrary(container)); }
</syntaxhighlight>
Class Diagrams
FAQ
Excerpt from RequestProcessor javadoc:
A client invoking any of the
syncRequest
-methods in this interface must not assume that the request is performed within the same thread that performed thesyncRequest
invocation. This is an implementation-specific matter.Granted, before 2009 the implementation was such that it would execute synchronous requests within the invoking thread but it is now specified as seen above. Note that you cannot synchronously schedule access to another thread and use the original ReadGraph/AsyncReadGraph from there since database access is tied to the dedicated threads assigned for it.
The current DB client implementation does properly support write request rollback, which unfortunately causes the DB client's view of the database to be corrupted. Also if your write operation is not small, even the server's view of the database may be corrupted. At the moment it looks like resolving this issue will be delayed until Simantics 1.2. Until then, the Simantics database cannot be considered for continuous use in any real-life applications.