Library Version 18.1.40

Reading and Writing Database Records
Prev | Chapter 8. Database Records | Next

## Reading and Writing Database Records

When reading and writing database records, be aware that there are some slight differences in behavior depending on whether your database supports duplicate records. Two or more database records are considered to be duplicates of one another if they share the same key. The collection of records sharing the same key are called a duplicates set. In DB, a given key is stored only once for a single duplicates set.

By default, DB databases do not support duplicate records. Where duplicate records are supported, cursors (see below) are typically used to access all of the records in the duplicates set.

DB provides two basic mechanisms for the storage and retrieval of database key/data pairs:

- The `Database.put()` and `Database.get()` methods provide the easiest access for all non-duplicate records in the database. These methods are described in this section.

- Cursors provide several methods for putting and getting database records. Cursors and their database access methods are described in Using Cursors.

### Writing Records to the Database

Records are stored in the database using whatever organization is required by the access method that you have selected. In some cases (such as BTree), records are stored in a sort order that you may want to define (see Setting Comparison Functions for more information).

In any case, the mechanics of putting and getting database records do not change once you have selected your access method, configured your sorting routines (if any), and opened your database. From your code's perspective, a simple database put and get is largely the same no matter what access method you are using.

You can use the following methods to put database records:

- `Database.put()`

  Puts a database record into the database. If your database does not support duplicate records, and if the provided key already exists in the database, then the currently existing record is replaced with the new data.

- `Database.putNoOverwrite()`

  Disallows overwriting (replacing) an existing record in the database. If the provided key already exists in the database, then this method returns `OperationStatus.KEYEXIST` even if the database supports duplicates.

- `Database.putNoDupData()`

  Puts a database record into the database. If the provided key and data already exists in the database (that is, if you are attempting to put a record that compares equally to an existing record), then this returns `OperationStatus.KEYEXIST`.

When you put database records, you provide both the key and the data as `DatabaseEntry` objects. This means you must convert your key and data into a Java `byte` array. For example:

    package db.GettingStarted;

    import com.sleepycat.db.DatabaseEntry;
    import com.sleepycat.db.Database;

    ...

    // Database opens omitted for clarity.
    // Databases must NOT be opened read-only.

    String aKey = "myFirstKey";
    String aData = "myFirstData";

    try {
        DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
        DatabaseEntry theData = new DatabaseEntry(aData.getBytes("UTF-8"));
        myDatabase.put(null, theKey, theData);
    } catch (Exception e) {
        // Exception handling goes here
    }

### Getting Records from the Database

The `Database` class provides several methods that you can use to retrieve database records. Note that if your database supports duplicate records, then these methods will only ever return the first record in a duplicate set. For this reason, if your database supports duplicates, you should use a cursor to retrieve records from it. Cursors are described in Using Cursors.

You can use either of the following methods to retrieve records from the database:

- `Database.get()`

  Retrieves the record whose key matches the key provided to the method. If no records exists that uses the provided key, then `OperationStatus.NOTFOUND` is returned.

- `Database.getSearchBoth()`

  Retrieve the record whose key matches both the key and the data provided to the method. If no record exists that uses the provided key and data, then `OperationStatus.NOTFOUND` is returned.

Both the key and data for a database record are returned as byte arrays in `DatabaseEntry` objects. These objects are passed as parameter values to the `Database.get()` method.

In order to retrieve your data once `Database.get()` has completed, you must retrieve the `byte` array stored in the `DatabaseEntry` and then convert that `byte` array back to the appropriate datatype. For example:

    package db.GettingStarted;

    import com.sleepycat.db.DatabaseEntry;
    import com.sleepycat.db.Database;
    import com.sleepycat.db.LockMode;
    import com.sleepycat.db.OperationStatus;

    ...

    Database myDatabase = null;
    // Database opens omitted for clarity.
    // Database may be opened read-only.

    String aKey = "myFirstKey";

    try {
        // Create a pair of DatabaseEntry objects. theKey
        // is used to perform the search. theData is used
        // to store the data returned by the get() operation.
        DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));
        DatabaseEntry theData = new DatabaseEntry();

        // Perform the get.
        if (myDatabase.get(null, theKey, theData, LockMode.DEFAULT) ==
            OperationStatus.SUCCESS) {

            // Recreate the data String.
            byte[] retData = theData.getData();
            String foundData = new String(retData, "UTF-8");
            System.out.println("For key: '" + aKey + "' found data: '" +
                                foundData + "'.");
        } else {
            System.out.println("No record found for key '" + aKey + "'.");
        }
    } catch (Exception e) {
        // Exception handling goes here
    }

### Deleting Records

You can use the `Database.delete()` method to delete a record from the database. If your database supports duplicate records, then all records associated with the provided key are deleted. To delete just one record from a list of duplicates, use a cursor. Cursors are described in Using Cursors.

You can also delete every record in the database by using `Environment.truncateDatabase().`

For example:

    package db.GettingStarted;

    import com.sleepycat.db.DatabaseEntry;
    import com.sleepycat.db.Database;

    ...

    Database myDatabase = null;
    // Database opens omitted for clarity.
    // Database can NOT be opened read-only.

    try {
        String aKey = "myFirstKey";
        DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));

        // Perform the deletion. All records that use this key are
        // deleted.
        myDatabase.delete(null, theKey);
    } catch (Exception e) {
        // Exception handling goes here
    }

### Data Persistence

When you perform a database modification, your modification is made in the in-memory cache. This means that your data modifications are not necessarily flushed to disk, and so your data may not appear in the database after an application restart.

Note that as a normal part of closing a database, its cache is written to disk. However, in the event of an application or system failure, there is no guarantee that your databases will close cleanly. In this event, it is possible for you to lose data. Under extremely rare circumstances, it is also possible for you to experience database corruption.

Therefore, if you care if your data is durable across system failures, and to guard against the rare possibility of database corruption, you should use transactions to protect your database modifications. Every time you commit a transaction, DB ensures that the data will not be lost due to application or system failure. Transaction usage is described in the Berkeley DB Getting Started with Transaction Processing guide.

If you do not want to use transactions, then the assumption is that your data is of a nature that it need not exist the next time your application starts. You may want this if, for example, you are using DB to cache data relevant only to the current application runtime.

If, however, you are not using transactions for some reason and you still want some guarantee that your database modifications are persistent, then you should periodically run environment syncs. Syncs cause any dirty entries in the in-memory cache and the operating system's file cache to be written to disk. As such, they are quite expensive and you should use them sparingly.

Remember that by default a sync is performed any time a non-transactional database is closed cleanly. (You can override this behavior by specifying `true` on the call to `Database.close()`.) That said, you can manually run a sync by calling `Database.sync().`

### Note

If your application or system crashes and you are not using transactions, then you should either discard and recreate your databases, or verify them. You can verify a database using Database.verify(). If your databases do not verify cleanly, use the db_dump command to salvage as much of the database as is possible. Use either the `-R` or `-r` command line options to control how aggressive db_dump should be when salvaging your databases.

Prev | Up | Next
Chapter 8. Database Records | Home | Using the BIND APIs