multiple thread "Creation of the database folder failed!"

Feb 7, 2013 at 8:11 AM
Edited Feb 7, 2013 at 8:45 AM
I try to access data from multiple thread exception throws:

"The process cannot access the file 'D:\Data\DBreeze_DBreezeSchema' because it is being used by another process."

The environment is: multiple thread keep calling Save to insert data, multiple thread keep calling Load to get data.


Main Code:
        private DBreezeEngine NewEngine
        {
            get { return new DBreezeEngine(@"D:\Data\DBreeze\"); } //exception here
        }

        public void DoBiz(Action<Transaction> Biz)
        {
            using (var engine = NewEngine)
            {
                using (var tran = engine.GetTransaction())
                {
                    try
                    {
                        Biz(tran);
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                    }
                }
            }
        }

        public void Save(Foo Result)
        {
            DoBiz((t) =>
                {
                    ulong id = t.Count("FooTable");
                    var key = Result.FooTime.To_8_bytes_array().ConcatMany(((ulong)Result.FooType).To_8_bytes_array_BigEndian(), Convert.ToUInt64(Result.Result).To_8_bytes_array_BigEndian());
                    t.Insert<byte[], DbCustomSerializer<Foo>>("FooTable", key, Result);
                });
        }

        public IList<Foo> Load(DateTime StartTime, DateTime EndTime, int FooType, ulong Result)
        {
            IList<Foo> result = null;
            DoBiz((t) =>
            {
                ulong id = t.Count("FooTable");
                var startType = PulseType == 0 ? 0 : PulseType;
                var startResult = Result == 0 ? ulong.MinValue : Result;
                var startKey = StartTime.To_8_bytes_array().ConcatMany(((ulong)startType).To_8_bytes_array_BigEndian(), startResult.To_8_bytes_array_BigEndian());
                var stopType = PulseType == 0 ? 10 : PulseType;
                var stopResult = Result == 0 ? ulong.MaxValue : Result;
                var stopKey = EndTime.To_8_bytes_array().ConcatMany(((ulong)stopType).To_8_bytes_array_BigEndian(), stopResult.To_8_bytes_array_BigEndian());
                result = t.SelectForwardFromTo<byte[], DbCustomSerializer<Foo>>("FooTable", startKey, true, stopKey, true)
                    .Select(r => r.Value.Get).ToList();
            });
            return result;
        }
Coordinator
Feb 7, 2013 at 8:57 AM
Edited Feb 7, 2013 at 8:59 AM
Instantiate your engine ONCE in the beginning of the program and Dispose it once when you quit your program or DLL.
DONT INITIALIZE ENGINE WHEN YOU NEED TO MAKE SELECT.
When you need to make select/inserts from different threads you must create only different transactions.
using (var tran = engine.GetTransaction())

All your threads must work with one Engine instance.

It's also not possible to get database simultaneously from different processes. It's and embedded database!!! If you need multi-process access you must create server, based on DBreeze.
Feb 7, 2013 at 10:03 AM
Right, I instantiate the DBreezeEngine at class constuctor, and it's working fine now. Thanks.