Page 1 of 1

Working with threads

Posted: Mon 09 Jul 2018 05:34
by jwrz
Hi, I would like to ask what are the recommendations for working with many threads (and many Contexts - these issues overlap in my opinion). Are EntityDAC mechanisms thread save?

How to work with Context - in Entity Framework, Context should be created for the smallest period of time (very fast warm start). At EntityDAC we have a special component that stores the main Context - I think that is a bad solution, for example, can lead to random saves or rejects. I'm afraid that in the case of the ISAP server side application, when IIS automatically paralleled the execution of queries, everything will fall apart.

PS I'm asking here because the documentation is silent on this topic.

Re: Working with threads

Posted: Tue 10 Jul 2018 08:15
by MaximG
Currently, EntityDAC does not support the work in multithreaded applications. We will add this possibility in one of the next builds of our product.

Re: Working with threads

Posted: Tue 10 Jul 2018 13:34
by jwrz
Hi, this excludes use in the most popular business application model - multi-layered with server side logic (at least for IIS).

Could you more accurately estimate when it can be. We are working on the concept of a new project, if this time is not too long we can continue working with EntityDAC, otherwise I will have to choose another system (we have two or three months to take decision). We would prefer not to do it because already known it and it suits us (except for the lack of Eager Loading, but it is not critical - I hope you will implement it one day, so I could optimize queries in the future).

Re: Working with threads

Posted: Fri 13 Jul 2018 10:49
by ZEuS
We are already working on the implementation of this functionality. We plan to release the next release this autumn. In order for you to test the functionality, we will release a beta version within a month and provide you with it.

Re: Working with threads

Posted: Mon 16 Jul 2018 10:40
by jwrz
Thank you very much for the information. I'm looking forward.

Re: Working with threads

Posted: Thu 16 Aug 2018 14:30
by anykey
According to the newly released v.2.0.1, Contexts are multi-threaded. I haven't noted any differences in this regard.

I'm working on a project where I have a Windows Service that has a main thread that spawns potentially dozens of worker threads. What I have done to is I have a dataModule in the project that has the EntityContext/EntityConnection etc., then I instantiate that data module in each worker thread. They play in their own memory pool. They live happily ever after.

The challenge becomes Thread A writes to an entity, Thread B could potentially have an older version in it's own cache. So, to combat that, before using the objects, you want to execute a .Reload of each entity first.

My front end is an IntraWeb 14 web app which of course is multi-threaded, and has plumbing to handle some of that for me. Each "UserSession" is managed under a class that has it's own DataModule (reused from the server project). Seems to keep everything partitioned.

Hope that helps?

Re: Working with threads

Posted: Wed 22 Aug 2018 05:46
by jwrz
To anykay: nice workaround but i think quite inefficient.

To MaximG/ZEuS: are there any differences in usage or recommendations for working with threads? I was looking for this in documentation, but there is nothing about this topic.

What about my second question:
How to work with Context - in Entity Framework, Context should be created for the smallest period of time (very fast warm start). At EntityDAC we have a special component that stores the main Context - I think that is a bad solution, for example, can lead to random saves or rejects.
What happen when one thread call save to DB?

Re: Working with threads

Posted: Fri 31 Aug 2018 13:37
by MaximG
..."What I have done to is I have a dataModule in the project that has the EntityContext/EntityConnection etc., then I instantiate that data module in each worker thread."
To work with EntityDAC in multiple threads, you should use a separate EntityContext for each thread


... "The challenge becomes Thread A writes to an entity, Thread B could potentially have an older version in it's own cache."

Indeed, such a situation is possible when two conditions are fulfilled:
- using caching : TEntityContext.Options.Cache.Enable : = True. In this case, Thread A and Thread B will work with the shared cache.
- work of two EntityContexts in one EntityConnection
To implement the independent behavior of two threads, it is enough to use a separate EntityConnection for each EntityContext or to disable caching: TEntityContext.Options.Cache.Enable := False

Re: Working with threads

Posted: Fri 31 Aug 2018 13:38
by MaximG
... "are there any differences in usage or recommendations for working with threads? I was looking for this in documentation, but there is nothing about this topic"
To work with multiple threads, we recommend using a separate EntityContext for each thread. We will describe the detailed recommendations in our documentation shortly

... "What happen when one thread call save to DB?"
In this case, data will be saved to DB

Re: Working with threads

Posted: Sun 02 Sep 2018 08:35
by jwrz
"To work with multiple threads, we recommend using a separate EntityContext for each thread. We will describe the detailed recommendations in our documentation shortly"
I will look for doc, but I am wondering is this context should be created for whole time of threads live or like in Entity Framework for the shortest possible time (create -> CRUD -> free)?

"... "What happen when one thread call save to DB?"
In this case, data will be saved to DB"
;)

Re: Working with threads

Posted: Wed 05 Sep 2018 13:16
by MaximG
You can create and work with the context while the thread exists, as well as work with contexts using the method you describe: it does not affect the thread safety of the application you are developing