Oracle by Example brandingCreating a Transactional Environment in Berkeley DB

section 0Before You Begin

This is the first tutorial in the Getting Started with the Graphical User Interface in Berkeley DB series. Perform both these tutorials sequentially as there is a dependency to complete one before starting the other.

In this first tutorial, you will create a transactional environment, create a database, create and manipulate data, and perform database transactions using the new Graphical User Interface in Berkeley DB 18.1.

This tutorial takes approximately 10 minutes to complete.

Background

Berkeley DB is a family of embedded key-value database libraries providing scalable high-performance data management services to applications. It enables the development of custom data management solutions, without any overhead. You can configure Berkeley DB to address any application need from the hand-held device to the data center, from a local storage solution to a world-wide distributed one, and from kilobytes to petabytes.  

The Oracle Berkeley DB Graphical User Interface (BDB GUI) is introduced in Berkeley DB 18.1 and provides a simple interface for interacting with the Berkeley DB environment.

What Do You Need?

  • Install JDK 8 or higher version in your machine. This is required to build and run Berkeley DB GUI.
  • Install Oracle Berkeley DB 18.1 from the Oracle Technology Network.
  • For Windows only: Install Oracle Berkeley DB 18.1 Windows binary installer from the Oracle Technology Network.
  • For Linux only:
    • After installing, enable GUI with the --enable-gui flag while building Berkeley DB 18.1.
    • Set the following environment variables. Replace [bdb-install_directory] with the path to the directory where you have Berkeley DB 18.1 installed:

      export CLASSPATH=[bdb-install_directory]/lib/db.jar;[bdb-install_directory]/lib/db_gui.jar;$CLASSPATH

      export LD_LIBRARY_PATH=[bdb-install_directory]/lib;$LD_LIBRARY_PATH

section 1Start the Berkeley DB GUI

  1. Open a Command Prompt in your machine.
    Open command prompt
    Description of the illustration sect_1_01.jpg
  2. Type java db_gui.db_gui and press Enter to start the Berkeley DB GUI.
    Type the command to start the GUI
    Description of the illustration sect_1_02.jpg
  3. The Berkeley DB GUI opens up for you.
    Thye Berkey DB GUI
    Description of the illustration sect_1_03.jpg

section 2Create a Transactional Environment

A Berkeley DB environment is a container that holds and manages databases.The Create Environment page is the first page that appears when you start a Berkeley DB GUI and allows you perform the operations on an environment such as create, open, backup, recovery, remove old logs, upgrade, verify data, and verify logs.

In this section, you create a transactional environment.

  1. Select a environment home directory to store any future databases, logs, and other files belonging to the environment. Click the Browse button by Environment Home and select a directory in which you want to store the environment.
    Click Browse and select a directory
    Description of the illustration sect_2_01.jpg
  2. Select the Transactional check box. This enables transactional data protection for the environment.
    Select the Transactional checkbox
    Description of the illustration sect_2_02.jpg
  3. Select a cache size of 25 megabytes. Enter 26214400 in the Cache Size box and click the Create button.This will move the GUI to the Database Page.
    Enter a cache size and click create
    Description of the illustration sect_2_04.jpg
  4. You see a message "Environment successfully created." at the bottom of the screen. This confirms that the environment is created successfully. 
    Environment created successfully.
    Description of the illustration sect_2_05.jpg
  5. Open your environment home directory and you see files like __db.001 created in it.
    Verify files created in home directory
    Description of the illustration sect_2_06.jpg

section 3Create a Database

The Database page allows you to perform operations on databases, such as create, open, compact, remove, and rename. In this section, you create a database by using the Berkeley DB GUI.

  1. In the Create Database page, enter a name for the database file. Enter test.db in the Database File text box.
    ENter tst.db for database file
    Description of the illustration sect_3_01.jpg
  2. Ensure that btree is the type of database selected from the Database Type menu and select the page size as 4096 from the Page Size menu.
    Sensure btree is selected and select page size as 4096
    Description of the illustration sect_3_03.jpg
  3. Click the Create button to create the database.This will move the GUI to the Data Access Page.
    Click Create
    Description of the illustration sect_3_04.jpg
  4. You see a message "Database successfully created." at the bottom of the screen. This confirms that the database is created successfully.
    database created successfully
    Description of the illustration sect_3_05.jpg
  5. Open your environment home directory. You see the database file test.db created in the directory.
    test.db file exists in home directory
    Description of the illustration sect_3_06.jpg

section 4Add, Browse, and Delete Data

The Data Access page allows you to browse and manipulate data in a database. In this section, you insert, browse, and delete data from the database.

  1. In the Data Access page, add data to the database by going to the Put Data section at the top of the page and performing the following steps:
    • Enter hello in the Key text box
    • Enter world in the Data text box
    • Click the Put button. 
    put data into the database
    Description of the illustration sect_4_01.jpg
    You should see the "Inserted record successful" message at the bottom of the page.
    inserted record successful message
    Description of the illustration sect_4_01_a.jpg
  2. Next, insert another record. Enter foo in the Key text box, bar in the Data text box, and click the Put button.
    Insert another record
    Description of the illustration sect_4_02.jpg
  3. To browse the data inserted into the database, click the Next button in the Browse Data section.The words "foo" and "bar" should appear in the Key and Data text boxes.
    click Next
    Description of the illustration sect_4_03.jpg
  4. Click Next again. The words "hello" and "world" will appear.
    Click Next again
    Description of the illustration sect_4_04.jpg
  5. Click Next again. The text boxes will be empty, and the message "End of the database has been reached, there is no next record." will appear at the bottom of the page.
    end of record
    Description of the illustration sect_4_05.jpg
  6. To fetch a specific record from the database, you use the Get button. In the Browse Data section, enter "hello" in the Key text box and click the Get button.
    fetch a specific record
    Description of the illustration sect_4_06.jpg
    The word "world" will appear in the Data text box.
    data fetched
    Description of the illustration sect_4_06.jpg
  7. Delete the "hello world" record by clicking the Delete button.
    delete a record
    Description of the illustration sect_4_07.jpg
  8. Try to get the "hello" record again by redoing step 6. The message "Database does not contain that key." will appear at the bottom of the page.
    fetch a deleted record
    Description of the illustration sect_4_08.jpg

section 5Begin, Commit, and Abort a Transaction

A transaction is a logical unit of work that contains one or more statements. The effects of all statements in a transaction can be either all committed (applied to the database) or all aborted (undone from the database). Perform the following steps to begin, commit, and abort a transaction:

Note: This section appears in the Berkeley DB GUI only if the environment is transactional.

  1.  Click the Begin button in the Transaction section at the top of the page.
    Click Begin
    Description of the illustration sect_5_01.jpg
  2. In the Put Data section, enter "abc" for Key, and "def" for Data and click Put.
    enter a record
    Description of the illustration sect_5_02.jpg
  3. In the Browse Data section, enter "abc" in the Key text box and click Get. This will retrieve the "abc def" record.
    Get the abc record
    Description of the illustration sect_5_03.jpg
  4. Click the Abort button. This will undo all the operations performed from steps 1 to 3.
    Click Abort
    Description of the illustration sect_5_04.jpg
  5. Click the Clear button to clear all the text fields.
    Click Clear
    Description of the illustration sect_5_05.jpg
  6. Try to get the "abc" record again, it will fail indicating that the transaction is rolled back from the database.
    fetch a deleted record
    Description of the illustration sect_5_06.jpg


next stepNext Tutorial

Performing Verification and Backup of a Transactional Environment in Berkeley DB