Database Reference
In-Depth Information
Here's a tip: to ensure that the database is always shut down
during normal operation of your application, it is recom‐
mended that you set up and tear down the database using a
try/finally block. That is to say, all of your database interac‐
tion should take place inside an encapsulating try block, and
your final collection close and subsequent Database Instance
Manager shutdown call should both happen inside the same
finally block that corresponds to the initial try block. See
Example 13-20 .
Example 13-20. Ensuring clean shutdown when using the XML:DB Local API
Collection coll = null ;
try {
coll =
DatabaseManager . getCollection ( "xmldb:exist:///db" username , password );
//TODO all of your database interaction code is called from here
} finally {
if ( coll != null ) {
final DatabaseInstanceManager manager =
( DatabaseInstanceManager ) coll . getService (
"DatabaseInstanceManager" , "1.0" );
try {
coll . close ();
} finally {
manager . shutdown ();
}
}
}
Example
The source code of a small example of using the XML:DB Local API from Java to
store a file, query the database, and remove a file is included in the folder chapters/
integration/xmldb-embedded of the book-code Git repository (see “Getting the Source
Code” on page 15 ).
To compile the example, enter the xmldb-embedded folder and run mvn package .
XML:DB local example. You can then execute the ExampleApp example like so:
java -jar xmldb-embedded-example/target/xmldb-embedded-example-1.0-example.jar
This shows the available arguments for using the ExampleApp.
A complete example of using the application might look like the following:
Search WWH ::




Custom Search