-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.html
394 lines (296 loc) · 14.6 KB
/
DB.html
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.7.4 at 2021-09-25
| Rendered using Apache Maven Fluido Skin 1.5
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="Date-Revision-yyyymmdd" content="20210925" />
<meta http-equiv="Content-Language" content="en" />
<title>JDBDT – Database handles</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-1.5.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script type="text/javascript" src="./js/apache-maven-fluido-1.5.min.js"></script>
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<div id="banner">
<div class="pull-left">
<a href="./" id="bannerLeft">
<img src="images/jdbdt-logo.png" alt="JDBDT"/>
</a>
</div>
<div class="pull-right"> </div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2021-09-25
<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 1.4.1
</li>
</ul>
</div>
<div class="row-fluid">
<div id="leftColumn" class="span2">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">JDBDT</li>
<li>
<a href="index.html" title="Home">
<span class="none"></span>
Home</a>
</li>
<li>
<a href="MIT_License.html" title="License">
<span class="none"></span>
License</a>
</li>
<li class="nav-header">Reference</li>
<li>
<a href="Facade.html" title="API facade">
<span class="none"></span>
API facade</a>
</li>
<li class="active">
<a href="#"><span class="none"></span>Database handles</a>
</li>
<li>
<a href="DataSources.html" title="Data sources">
<span class="none"></span>
Data sources</a>
</li>
<li>
<a href="DataSets.html" title="Data sets">
<span class="none"></span>
Data sets</a>
</li>
<li>
<a href="DBSetup.html" title="Database setup">
<span class="none"></span>
Database setup</a>
</li>
<li>
<a href="DBAssertions.html" title="Assertions">
<span class="none"></span>
Assertions</a>
</li>
<li>
<a href="Logs.html" title="Logging format">
<span class="none"></span>
Logging format</a>
</li>
<li>
<a href="Compatibility.html" title="Compatibility">
<span class="none"></span>
Compatibility</a>
</li>
<li>
<a href="apidocs/index.html" title="Javadoc">
<span class="none"></span>
Javadoc</a>
</li>
<li class="nav-header">Tutorial</li>
<li>
<a href="Tutorial.html" title="Guide">
<span class="none"></span>
Guide</a>
</li>
<li>
<a href="http://github.com/JDBDT/jdbdt-tutorial" class="externalLink" title="Code @ GitHub">
<span class="none"></span>
Code @ GitHub</a>
</li>
<li class="nav-header">Development</li>
<li>
<a href="http://search.maven.org/#search%7Cga%7C1%7Cjdbdt" class="externalLink" title="Maven Central">
<span class="none"></span>
Maven Central</a>
</li>
<li>
<a href="https://github.com/JDBDT/jdbdt/" class="externalLink" title="Source code">
<span class="none"></span>
Source code</a>
</li>
<li>
<a href="https://github.com/JDBDT/jdbdt/issues/" class="externalLink" title="Issue tracker">
<span class="none"></span>
Issue tracker</a>
</li>
<li>
<a href="https://travis-ci.org/JDBDT/jdbdt/" class="externalLink" title="Travis CI">
<span class="none"></span>
Travis CI</a>
</li>
<li>
<a href="https://ci.appveyor.com/project/edrdo/jdbdt" class="externalLink" title="AppVeyor CI">
<span class="none"></span>
AppVeyor CI</a>
</li>
<li>
<a href="https://sonarcloud.io/organizations/jdbdt/projects" class="externalLink" title="SonarCloud">
<span class="none"></span>
SonarCloud</a>
</li>
<li>
<a href="https://www.codefactor.io/repository/github/jdbdt/jdbdt" class="externalLink" title="CodeFactor">
<span class="none"></span>
CodeFactor</a>
</li>
<li>
<a href="project-info.html" title="Maven reports">
<span class="none"></span>
Maven reports</a>
</li>
<li>
<a href="Inception.html" title="Inception">
<span class="none"></span>
Inception</a>
</li>
</ul>
<hr />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" />
</a>
</div>
</div>
</div>
<div id="bodyColumn" class="span10" >
<h1>Database handles</h1>
<p>A database handle encapsulates access to a database connection.</p>
<p> <a name="Creation"></a></p>
<div class="section">
<h2><a name="Creation_and_teardown"></a>Creation and teardown</h2>
<p>A database handle is created using the <tt>database</tt> facade method, for instance supplying as argument a database URL. Once the database handle is no longer required, internal resources may be freed up using the <tt>teardown</tt> method.</p>
<p><i>Illustration</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">import static org.jdbdt.JDBDT.*;
import org.jdbdt.DB;
import java.sql.Connection;
import java.sql.DriverManager;
...
// Creation
String dbURL = ...;
DB dbHandle = database("jdbc:myFaveDBEngine://myDB");
...
// Tear-down.
// The second parameter indicates if the underlying
// JDBDT connection should be closed or kept open.
// In this case we close the connection.
teardown(db, true);
</pre></div></div>
<p> <a name="Configuration"></a></p></div>
<div class="section">
<h2><a name="Configuration"></a>Configuration</h2>
<p>Database handle options are defined by the <tt>DB.Option</tt> enumeration. They may be enabled and disabled using <tt>enable</tt> and <tt>disable</tt>, respectively. The available options relate to logging and a few other features discussed below. </p>
<p><i>Illustration</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">import static org.jdbdt.JDBDT.*;
import org.jdbdt.DB;
import org.jdbdt.DB.Option;
...
DB db = database(...);
db.enable(Option.LOG_SETUP);
</pre></div></div>
<p><a name="Logging"></a></p>
<div class="section">
<h3><a name="Logging"></a>Logging</h3>
<p>For debugging purposes or report generation, trace output may be written to a <a href="Logs.html">log file</a>. The following logging options are defined in <tt>DB.Option</tt></p>
<ul>
<li><tt>LOG_ASSERTION_ERRORS</tt>: log failed assertions;</li>
<li><tt>LOG_ASSERTIONS</tt>: log all assertions (passed or failed);</li>
<li><tt>LOG_QUERIES</tt>: log the result of database queries;</li>
<li><tt>LOG_SETUP</tt>: log database setup operations (data set insertions and SQL setup commands);</li>
<li><tt>LOG_SNAPSHOTS</tt>: log database snapshots;</li>
<li><tt>LOG_DATABASE_EXCEPTIONS</tt>: log database exceptions.</li>
</ul>
<p>At creation time, the <tt>LOG_ASSERTION_ERRORS</tt> and <tt>LOG_DATABASE_EXCEPTIONS</tt> options are enabled by default, and the log output is redirected to <tt>System.err</tt>. Subsequently, logging options may be enabled/disabled using <tt>enable</tt> / <tt>disable</tt> and the output log may be changed using <tt>setLog</tt>. A call to <tt>enableFullLogging()</tt> enables all logging options at once.</p>
<p><i>Illustration</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">import static org.jdbdt.JDBDT.*;
import org.jdbdt.DB;
import org.jdbdt.Log;
...
DB db = database(...);
// Write log output to a file
db.setLog(new File("MyLog.jdbdt.xml"));
// Enable all logging options
db.enableFullLogging();
</pre></div></div>
<p>Note that if you use a <tt>.gz</tt> extension for log files, they will be GZIP-compressed and have a much smaller in size, e.g.,</p>
<div class="source">
<div class="source"><pre class="prettyprint">db.setLog(new File("MyLog.jdbdt.xml.gz"));
</pre></div></div>
<p><a name="StatementReuse"></a></p></div>
<div class="section">
<h3><a name="Statement_reuse"></a>Statement reuse</h3>
<p>A database handle internally reuses <tt>java.sql.PreparedStatement</tt> objects to avoid re-compiling SQL code, regardless of any statement pooling in place for the JDBC driver in use, The scheme is enabled by default and it should generally provide a little more efficiency and cause no problems. For drivers that do not deal well with statement reuse, however, the <tt>REUSE_STATEMENTS</tt> option should be disabled as follows:</p>
<p><i>Illustration</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">import static org.jdbdt.JDBDT.*;
import org.jdbdt.DB;
import org.jdbdt.DB.Option;
...
DB db = database(...);
db.disable(Option.REUSE_STATEMENTS)
</pre></div></div>
<p><b>Known issue</b>: statement reuse should be disabled for <a href="Compatibility.html#KnownIssues">xerial’s JDBC driver for sqlite</a>. No problems were detected for <a href="Compatibility.html#Drivers">all other JDBC drivers tested in the JDBDT build</a>.</p>
<p><a name="BatchUpdates"></a></p></div>
<div class="section">
<h3><a name="Batch_updates"></a>Batch updates</h3>
<p>The <tt>BATCH_UPDATES</tt> option indicates that database insertions should use the JDBC batch update mechanism, unless the JDBC driver in does not support this feature (in this case the option will have no effect). The option is enabled by default.</p>
<p><a name="CaseSensitiveColumnNames"></a></p></div>
<div class="section">
<h3><a name="Case-sensitive_column_handling"></a>Case-sensitive column handling</h3>
<p>The <tt>CASE_SENSITIVE_COLUMN_NAMES</tt> option indicates that column names should be handled in case-sensitive manner. This is required if you wish to use double-quotes with column names. If this option is set, the behavior will be more dependent on the database engine in use, so you should make sure you are aware of the engine’s policy regarding column names and be consistent with it. </p>
<p>This support is preliminary and has known problems with MySQL and PostgreSQL.</p>
<p><a name="SummaryOfMethods"></a></p></div></div>
<div class="section">
<h2><a name="Summary_of_API_methods"></a>Summary of API methods</h2>
<div class="section">
<h3><a name="JDBDT"></a><tt>JDBDT</tt></h3>
<ul>
<li><tt>database(c)</tt> creates a handle for database connection <tt>c</tt>.</li>
<li><tt>database(url)</tt> creates a handle for the given database URL.</li>
<li><tt>database(url, user, pass)</tt> creates a handle for the given database URL, user name, and password.</li>
<li><tt>teardown(db, closeConn)</tt> frees up internal resources used by <tt>db</tt>, and also closes the underlying database connection if <tt>closeConn == true</tt>.</li>
</ul></div>
<div class="section">
<h3><a name="DB"></a><tt>DB</tt></h3>
<ul>
<li><tt>getConnection()</tt> returns the connection associated to the handle.</li>
<li><tt>enable(o1, o2, ...)</tt> enables given options.</li>
<li><tt>disable(o1, o2, ...)</tt> disables given options.</li>
<li><tt>isEnabled(o)</tt> tests if option <tt>o</tt> is enabled.</li>
<li><tt>enableFullLogging()</tt> enables all logging options.</li>
<li><tt>setLog(out)</tt> redirects log output to <tt>out</tt>, a <tt>java.io.File</tt> or <tt>java.io.PrintStream</tt>.</li>
<li><tt>setMaximumBatchUpdateSize(n)</tt> sets <tt>n</tt> as the maximum number of operations in a batch update.</li>
<li><tt>getMaximumBatchUpdateSize()</tt> gets the current setting for the maximum number of operations in a batch update.</li>
<li><tt>setAutoCommit(enable)</tt> enables/disables the auto-commit mode for the underlying database connection.</li>
<li><tt>getAutoCommit()</tt> gets the auto-commit mode for the underlying database connection.</li>
</ul></div></div>
</div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p >Copyright © 2016–2021
<a href="http://jdbdt.org">JDBDT</a>.
All rights reserved.
</p>
</div>
</div>
</footer>
</body>
</html>