%PDF- %PDF-
Direktori : /usr/share/doc/db4-devel-4.7.25/collections/tutorial/ |
Current File : //usr/share/doc/db4-devel-4.7.25/collections/tutorial/implementingmain.html |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title> Implementing the Main Program </title> <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" /> <link rel="up" href="BasicProgram.html" title="Chapter 2. 		The Basic Program 	" /> <link rel="previous" href="createbindingscollections.html" title=" 		Creating Bindings and Collections 	" /> <link rel="next" href="usingtransactions.html" title=" 		Using Transactions 	" /> </head> <body> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center"> Implementing the Main Program </th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="createbindingscollections.html">Prev</a> </td> <th width="60%" align="center">Chapter 2. The Basic Program </th> <td width="20%" align="right"> <a accesskey="n" href="usingtransactions.html">Next</a></td> </tr> </table> <hr /> </div> <div class="sect1" lang="en" xml:lang="en"> <div class="titlepage"> <div> <div> <h2 class="title" style="clear: both"><a id="implementingmain"></a> Implementing the Main Program </h2> </div> </div> <div></div> </div> <p> The main program opens the environment and databases, stores and retrieves objects within a transaction, and finally closes the environment databases. This section describes the main program shell, and the next section describes how to run transactions for storing and retrieving objects. </p> <p> The <tt class="classname">Sample</tt> class contains the main program. The skeleton for the <tt class="classname">Sample</tt> class follows. </p> <a id="cb_java_sample"></a> <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.db.DatabaseException; import java.io.FileNotFoundException; public class Sample { private SampleDatabase db; private SampleViews views; public static void main(String args) { } private Sample(String homeDir) throws DatabaseException, FileNotFoundException { } private void close() throws DatabaseException { } private void run() throws Exception { } }</tt></b> </pre> <p> The main program uses the <tt class="classname">SampleDatabase</tt> and <tt class="classname">SampleViews</tt> classes that were described in the preceding sections. The <tt class="methodname">main</tt> method will create an instance of the <tt class="classname">Sample</tt> class, and call its <tt class="methodname">run()</tt> and <tt class="methodname">close()</tt> methods. </p> <p> The following statements parse the program's command line arguments. </p> <a id="cb_main"></a> <pre class="programlisting"> public static void main(String[] args) { <b class="userinput"><tt> System.out.println("\nRunning sample: " + Sample.class); String homeDir = "./tmp"; for (int i = 0; i < args.length; i += 1) { String arg = args[i]; if (args[i].equals("-h") && i < args.length - 1) { i += 1; homeDir = args[i]; } else { System.err.println("Usage:\n java " + Sample.class.getName() + "\n [-h <home-directory>]"); System.exit(2); } }</tt></b> ... } </pre> <p> The usage command is: </p> <pre class="programlisting"><b class="userinput"><tt>java com.sleepycat.examples.bdb.shipment.basic.Sample [-h <home-directory> ]</tt></b> </pre> <p> The <tt class="literal">-h</tt> command is used to set the <tt class="filename">homeDir</tt> variable, which will later be passed to the <tt class="methodname">SampleDatabase()</tt> constructor. Normally all Berkeley DB programs should provide a way to configure their database environment home directory. </p> <p> The default for the home directory is <tt class="filename">./tmp</tt> — the tmp subdirectory of the current directory where the sample is run. The home directory must exist before running the sample. To re-create the sample database from scratch, delete all files in the home directory before running the sample. </p> <p> The home directory was described previously in <a href="opendbenvironment.html"> Opening and Closing the Database Environment </a>. </p> <p> Of course, the command line arguments shown are only examples and a real-life application may use different techniques for configuring these options. </p> <p> The following statements create an instance of the <tt class="classname">Sample</tt> class and call its <tt class="methodname">run()</tt> and <tt class="methodname">close()</tt> methods. </p> <a id="cb_main2"></a> <pre class="programlisting"> public static void main(String args) { ... <b class="userinput"><tt> Sample sample = null; try { sample = new Sample(homeDir); sample.run(); } catch (Exception e) { e.printStackTrace(); } finally { if (sample != null) { try { sample.close(); } catch (Exception e) { System.err.println("Exception during database close:"); e.printStackTrace(); } } }</tt></b> } </pre> <p> The <tt class="methodname">Sample()</tt> constructor will open the environment and databases, and the <tt class="methodname">run()</tt> method will run transactions for storing and retrieving objects. If either of these throws an exception, then the program was unable to run and should normally terminate. (Transaction retries are handled at a lower level and will be described later.) The first <tt class="literal">catch</tt> statement handles such exceptions. </p> <p> The <tt class="literal">finally</tt> statement is used to call the <tt class="methodname">close()</tt> method since an attempt should always be made to close the environment and databases cleanly. If an exception is thrown during close and a prior exception occurred above, then the exception during close is likely a side effect of the prior exception. </p> <p> The <tt class="methodname">Sample()</tt> constructor creates the <tt class="literal">SampleDatabase</tt> and <tt class="classname">SampleViews</tt> objects. </p> <a id="cb_sample"></a> <pre class="programlisting"> private Sample(String homeDir) throws DatabaseException, FileNotFoundException { <b class="userinput"><tt> db = new SampleDatabase(homeDir); views = new SampleViews(db);</tt></b> } </pre> <p> Recall that creating the <tt class="classname">SampleDatabase</tt> object will open the environment and all databases. </p> <p> To close the database the <tt class="methodname">Sample.close()</tt> method simply calls <tt class="methodname">SampleDatabase.close()</tt>. </p> <a id="cb_sample-close"></a> <pre class="programlisting"> private void close() throws DatabaseException { <b class="userinput"><tt> db.close();</tt></b> } </pre> <p> The <tt class="methodname">run()</tt> method is described in the next section. </p> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="createbindingscollections.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="BasicProgram.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="usingtransactions.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top"> Creating Bindings and Collections </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> Using Transactions </td> </tr> </table> </div> </body> </html>