Server JAVA GSG
Server JAVA GSG
Release 18.1
Library Version 18.1.32
Legal Notice
Copyright © 2002 - 2019 Oracle and/or its affiliates. All rights reserved.
This software and related documentation are provided under a license agreement containing restrictions on use and disclosure
and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you
may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any
part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for
interoperability, is prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors,
please report them to us in writing.
Berkeley DB, and Sleepycat are trademarks or registered trademarks of Oracle. All rights to these marks are reserved. No third-
party use is permitted without the express prior written consent of Oracle.
If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S.
Government, the following notice is applicable:
U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on
the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to
the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure,
modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the
hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other
rights are granted to the U.S. Government.
This software or hardware is developed for general use in a variety of information management applications. It is not developed or
intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you
use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup,
redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages
caused by use of this software or hardware in dangerous applications.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.
Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and
are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are
trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group.
This software or hardware and documentation may provide access to or information on content, products, and services from third
parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect
to third-party content, products, and services. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or
damages incurred due to your access to or use of third-party content, products, or services.
Published 19-Feb-2019
Table of Contents
Preface ...................................................................................................... iv
Conventions Used in this Book .................................................................... iv
For More Information ............................................................................... v
Contact Us ...................................................................................... v
1. Introduction to Building Distributed Applications with Berkeley DB ............................ 1
Berkeley DB Server .................................................................................. 1
Supported Features ........................................................................... 1
Client Driver APIs .................................................................................... 2
Secure Connections .................................................................................. 2
2. Berkeley DB Server ..................................................................................... 3
System Requirements ............................................................................... 3
Starting the Server .................................................................................. 3
Stopping the server ................................................................................. 4
Enabling SSL for the Server ........................................................................ 4
Configuring the Server .............................................................................. 5
log4j Configuration File ...................................................................... 5
Server Configuration File .................................................................... 6
Server Configuration Options ......................................................... 7
Impacts on Utility Programs ...................................................................... 10
3. Berkeley DB Client Driver APIs ...................................................................... 13
System Requirements .............................................................................. 13
Connecting to a Server without SSL ............................................................ 13
Connecting to a Server with SSL ................................................................ 13
Administrative Functions .......................................................................... 14
Shutdown the Server ........................................................................ 14
Using ping ..................................................................................... 14
Getting the server version ................................................................. 15
Cleaning up Inactive Handles .............................................................. 15
Deleting an Environment and Its Databases ............................................. 16
Removing and Renaming Objects ................................................................ 16
Key/Data Pairs and Bulk Operations ............................................................ 17
Statistic Functions ................................................................................. 18
Secondary Databases .............................................................................. 18
Multi-Threaded Applications ...................................................................... 19
The steps to start the server and the driver APIs used to implement client applications
are described here. This book describes the concepts surrounding distributed Berkeley DB
applications, the scenarios under which you might choose to use it, and the requirements of
using the server program and the driver APIs.
This book is aimed at the software engineer responsible for writing a distributed DB
application.
This book assumes that you have already read and understood the concepts contained in the
Berkeley DB Getting Started with Transaction Processing guide.
Class names are represented in monospaced font, as are method names. For example: "The
Environment() constructor returns an Environment class object."
Variable or non-literal text is presented in italics. For example: "Go to your DB_INSTALL
directory."
Program examples are displayed in a monospaced font on a shaded background. For example:
import com.sleepycat.client.SDatabaseConfig;
...
In some situations, programming examples are updated from one chapter to the next. When
this occurs, the new code is presented in monospaced bold font. For example:
import com.sleepycat.client.SDatabase;
import com.sleepycat.client.SDatabaseConfig;
...
• Berkeley DB Javadoc
To download the latest Berkeley DB documentation along with white papers and other
collateral, visit http://www.oracle.com/technetwork/indexes/documentation/index.html.
For the latest version of the Oracle Berkeley DB downloads, visit http://www.oracle.com/
technetwork/database/database-technologies/berkeleydb/downloads/index.html.
Contact Us
You can post your comments and questions at the Oracle Technology (OTN) forum for Oracle
Berkeley DB at: https://forums.oracle.com/forums/forum.jspa?forumID=271, or for Oracle
Berkeley DB High Availability at: https://forums.oracle.com/forums/forum.jspa?forumID=272.
For sales or support information, email to: berkeleydb-info_us@oracle.com You can subscribe
to a low-volume email announcement list for the Berkeley DB product family by sending email
to: bdb-join@oss.oracle.com
This book provides a thorough introduction and discussion on how to build distributed
applications with Berkeley DB (DB). It describes the features supported by the server, and how
to configure and start the server program. Finally, it describes the driver APIs that you use to
implement client applications.
You should understand the concepts from the Berkeley DB Getting Started with Transaction
Processing guide before reading this book.
Berkeley DB Server
The server is capable of managing multiple DB environments and databases on behalf of
client applications. All environments and databases managed by a server are protected by
transactions. Non-transactional environments or databases are not supported by the server.
The server can serve multiple client applications simultaneously.
Not all features offered by the DB library are supported by the server. For example, features
that require callback functions are not supported. An exception to this is secondary databases,
where a callback function is needed to create the set of secondary keys corresponding to a
given primary key and data pair.
To avoid leaking DB handles, the server can be configured to close long-inactive handles
automatically. This is useful when client applications may disconnect unexpectedly. For
example, a client application may open a transactional cursor, update a few records using the
cursor and then disconnect unexpectedly (e.g. the client application crashes). Without the
ability to close (e.g. abort) the transaction handle by the server, the transaction will be open
forever, and the locks held by the transaction will never be released. This would block other
client applications that require any of the locks.
Supported Features
The following high-level features are supported by Berkeley DB server:
• Transaction
• In-memory databases
• Sequences
In addition, the client driver provides the BdbServerConnection class, which can be used to
establish a connection to a server and also serves as the starting point to everything else in
the client driver APIs. For example, client environment handles are created like this:
BdbServerConnection conn =
BdbServerConnection.connect("localhost", 8080);
SEnvironment env = conn.openEnvironment("env",
new SEnvironmentConfig().setAllowCreate(true));
The BdbServerConnection class is not thread-safe. Furthermore, all objects created from
a BdbServerConnection instance use the connection object internally. Thus, they must be
accessed in the same thread as the connection object, or at least their accesses must be
serialized. This rule applies recursively to handles created from environment handles (which
are created from connection objects). Therefore, the entire object graph rooted at a single
BdbServerConnection instance must be accessed in a single thread, or accessed serially.
Secure Connections
Secure Socket Layer (SSL) can be enabled to secure communications between clients and
servers. Both uni- and bi-directional authentications are supported.
Java key stores are used to manage private and public keys for SSL authentication. Depending
on whether uni- or bi-directional authentication is used, key stores and/or trust stores must
be configured properly on both client and server machines.
System Requirements
The server program requires JDK 8 or above, slf4j, log4j, Apache Thrift's Java library, and
Berkeley DB Java library. All libraries are required at both compile and run time.
Note
The appropriate versions of the third-party libraries slf4j, log4j and Apache Thrift are
bundled in the release package. You do not need to install them separately.
On Windows systems, no batch script is provided because the place of the db.jar depends on
the build configuration. To start a server, you need to do the following:
2. Include the db.jar and all jar files under lang/thrift/jars to the classpath.
3. Set the java.library.path JVM option to point to the directory where native Berkeley
DB libraries are placed.
For example, if the native libraries and db.jar are placed under build_windows/Win32/
Release, a batch script placed under it would look like:
set THRIFT_HOME=..\..\..\lang\thrift
copy %THRIFT_HOME%\server\bdb.properties .
copy %THRIFT_HOME%\server\log4j.xml .
set CLASSPATH=.;%THRIFT_HOME%\jars\slf4j-api.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\slf4j-api.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\log4j-slf4j-impl.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\log4j-core.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\log4j-api.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\log4j-1.2-api.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\libthrift.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\db_thrift_interface.jar
set CLASSPATH=%CLASSPATH%;%THRIFT_HOME%\jars\db_thrift_server.jar
set CLASSPATH=%CLASSPATH%;db.jar
• -v
• -h
• -config-file
• -log-config
Specify the log4j XML configuration file for the server program.
If you want to authenticate the server so that clients know that they are connecting to the
correct server, a key store with the server's private key must be setup on the server. For
example, the following command creates a key store keystore.jks containing a generated
private/public key pair.
keytool -genkeypair -alias certificatekey -keyalg RSA \
-validity 7 -keystore keystore.jks
If you want to authenticate clients, a trust store with trusted clients' public keys must be
setup on the server. For more information, see Connecting to a Server with SSL (page 13).
Once the key store and/or trust store are setup, you should list them in the server
configuration file. For example:
ssl.host=localhost
ssl.keyStore=keystore.jks
ssl.keyStore.password=<password>
#ssl.trustStore=truststore.jks
#ssl.trustStore.password=<password>
For more information on the server configuration file, see Server Configuration File (page
6).
Berkeley DB server uses log4j, and thus requires a log4j configuration file. Berkeley DB
server accepts configuration files in XML only. By default, it looks for a log4j.xml file under
the current working directory. This can be overridden by the -log-config command-line
argument when the server is started.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="File" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="path-to-log-file"/>
<param name="MaxBackupIndex" value="20"/>
<param name="MaxFileSize" value="50MB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="Async" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="256"/>
<appender-ref ref="File" />
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="Async"/>
</root>
</log4j:configuration>
Note
Because this file may contain sensitive information, its permissions should be limited
to the minimum. If the file is accessible globally (globally readable, writable or
executable), the server would not start.
port=8080
workers=10
#ssl.host=server-host-name
#ssl.keyStore=path-to-key-store
#ssl.keyStore.password=key-store-password
#ssl.keyStore.type=jks
#ssl.keyStore.manager=SunX509
#ssl.trustStore=path-to-trust-store
#ssl.trustStore.password=trust-store-password
#ssl.trustStore.type=jks
#ssl.trustStore.manager=PKIX
###################################
# Database service configurations
###################################
# The root directory for all environments, the home directory for an
# environment with name "<env>" is $root.home/<env>
root.home=path-to-environment-root-directory
# The root directory for all data directories, the data directory
# for an environment with name "<env>" is $root.data/<env>
root.data=path-to-data-root-directory
# The root directory for all blob directories, the blob directory
# for an environment with name "<env>" is $root.blob/<env>
root.blob=path-to-blob-root-directory
# The root directory for all log directories, the log directory for
# an environment with name "<env>" is $root.log/<env>
root.log=path-to-db-log-root-directory
# Configure the timeout value for open handles. The timeout value
# is set in seconds.
#
# For example, if the timeout is set to 600 seconds (10 minutes),
# and a handle has not been accessed for longer than 600 seconds,
# then this handle will be closed automatically when the cleanup
# worker runs the next time.
handle.timeout=600
cleanup.interval=300
• port
Specifies the port number the server listens for client connections. The default value is
8080.
• workers
Specifies the number of worker threads used for handling client requests. The default value
is 20.
If SSL is not enabled, client requests are handled by a shared pool of worker threads. A free
worker thread is chosen to handle each client request, and the worker thread becomes free
again after handling the request. The number of worker threads determines the maximum
number of client requests that can be handled concurrently. If the number of concurrent
requests exceeds the number of worker threads, later requests will be blocked until a free
worker thread is available.
Note
In this mode, the number of concurrent client connections is limited only by system
resources. For example, the server can serve 200 concurrent client connections
even if the workers is set to 20.
• ssl.host
SSL is enabled if this property is specified. This property specifies the host name of the
server. Clients should use this name when connecting to the server. There is no default
value, and SSL is disabled by default.
• ssl.keyStore
Specifies the path to the Java key store file which manages the server's private key. There is
no default value.
Note
This file maintains sensitive information. If the file is accessible globally (globally
readable, writable or executable), the server would not start.
• ssl.keyStore.password
Specifies the password of the key store file specified in ssl.keyStore. There is no default
value.
• ssl.keyStore.type
Specifies the type of the key store file specified in ssl.keyStore. There is no default value.
• ssl.keyStore.manager
Specifies the algorithm name of the key manager factory. There is no default value.
• ssl.trustStore
Specifies the path to the Java trust store file which manages the server's trusted public
certificates. There is no default value.
Note
This file maintains sensitive information. If the file is accessible globally (globally
readable, writable or executable), the server would not start.
• ssl.trustStore.password
Specifies the password of the trust store file specified in ssl.trustStore. There is no default
value.
• ssl.trustStore.type
Specifies the type of the trust store file specified in ssl.trustStore. There is no default
value.
• ssl.trustStore.manager
Specifies the algorithm name of the trust manager factory. There is no default value.
• root.home
The root directory for all environment's home directory. For example, if the environment
home specified by a client is env_home, the actual home directory on the server is
<root.home>/env_home. The default value is the current working directory.
Note
The environment home specified by a client cannot escape the specified root
directory. Absolute paths or relative paths that escape the root directory are not
allowed.
• root.data
The root directory for all environment's data directories. For example, if the environment
home specified by a client is env_home, the actual directory for access method database
files of this environment is <root.data>/env_home.
• root.blob
The root directory for all environment's blob directories. For example, if the environment
home specified by a client is env_home, the actual directory for blob files of this
environment is <root.blob>/env_home.
• root.log
The root directory for all environment's log directories. For example, if the environment
home specified by a client is env_home, the actual directory for log files of this
environment is <root.log>/env_home.
• handle.timeout
Specifies the timeout for open handles on the server, in seconds. The server scans all open
handles periodically, and closes those handles that have not been accessed for the last
handle.timeout seconds. The default value is 600.
• cleanup.interval
Specifies the interval between consecutive scans of open handles, in seconds. The default
value is 300.
The following constraints apply when running utility programs on managed environments and
databases that are accessed simultaneously by other processes:
Note
Constraints do not apply if the accessed environment or database has no client
handles opened or if the server is offline.
• db_archive
No constraint applies.
Note
If root.data and/or root.log are different from root.home, you need a DB_CONFIG
file which sets the proper paths using the add_data_dir , or set_lg_dir
configuration parameters.
• db_dump
• db_load
Note
If root.blob is different from root.home, you need to set the proper blob path using
the -b option.
• db_log_verify
No constraint applies.
Note
If root.log is different from root.log, you need a DB_CONFIG file which sets the
proper path using the set_lg_dir configuration parameter.
• db_printlog
No constraint applies.
Note
If root.log is different from root.log, you need a DB_CONFIG file which sets the
proper path using the set_lg_dir configuration parameter.
• db_recover
db_recover must have exclusive access to the environment being recovered. An alternative
way to perform normal recovery of an environment is to open the environment with the
DB_RECOVER flag from a client application.
• db_replicate
Note
Replication is not supported by Berkeley DB servers. Therefore, even when the
server is offline, db_replicate still cannot be used on databases managed by
Berkeley DB server.
• db_sql_codegen
• dbsql
dbsql cannot be used on databases managed by Berkeley DB servers because they are not
created with the SQL APIs.
• db_stat
• db_upgrade
• db_verify
Note
If root.blob is different from root.home, you need to set the proper blob path using
the -b option.
• sqlite3
Secondary Databases
To manage secondary keys created for secondary databases, an auxiliary database is
maintained on the server for each secondary database. If the secondary database is file based,
the auxiliary database is also file based. If the secondary database is in-memory, the auxiliary
database is also in-memory.
For file-based auxiliary databases, its file name is constructed by appending "__aux.db" to
the file name of its corresponding secondary database. For example, if the file name of a
secondary database is "secondary", the file name of its corresponding auxiliary database is
"secondary__aux.db".
Make sure that the auxiliary database is in sync with its corresponding secondary databases.
Otherwise, operations on the secondary database may fail. For example, when backing up
or restoring databases, the secondary databases and their corresponding auxiliary databases
must be backed up or restored together. When restoring databases, the databases must be
restored together.
This chapter begins by describing how to connect to a server. It then follows with a detailed
list of differences between the client driver APIs and Java base APIs.
System Requirements
The client driver requires JDK 8 or above, slf4j, log4j , and Apache Thrift's Java library. All
libraries are required at both compile and run time.
Note
The appropriate versions of the third-party libraries slf4j, log4j and Apache Thrift are
bundled in the release package. You do not need to install them separately.
For Java applications, you should add the following jar files under the lang/thrift/jars
directory to your applications classpath:
• db_thrift_client.jar
• db_thrift_interface.jar
• libthrift.jar
• log4j-1.2-api.jar
• log4j-api.jar
• log4j-core.jar
• log4j-slf4j-impl.jar
• slf4j-api.jar
To continue from the example of authenticating the server found in Enabling SSL for the
Server (page 4), to setup the trust store files and connect to a server with SSL, do the
following:
1. Export the certificate containing the server's public key from the key store on the server
using the following command:
keytool -export -alias certificatekey \
-keystore keystore.jks -rfc -file cert.cer
2. Create a trust store on the client machine and import the certificate to it using the
following command:
keytool -import -alias certificatekey \
-file cert.cer -keystore truststore.jks
Administrative Functions
Several functions are added to the client driver APIs to control the server, to clean up open
handles and to delete unused environments. These functions do not rely on open handles and
are put together into the BdbServerAdmin class.
Using ping
You can test the server's reachability using the ping method:
BdbServerConnection conn =
BdbServerConnection.connect("localhost", 8080);
...
...
The Berkeley DB server can be configured to close these inactive handles periodically with
the cleanup.interval and handle.timeout properties. For more information, see Server
Configuration Options (page 7)
In addition, the driver APIs also provide functions to clean up inactive environment and
database handles. For example:
Note
When an environment or database handle is closed, all handles that depend on it
are also closed. For example, when an environment handle is closed, all transaction
handles opened under the environment handles are also closed.
BdbServerConnection conn =
BdbServerConnection.connect("localhost", 8080);
// Delete the environment whose home directory is "env" and all the
// databases opened in it. Close all open handles on the environment
// and databases.
adminService.deleteEnvironmentAndDatabases("env", true);
In the driver APIs, each of these functions has a boolean argument force. If this argument is
set to true, all handles opened on the object to be removed or renamed are automatically
closed by the server, so the operation can proceed. If this argument is set to false, the
operation is executed only if there is no open handle on the object; otherwise, the function
throws a SResourceInUseException exception.
For example, if you want to remove a database even if someone is still using it, you can call
SEnvironment.removeDatabase with the force parameter set to true:
BdbServerConnection conn = ...;
SEnvironment env = conn.openEnvironment("env", null);
If you want to remove a database, but only when no one is using it, you can call the same
function with the force parameter set to false:
BdbServerConnection conn = ...;
SEnvironment env = conn.openEnvironment("env", null);
try {
// Try to remove the database if no one is using it.
env.removeDatabase(null, "db", null, false);
// The database is removed successfully.
} catch (SResourceInUseException e) {
// Someone is still using the database and the database
// is not removed
}
In driver APIs, the amount of data to be returned in a single bulk retrieval is not
limited by a buffer size, because SMultipleDataEntry , SMultipleKeyDataEntry or
SMultipleRecnoDataEntry does not use a byte array buffer. Instead, the number of entries
to be returned is specified with the setBatchSize method:
SEnvironment env = ...
Statistic Functions
All statistic functions in the Java base APIs are supported in the driver APIs. In addition, a
function that returns multiple environment statistics at once is provided to save a few client-
server round trips when multiple statistics are needed:
SEnvironment env = ...
Secondary Databases
The secondary-key callback configured for a secondary database is called at the client node
only. To make it work, the client driver tries to maintain enough information for the callback.
However, certain operations are still not supported by the client driver.
• Partial update is not supported on databases that have associated secondary databases.
Multi-Threaded Applications
The BdbServerConnection class is not thread-safe. Furthermore, objects created from a
BdbServerConnection instance use the connection object internally for remote invocations.
Therefore, they are not thread-safe either. The connection object together with all objects
created from it must be used in a single thread, because they all share the same connection
object. For example, if we create a BdbServerAdmin object, a SEnvironment object from
a BdbServerConnection object, a SDatabase object, and a STransaction object from the
SEnvironment object, all five objects must be accessed in a single thread.