# Create/Open Persistent Databases in Java

> To create or open a persistent database, the application first specifies the `Database.MCO_CFG_DISK_SUPPORT` flag in the `mode` argument passed to the Database constructor.

> Whereas an in-memory database application needs only a single memory device, persistent database applications must define at least 4 memory devices. These are defined in an array that is passed to the Database method `open()`.

> Also at least the following two properties are specified in the Database.Parameters that are passed to the `Open()` method:

> - `diskPageSize` - It is recommended to set this to the blocking factor of the persistent media

> - `diskClassesByDefault` - Marks all classes with attribute [Persistent] to be stored on persistent media (Disk-based classes can also be defined explicitly with the attribute `[Persistent(Disk=true)]`)

> ## Parameter Settings

> Please refer to the general guidelines discussed in the Creating Databases page for determining the proper values for `MemPageSize`, `DiskPageSize`, `MaxConnections`, `MaxDiskDatabaseSize` and `LogType`.

> ### Log Type and Transaction Commit Policy

> Applications set the `DatabaseParameters.LogType` to `RedoLog, UndoLog` or `NoLog` for the Database method `open()`. Depending on the log type selected, the additional Database.Parameters property `RedoLogLimit` or `DelayedCommitThreshold` can be set. (Please see the Persistent Database I/O page for detailed explanations.)

> Note that if the `NoLog` log type is selected, the application can explicitly force the cache to be flushed to the media with the Connection method `diskFlush()`.

> ## Database Creation Example

> Typical application code to create a persistent database could look like the following:

>         @Persistent
>         class Obj
>         {
>             public int i4;
>         };
>         ...
>         static final int PAGE_SIZE = 128;
>         static final int DISK_PAGE_SIZE = 4096;
>         static final int DISK_CACHE_SIZE = 8*1024*1024;
>         static final int DATABASE_SIZE = 16*1024*1024;

>         Database.Mode mode = Database.MCO_CFG_DISK_SUPPORT;
>         Database.Parameters parameters = new Database.Parameters();
>         devs = new Database.Device[4];

>         Database db = new Database(mode);

>         parameters.memPageSize = PAGE_SIZE;
>         parameters.classes = new Class[]{Obj.class};
>         parameters.maxConnections = 10;
>         parameters.diskPageSize = DISK_PAGE_SIZE;
>         parameters.diskClassesByDefault = true;

>         devs[0] = new Database.PrivateMemoryDevice(Database.Device.Kind.Data, DATABASE_SIZE);
>         devs[1] = new Database.PrivateMemoryDevice(Database.Device.Kind.DiskCache, DISK_CACHE_SIZE);
>         devs[2] = new Database.FileDevice(Database.Device.Kind.Data, "opendb.dbs");
>         devs[3] = new Database.FileDevice(Database.Device.Kind.TransactionLog, "opendb.log");

>         db.open("opendb", parameters, devs);

>         // Do database processing

>         db.close();