ジャナ・ビジネス・コンサルティング有限会社トップページへ
/*
 * Berkeley DB XML .NET API
 * 
 * Copyright (C) 2007 Jana Business Consulting Ltd. All rights reserved.
 * 
 * For more information, see http://www.janabiz.com.
 * 
 */

using System;
using JanaBiz.Db;
using JanaBiz.DbXml;
using JanaBiz.DbXml.Exceptions;

public class SimpleContainerInEnv
{
    public static void Main(string[] args)
    {
        // The path the directory where you want to place the environment
        // must exist!!
        string environmentPath = "/path/to/environment/directory";

        // Environment configuration is minimal:
        // create + 50MB cache
        // no transactions, logging, or locking
        DbEnvironmentConfig config = new DbEnvironmentConfig();
        config.CacheSize = 50*1024;
        config.AllowCreate = true;
        config.InitializeCache = true;
        config.ErrorWriter = Console.Out;
        config.ErrorPrefix = "LOG";

        DbEnvironment dbEnv = null;
        XmlManager mgr = null;

        XmlContainer container1 = null;
        XmlContainer container2 = null;
        XmlContainer container3 = null;

        try {
            dbEnv = new DbEnvironment(environmentPath, config);

            // Create Manager using that environment, no DBXML flags
            mgr = new XmlManager(dbEnv, new XmlManagerConfig());

            // Multiple containers can be opened in the same database environment
            using (container1 = mgr.CreateContainer(null, "myContainer1")) {
                using (container2 = mgr.CreateContainer(null, "myContainer2")) {
                    using (container3 = mgr.CreateContainer(null, "myContainer3")) {
                        // do work here //
                    }
                }
            }
        } catch (DbXmlException e) {
            Console.WriteLine(e.ToString());
        } finally {
            if (container3 != null) {
                container3.Close();
            }
            if (container2 != null) {
                container2.Close();
            }
            if (container1 != null) {
                container1.Close();
            }
            if (mgr != null) {
                mgr.Close();
            }
            if (dbEnv != null) {
                dbEnv.Close();
            }
        }
    }
}