Shared Memory Applications in C#

As explained in the Shared Memory Applications page, eXtremeDB allows two or more processes to share a common database. General instructions are given there explaining how shared memory segments must be created on different operating systems. Further implementation details for C# applications are provided in the following sections.

Example

The following code snippet demonstrates how a process initializes a shared memory device, then uses a try-catch blocks to and open or connect to a database in shared-memory:

 
    public class Open
    {
        const int PAGE_SIZE = 128;
        const int DATABASE_SIZE = 16*1024*1024;
 
        // create Database object and define global connection instance
        public static Database db = new Database(new ExtremedbWrapper());
        public static Connection con;
         
        public static void OpenSharedDb(
            String dbName,        // name of the database
            Int32  dbSize,        // size of memory segment for in-mem part
            Int32  pgSize,        // size of memory page
            Int32  maxConnections // max. number of connections
        )
        {
            Database.Device[] devs = new Database.Device[1];
            Database.Parameters parameters = new Database.Parameters();
            parameters.MemPageSize = pgSize;
            parameters.Classes = new Type[] { typeof(Obj) }; // list of database 					    classes
            parameters.MaxConnections = maxConnections;
 
            devs[0] = new Database.SharedMemoryDevice(
                        Database.Device.Kind.Data,
                        dbName + “-db”,
                        new IntPtr(0), dbSize);
 
            try
            { // Open database. If already opened an exception is thrown
                db.Open("opendb", parameters, devs);       
            }
            catch (DatabaseError dbe)
            {
                // Note that code 66 = “duplicate instance” is a valid case
                // for a Shared-memory database; the secondary application
                // will connect to the existing database. So this exception is
                // ignored and the application continues. Other exceptions are
                // handled below…
                if (dbe.errorCode != 66)
                {
                    if (dbe.errorCode == 62)
                        Console.WriteLine("eXtremeDB assembly is not
                            compatible with option 'shm'. Please replace 
                            reference to THE assembly with 
                            shared memory                                       functionality");
                    else if (dbe.errorCode == 620000)
                        Console.WriteLine("eXtremeDB assembly is not
                            compatible with option 'disk'. Please replace
                            reference to assembly with disk manager
                            functionality");
                    throw dbe;
                    // equivalent to return MCO_E_UNSUPPORTED;
                }
            }
        }
 
        public static void ConnectDb()
        {
            try
            {
                con = new Connection(db); // connect to the database
            }
            catch (DatabaseError dbe)
            {
                throw dbe;
            }
        }
     
        public static void Main(String[] args)
        {
            try
            {
                // This method starts the database runtime and opens a
                // new database or detects if the shared memory database is
                // already opened.
                OpenSharedDb("sharedDb", DATABASE_SIZE, PAGE_SIZE, 1);
                 
                // Connect to the new or existing shared-memory database
                ConnectDb();
 
                // Normal database processing goes here
                con.Disconnect();
                db.Close();
            }
            catch (DatabaseError dbe)
            {
                Console.WriteLine("\n\nError %d creating/connecting shared
                            memory database.\n", dbe.errorCode);
            }
            Console.ReadLine();
        }
    }