Problem running database functions from Task

Nov 10, 2015 at 11:00 AM
Hi Everyone.

I am using Dbreeze on mono on linux.

I have a function that runs in a task that accesses the Dbreeze database. When I perform actions on the database like setting Technical_SetTable_OverwriteIsNotAllowed or sometimes Inserting values the Task hangs with no error messages.

I have wrapped the database functions in a try catch loop and when stepping through with the debugger the code just seems to stop when executing those steps and doesn't throw any exception.

I can read the database perfectly but cant seem to insert any new values.

the managed threadid of the trasaction is the same as the one for Task thread that is trying to access the database.

If i take the function out of a task and just run it there are no problems at all and everything works as expected.

Any ideas? This is really annoying as I am very close to finishing my project.

Thanks

Dean
Nov 10, 2015 at 1:18 PM
Hi, please publish your example code.
Nov 10, 2015 at 1:23 PM
But I can tell, with a very high probability, that transaction and execution threads have different IDs (Thread who creates transaction and thread who insert value are different).
Nov 10, 2015 at 1:24 PM
I will try and create some example code tomorrow to see if I can recreate the problem.

I wondered if there was anything obvious I had missed to do with threads. I create the database connection in one thread, but the transaction is created in the thread that is doing all the database work.

Is this the correct way to do it?

Thanks

Dean
Nov 10, 2015 at 1:30 PM
When I step through the code and look at the transaction object it has the same managedthreadid as the thread that is running the code.

In my application logging the threadid is logged as part of the message and the debug message that occurs before the database operation has the same id as the transaction when I watch the transaction during debugging.

So i think it should work.

I will try and recreate and let you know.

Thanks

Dean
Nov 10, 2015 at 7:57 PM
Edited Nov 10, 2015 at 7:58 PM
DBreeze engine must be instantiated only once per program.
But transaction and its content must be in one thread.
DBreezeEngine engine = null;

void Main()
{
   engine = new DBreezeEngine(path);
}

void Test(){

System.Threading.Tasks.Task.Run(() =>
            {
                //init of transaction and ...    
                using (var tran = engine.GetTransaction())  
                {
                    //...and its content must be in the same managed thread, like here. 
                    //This examples works.
                    tran.Insert<int, int>("t1", 1, 1);    
                    tran.Commit();
                }

                //check if all insert was ok
                using (var tran = engine.GetTransaction())
                {
                    Console.WriteLine(tran.Select<int, int>("t1", 1).Key);
                }
            });
}
Nov 11, 2015 at 10:16 AM
Maybe that is where I am going wrong.

My application has 2 classes which each have their own database (completely separate purposes). So I have 2 DBreeze engines in my application. Is this permitted?

Thanks

Dean
Nov 11, 2015 at 10:22 AM
It's allowed as many instances of DBreeze (DBreezeEngines) as you want. Of course they must refer to different folders.
Each of them must be instantiated only once, they can be static variables for easy access among the complete program.
Nov 11, 2015 at 10:31 AM
Fixed it,

Thank you for your help. Looking back through my code I did not Dispose the transaction after committing it before I changed threads. I just set it to null.

It all looks perfect now.

Thank you for an excellent product.

Regards

Dean
Nov 11, 2015 at 10:35 AM
Excellent!