Library Version 18.1.40

Chapter 3. The DB Replication Manager
Prev |  | Next

## Chapter 3. The DB Replication Manager

The easiest way to add replication to your transactional application is to use the Replication Manager. The Replication Manager provides a comprehensive communications layer that enables replication. For a brief listing of the Replication Manager's feature set, see Replication Manager Overview.

To use the Replication Manager, you make use of a combination of the `DbSite` class and related methods, plus special methods off the `DbEnv` class. That is:

1. Create an environment handle as normal.

2. Configure your environment handle as needed (e.g. set the error file and error prefix values, if desired).

3. Use the Replication Manager replication classes and methods to configure the Replication Manager. Using these classes and methods causes DB to know that you are using the Replication Manager.

   Configuring the Replication Manager entails setting the replication environment's priority, setting the TCP/IP address that this replication environment will use for incoming replication messages, identifying TCP/IP addresses of other replication environments, and so forth. These actions are discussed throughout the remainder of this chapter.

4. Open your environment handle. When you do this, be sure to specify `DB_INIT_REP` and `DB_THREAD` to your open flags. (This is in addition to the flags that you normally use for a single-threaded transactional application). The first of these causes replication to be initialized for the application. The second causes your environment handle to be free-threaded (thread safe). Both flags are required for Replication Manager usage.

5. Start replication by calling `DbEnv::repmgr_start()`.

6. Open your databases as needed. Masters must open their databases for read and write activity. Replicas can open their databases for read-only activity, but doing so means they must re-open the databases if the replica ever becomes a master. Either way, replicas should never attempt to write to the database(s) directly.

When you are ready to shut down your application:

1. Close any open `DbSite` handles that you might have open.

2. Close your databases

3. Close your environment. This causes replication to stop as well.

### Note

Before you can use the Replication Manager, you may have to enable it in your DB library. This is not a requirement for Microsoft Windows systems, or Unix systems that use pthread mutexes by default. Other systems, notably BSD and BSD-derived systems (such as Mac OS X), must enable the Replication Manager when you configure the DB build.

You do this by not disabling replication and by configuring the library with POSIX threads support. In other words, replication must be turned on in the build (it is by default), and POSIX thread support must be enabled if it is not already by default. To do this, use the `--enable-pthread_api` switch on the configure script.

For example:

    ../dist/configure --enable-pthread-api

## The DbSite Handle

Before continuing, it is useful to mention the `DbSite` handle. This class is used to configure important attributes about a site such as its host name and port number, and whether it is the local site. It is also used to indicate whether a site is a group creator, which is important when you are starting the very first site in a replication group for the very first time.

The `DbSite` handle is used whenever you start up a site. It must be closed before you close your `DbEnv` handle.

The `DbSite` handle is plays an important role in replication group management. This topic is fully described in the Berkeley DB Programmer's Reference Guide.

Prev |  | Next
Program Listing | Home | Starting and Stopping Replication