Debian Codegraph @

docs/collections/tutorial/implementingmain.html

raw
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>Implementing the Main Program</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB Collections Tutorial" />
10 <link rel="up" href="BasicProgram.html" title="Chapter 2.  The Basic Program" />
11 <link rel="prev" href="createbindingscollections.html" title="Creating Bindings and Collections" />
12 <link rel="next" href="usingtransactions.html" title="Using Transactions" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">
22 Implementing the Main Program
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="createbindingscollections.html">Prev</a> </td>
27 <th width="60%" align="center">Chapter 2. 
28 The Basic Program
29 </th>
30 <td width="20%" align="right"> <a accesskey="n" href="usingtransactions.html">Next</a></td>
31 </tr>
32 </table>
33 <hr />
34 </div>
35 <div class="sect1" lang="en" xml:lang="en">
36 <div class="titlepage">
37 <div>
38 <div>
39 <h2 class="title" style="clear: both"><a id="implementingmain"></a>
40 Implementing the Main Program
41 </h2>
42 </div>
43 </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 <code class="classname">Sample</code> class contains the main program. The skeleton
54 for the <code class="classname">Sample</code> class follows.
55 </p>
56 <a id="cb_java_sample"></a>
57 <pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.db.DatabaseException;
58 import java.io.FileNotFoundException;
59
60 public 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 }</code></strong> </pre>
84 <p>
85 The main program uses the <code class="classname">SampleDatabase</code> and
86 <code class="classname">SampleViews</code> classes that were described in the preceding
87 sections. The <code class="methodname">main</code> method will create an instance of the
88 <code class="classname">Sample</code> class, and call its <code class="methodname">run()</code> and <code class="methodname">close()</code>
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 <strong class="userinput"><code> 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 }</code></strong>
117 ...
118 } </pre>
119 <p>
120 The usage command is:
121 </p>
122 <pre class="programlisting"><strong class="userinput"><code>java com.sleepycat.examples.bdb.shipment.basic.Sample
123 [-h &lt;home-directory&gt; ]</code></strong> </pre>
124 <p>
125 The <code class="literal">-h</code> command is used to set the <code class="filename">homeDir</code>
126 variable, which will later be passed to the <code class="methodname">SampleDatabase()</code>
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 <code class="filename">./tmp</code> — 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 class="xref" href="opendbenvironment.html" title="Opening and Closing the Database Environment">
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 <code class="classname">Sample</code>
151 class and call its <code class="methodname">run()</code> and <code class="methodname">close()</code> methods.
152 </p>
153 <a id="cb_main2"></a>
154 <pre class="programlisting"> public static void main(String args)
155 {
156 ...
157 <strong class="userinput"><code> 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 }</code></strong>
182 } </pre>
183 <p>
184 The <code class="methodname">Sample()</code> constructor will open the environment and
185 databases, and the <code class="methodname">run()</code> 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 <code class="literal">catch</code> statement handles
190 such exceptions.
191 </p>
192 <p>
193 The <code class="literal">finally</code> statement is used to call the <code class="methodname">close()</code>
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 <code class="methodname">Sample()</code> constructor creates the <code class="literal">SampleDatabase</code>
202 and <code class="classname">SampleViews</code> objects.
203 </p>
204 <a id="cb_sample"></a>
205 <pre class="programlisting"> private Sample(String homeDir)
206 throws DatabaseException, FileNotFoundException
207 {
208 <strong class="userinput"><code> db = new SampleDatabase(homeDir);
209 views = new SampleViews(db);</code></strong>
210 } </pre>
211 <p>
212 Recall that creating the <code class="classname">SampleDatabase</code> object will open
213 the environment and all databases.
214 </p>
215 <p>
216 To close the database the <code class="methodname">Sample.close()</code> method simply
217 calls <code class="methodname">SampleDatabase.close()</code>.
218 </p>
219 <a id="cb_sample-close"></a>
220 <pre class="programlisting"> private void close()
221 throws DatabaseException
222 {
223 <strong class="userinput"><code> db.close();</code></strong>
224 } </pre>
225 <p>
226 The <code class="methodname">run()</code> 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>