Create/Open Persistent Databases in C#

To create or open a persistent database, the application first specifies the Database.Mode.DiskSupport 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:

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;
    };
    ...
    const int PAGE_SIZE = 128;
    const int DISK_PAGE_SIZE = 4096;
    const int DISK_CACHE_SIZE = 8*1024*1024;
    const int DATABASE_SIZE = 16*1024*1024;
     
    Database.Mode mode = Database.Mode.DiskSupport;
    Database.Parameters parameters = new Database.Parameters();
    devs = new Database.Device[4];
     
    Database db = new Database(mode);
     
    parameters.MemPageSize = PAGE_SIZE; 
    parameters.Classes = new Type[] { typeof(Obj) }; 
    parameters.MaxConnections = 10;
    parameters.DiskPageSize = DISK_PAGE_SIZE; 
    parameters.DiskClassesByDefault = true; 
    parameters.LogType = Database.LogType.NoLog;
 
    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();