1<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml">
4  <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6    <title>
7		Implementing the Main Program
8	</title>
9    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
10    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
11    <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
12    <link rel="up" href="BasicProgram.html" title="Chapter 2. &#10;&#9;&#9;The Basic Program&#10;&#9;" />
13    <link rel="previous" href="createbindingscollections.html" title="&#10;&#9;&#9;Creating Bindings and Collections&#10;&#9;" />
14    <link rel="next" href="usingtransactions.html" title="&#10;&#9;&#9;Using Transactions&#10;&#9;" />
15  </head>
16  <body>
17    <div class="navheader">
18      <table width="100%" summary="Navigation header">
19        <tr>
20          <th colspan="3" align="center">
21		Implementing the Main Program
22	</th>
23        </tr>
24        <tr>
25          <td width="20%" align="left"><a accesskey="p" href="createbindingscollections.html">Prev</a> </td>
26          <th width="60%" align="center">Chapter 2. 
27		The Basic Program
28	</th>
29          <td width="20%" align="right"> <a accesskey="n" href="usingtransactions.html">Next</a></td>
30        </tr>
31      </table>
32      <hr />
33    </div>
34    <div class="sect1" lang="en" xml:lang="en">
35      <div class="titlepage">
36        <div>
37          <div>
38            <h2 class="title" style="clear: both"><a id="implementingmain"></a>
39		Implementing the Main Program
40	</h2>
41          </div>
42        </div>
43        <div></div>
44      </div>
45      <p>
46    The main program opens the environment and databases, stores and retrieves
47	objects within a transaction, and finally closes the environment
48	databases. This section describes the main program shell, and the
49	next section describes how to run transactions for storing and
50	retrieving objects.
51</p>
52      <p>
53    The <tt class="classname">Sample</tt> class contains the main program. The skeleton
54	for the <tt class="classname">Sample</tt> class follows.
55</p>
56      <a id="cb_java_sample"></a>
57      <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.db.DatabaseException;
58import java.io.FileNotFoundException;
59
60public class Sample
61{
62    private SampleDatabase db;
63    private SampleViews views;
64
65    public static void main(String args)
66    {
67    }
68
69    private Sample(String homeDir)
70        throws DatabaseException, FileNotFoundException
71    {
72    }
73
74    private void close()
75        throws DatabaseException
76    {
77    }
78
79    private void run()
80        throws Exception
81    {
82    }
83}</tt></b> </pre>
84      <p>
85    The main program uses the <tt class="classname">SampleDatabase</tt> and
86	<tt class="classname">SampleViews</tt> classes that were described in the preceding
87	sections. The <tt class="methodname">main</tt> method will create an instance of the
88	<tt class="classname">Sample</tt> class, and call its <tt class="methodname">run()</tt> and <tt class="methodname">close()</tt>
89	methods.
90    
91</p>
92      <p>
93    The following statements parse the program's command line
94	arguments.
95</p>
96      <a id="cb_main"></a>
97      <pre class="programlisting">    public static void main(String[] args)
98    {
99<b class="userinput"><tt>        System.out.println("\nRunning sample: " + Sample.class);
100        String homeDir = "/tmp";
101        for (int i = 0; i &lt; args.length; i += 1)
102        {
103            String arg = args[i];
104            if (args[i].equals("-h") &amp;&amp; i &lt; args.length - 1)
105            {
106                i += 1;
107                homeDir = args[i];
108            }
109            else
110            {
111                System.err.println("Usage:\n java " + 
112                                   Sample.class.getName() +
113                                  "\n  [-h &lt;home-directory&gt;]");
114                System.exit(2);
115            }
116        }</tt></b>
117        ...
118    } </pre>
119      <p>
120    The usage command is:
121</p>
122      <pre class="programlisting"><b class="userinput"><tt>java com.sleepycat.examples.bdb.shipment.basic.Sample
123     [-h &lt;home-directory&gt; ]</tt></b> </pre>
124      <p>
125    The <tt class="literal">-h</tt> command is used to set the <tt class="filename">homeDir</tt>
126	variable, which will later be passed to the <tt class="methodname">SampleDatabase()</tt>
127	constructor. Normally all Berkeley DB programs should provide a way
128	to configure their database environment home directory.
129</p>
130      <p>
131    The default for the home directory is <tt class="filename">/tmp</tt> — the tmp
132	subdirectory of the current directory where the sample is run. The
133	home directory must exist before running the sample. To re-create
134	the sample database from scratch, delete all files in the home
135	directory before running the sample.
136</p>
137      <p>
138    The home directory was described previously in
139    <a href="opendbenvironment.html">
140		Opening and Closing the Database Environment
141	</a>.
142</p>
143      <p>
144    Of course, the command line arguments shown are only examples
145	and a real-life application may use different techniques for
146	configuring these options.
147    
148</p>
149      <p>
150    The following statements create an instance of the <tt class="classname">Sample</tt>
151	class and call its <tt class="methodname">run()</tt> and <tt class="methodname">close()</tt> methods.
152</p>
153      <a id="cb_main2"></a>
154      <pre class="programlisting">    public static void main(String args)
155    {
156        ...
157<b class="userinput"><tt>        Sample sample = null;
158        try
159        {
160            sample = new Sample(homeDir);
161            sample.run();
162        }
163        catch (Exception e)
164        {
165            e.printStackTrace();
166        }
167        finally
168        {
169            if (sample != null)
170            {
171                try
172                {
173                    sample.close();
174                }
175                catch (Exception e)
176                {
177                    System.err.println("Exception during database close:");
178                    e.printStackTrace();
179                }
180            }
181        }</tt></b>
182    } </pre>
183      <p>
184    The <tt class="methodname">Sample()</tt> constructor will open the environment and
185	databases, and the <tt class="methodname">run()</tt> method will run transactions for
186	storing and retrieving objects. If either of these throws an
187	exception, then the program was unable to run and should normally
188	terminate. (Transaction retries are handled at a lower level and
189	will be described later.) The first <tt class="literal">catch</tt> statement handles
190	such exceptions.
191</p>
192      <p>
193    The <tt class="literal">finally</tt> statement is used to call the <tt class="methodname">close()</tt>
194	method since an attempt should always be made to close the environment and
195    databases
196	cleanly. If an exception is thrown during close and a prior
197	exception occurred above, then the exception during close is likely
198	a side effect of the prior exception.
199</p>
200      <p>
201    The <tt class="methodname">Sample()</tt> constructor creates the <tt class="literal">SampleDatabase</tt>
202	and <tt class="classname">SampleViews</tt> objects.
203</p>
204      <a id="cb_sample"></a>
205      <pre class="programlisting">    private Sample(String homeDir)
206        throws DatabaseException, FileNotFoundException
207    {
208<b class="userinput"><tt>        db = new SampleDatabase(homeDir);
209        views = new SampleViews(db);</tt></b>
210    } </pre>
211      <p>
212    Recall that creating the <tt class="classname">SampleDatabase</tt> object will open
213	the environment and all databases.
214</p>
215      <p>
216    To close the database the <tt class="methodname">Sample.close()</tt> method simply
217	calls <tt class="methodname">SampleDatabase.close()</tt>.
218</p>
219      <a id="cb_sample-close"></a>
220      <pre class="programlisting">     private void close()
221        throws DatabaseException
222    {
223<b class="userinput"><tt>        db.close();</tt></b>
224    } </pre>
225      <p>
226    The <tt class="methodname">run()</tt> method is described in the next section.
227</p>
228    </div>
229    <div class="navfooter">
230      <hr />
231      <table width="100%" summary="Navigation footer">
232        <tr>
233          <td width="40%" align="left"><a accesskey="p" href="createbindingscollections.html">Prev</a> </td>
234          <td width="20%" align="center">
235            <a accesskey="u" href="BasicProgram.html">Up</a>
236          </td>
237          <td width="40%" align="right"> <a accesskey="n" href="usingtransactions.html">Next</a></td>
238        </tr>
239        <tr>
240          <td width="40%" align="left" valign="top">
241		Creating Bindings and Collections
242	 </td>
243          <td width="20%" align="center">
244            <a accesskey="h" href="index.html">Home</a>
245          </td>
246          <td width="40%" align="right" valign="top"247		Using Transactions
248	</td>
249        </tr>
250      </table>
251    </div>
252  </body>
253</html>
254