File: implementingmain.html

package info (click to toggle)
db5.3 5.3.28%2Bdfsg1-0.8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 158,400 kB
  • sloc: ansic: 448,406; java: 111,824; tcl: 80,544; sh: 44,326; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (253 lines) | stat: -rw-r--r-- 9,304 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?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.73.2" />
    <link rel="start" href="index.html" title="Berkeley DB Collections Tutorial" />
    <link rel="up" href="BasicProgram.html" title="Chapter 2.  The Basic Program" />
    <link rel="prev" href="createbindingscollections.html" title="Creating Bindings and Collections" />
    <link rel="next" href="usingtransactions.html" title="Using Transactions" />
  </head>
  <body>
    <div xmlns="" class="navheader">
      <div class="libver">
        <p>Library Version 11.2.5.3</p>
      </div>
      <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>
      <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 <code class="classname">Sample</code> class contains the main program. The skeleton
	for the <code class="classname">Sample</code> class follows.
</p>
      <a id="cb_java_sample"></a>
      <pre class="programlisting"><strong class="userinput"><code>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
    {
    }
}</code></strong> </pre>
      <p>
    The main program uses the <code class="classname">SampleDatabase</code> and
	<code class="classname">SampleViews</code> classes that were described in the preceding
	sections. The <code class="methodname">main</code> method will create an instance of the
	<code class="classname">Sample</code> class, and call its <code class="methodname">run()</code> and <code class="methodname">close()</code>
	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)
    {
<strong class="userinput"><code>        System.out.println("\nRunning sample: " + Sample.class);
        String homeDir = "./tmp";
        for (int i = 0; i &lt; args.length; i += 1)
        {
            String arg = args[i];
            if (args[i].equals("-h") &amp;&amp; i &lt; args.length - 1)
            {
                i += 1;
                homeDir = args[i];
            }
            else
            {
                System.err.println("Usage:\n java " + 
                                   Sample.class.getName() +
                                  "\n  [-h &lt;home-directory&gt;]");
                System.exit(2);
            }
        }</code></strong>
        ...
    } </pre>
      <p>
    The usage command is:
</p>
      <pre class="programlisting"><strong class="userinput"><code>java com.sleepycat.examples.bdb.shipment.basic.Sample
     [-h &lt;home-directory&gt; ]</code></strong> </pre>
      <p>
    The <code class="literal">-h</code> command is used to set the <code class="filename">homeDir</code>
	variable, which will later be passed to the <code class="methodname">SampleDatabase()</code>
	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 <code class="filename">./tmp</code> — 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 class="xref" href="opendbenvironment.html" title="Opening and Closing the Database Environment">
		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 <code class="classname">Sample</code>
	class and call its <code class="methodname">run()</code> and <code class="methodname">close()</code> methods.
</p>
      <a id="cb_main2"></a>
      <pre class="programlisting">    public static void main(String args)
    {
        ...
<strong class="userinput"><code>        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();
                }
            }
        }</code></strong>
    } </pre>
      <p>
    The <code class="methodname">Sample()</code> constructor will open the environment and
	databases, and the <code class="methodname">run()</code> 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 <code class="literal">catch</code> statement handles
	such exceptions.
</p>
      <p>
    The <code class="literal">finally</code> statement is used to call the <code class="methodname">close()</code>
	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 <code class="methodname">Sample()</code> constructor creates the <code class="literal">SampleDatabase</code>
	and <code class="classname">SampleViews</code> objects.
</p>
      <a id="cb_sample"></a>
      <pre class="programlisting">    private Sample(String homeDir)
        throws DatabaseException, FileNotFoundException
    {
<strong class="userinput"><code>        db = new SampleDatabase(homeDir);
        views = new SampleViews(db);</code></strong>
    } </pre>
      <p>
    Recall that creating the <code class="classname">SampleDatabase</code> object will open
	the environment and all databases.
</p>
      <p>
    To close the database the <code class="methodname">Sample.close()</code> method simply
	calls <code class="methodname">SampleDatabase.close()</code>.
</p>
      <a id="cb_sample-close"></a>
      <pre class="programlisting">     private void close()
        throws DatabaseException
    {
<strong class="userinput"><code>        db.close();</code></strong>
    } </pre>
      <p>
    The <code class="methodname">run()</code> 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>