1

Closed

Can't open a different database in the same process

description

I'm currently writing some tests for some low-level data access code. Each unit test uses a new database, and deletes the database at the end of the test. The path is randomly generated.

I've discovered that DBreeze only works if I only use a single database for the lifetime of the process, but it doesn't work if use multiple databases in the process.

For example, if I open a database at C:\foo\1, it'll work fine. But, once I close the database at C:\foo\1 and open a database at C:\foo\2, it doesn't work. Inserting data, and then selecting it, always returns a row where Exists is false.

I'm using Mono on Mac.

Sample program:
using System;
using System.IO;
using System.Text;

namespace BreakDBreeze
{
class MainClass
{
    public static void Main (string[] args)
    {
        var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;

        var path = Path.Combine(Path.GetDirectoryName(assemblyLocation), "database");

        if (Directory.Exists(path))
            Directory.Delete(path, recursive: true);


        Directory.CreateDirectory(path);

        for (var ctr = 0; ctr < 10; ctr++)
        {
            var databasePath = Path.Combine(path, ctr.ToString());
            MainClass.UseDatabase(databasePath);
        }
    }

    private static void UseDatabase(string databasePath)
    {
        Console.WriteLine("Testing with database at {0}", databasePath);

        string oldVal = null;
        for (var ctr = 0; ctr < 5; ctr++)
        {
            var val = string.Format("xxx{0}xxx", ctr);

            MainClass.UseValues(databasePath, 555, val, oldVal);

            oldVal = val;
        }

        Console.WriteLine("============================================");
    }

    private static void UseValues(string databasePath, int key, string val, string oldVal)
    {
        //var actualKey = key;
        var actualKey = BitConverter.GetBytes(key);

        //var actualVal = val;
        var actualVal = Encoding.UTF8.GetBytes(val);

        using (var engine = new DBreeze.DBreezeEngine(databasePath))
        {
            using (var transaction = engine.GetTransaction())
            {
                var row = transaction.Select<byte[], byte[]>("t", actualKey);

                string storedValue = null;
                if (row.Exists)
                {
                    //storedValue = row.Value;
                    storedValue = Encoding.UTF8.GetString(row.Value);
                }

                if (storedValue == oldVal)
                    Console.Write("Old value correct, {0}; ", oldVal);
                else
                    Console.Write("Old value incorrect, expected {0}, got {1}; ", oldVal, storedValue);

                transaction.Insert("t", actualKey, actualVal);

                transaction.Commit();
            }
        }

        using (var engine = new DBreeze.DBreezeEngine(databasePath))
        {
            using (var transaction = engine.GetTransaction())
            {
                var row = transaction.Select<byte[], byte[]>("t", actualKey);

                string storedValue = null;
                if (row.Exists)
                {
                    //storedValue = row.Value;
                    storedValue = Encoding.UTF8.GetString(row.Value);
                }

                if (storedValue == val)
                    Console.WriteLine("Stored value correct, {0}", val);
                else
                    Console.WriteLine("Stored value incorrect, expected {0}, got {1}", val, storedValue);
            }
        }
    }
}
}




Sample output


Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/0
Old value correct, ; Stored value correct, xxx0xxx
Old value correct, xxx0xxx; Stored value correct, xxx1xxx
Old value correct, xxx1xxx; Stored value correct, xxx2xxx
Old value correct, xxx2xxx; Stored value correct, xxx3xxx

Old value correct, xxx3xxx; Stored value correct, xxx4xxx

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/1
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/2
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/3
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/4
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/5
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/6
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/7
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/8
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Testing with database at /Users/sanitized/Projects/BreakDBreeze/BreakDBreeze/bin/Debug/database/9
Old value correct, ; Stored value incorrect, expected xxx0xxx, got
Old value incorrect, expected xxx0xxx, got ; Stored value incorrect, expected xxx1xxx, got
Old value incorrect, expected xxx1xxx, got ; Stored value incorrect, expected xxx2xxx, got
Old value incorrect, expected xxx2xxx, got ; Stored value incorrect, expected xxx3xxx, got

Old value incorrect, expected xxx3xxx, got ; Stored value incorrect, expected xxx4xxx, got

Closed Mar 9, 2015 at 8:40 AM by hhblaze
fixed in version 71

comments

hhblaze wrote Mar 1, 2015 at 12:28 PM

In many our projects we open more than one database instances per process and in the end of the process we close every instance of the database. It must work fine.

Opening database for a single transaction is not a correct usage of DBreeze.

DB instance must be open once in the beginnig of the process or DLL and closed (only for graceful termination, but can be avoided in case of violent termination) in the end of the process.

Nevertheless, I will take a look later what can be the problem here.

hhblaze wrote Mar 6, 2015 at 7:32 AM

I have tested, DBreeze works correctly, search mistake in your code.

hhblaze wrote Mar 6, 2015 at 8:52 AM

Nope, my bad, something is wrong there... will take a look.

hhblaze wrote Mar 6, 2015 at 10:08 AM

Issue is fixed in version 71

GWBasic wrote Mar 8, 2015 at 5:57 PM

Just downloaded your new release, and now my unit tests pass!

Thanks!

hhblaze wrote Mar 9, 2015 at 8:40 AM

Thank you for your reports.

I wanted to point

(from your example)
actualKey = BitConverter.GetBytes(key);

that DBreeze has integrated functions of converting simple datatypes into byte[]. Received byte[] will differ from BitConverter functions and will create sortable byte[].
So, you can easily use tran.Insert<int,double>(....etc
and DBreeze.Utils ByteArrayConversion different extension functions.

GWBasic wrote Mar 11, 2015 at 12:39 AM

Yeah, I'm aware. I'm still taking the time to learn how to use DBreeze and what patterns will work for the kind of application that I'm writing.

Thanks again!