Working with threads

Discussion of open issues, suggestions and bugs regarding EntityDAC
Post Reply
jwrz
Posts: 18
Joined: Mon 22 Aug 2016 05:30

Working with threads

Post by jwrz » Mon 09 Jul 2018 05:34

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.

MaximG
Devart Team
Posts: 1134
Joined: Mon 06 Jul 2015 11:34

Re: Working with threads

Post by MaximG » Tue 10 Jul 2018 08:15

Currently, EntityDAC does not support the work in multithreaded applications. We will add this possibility in one of the next builds of our product.

jwrz
Posts: 18
Joined: Mon 22 Aug 2016 05:30

Re: Working with threads

Post by jwrz » Tue 10 Jul 2018 13:34

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).

ZEuS
Devart Team
Posts: 240
Joined: Thu 05 Apr 2012 07:32

Re: Working with threads

Post by ZEuS » Fri 13 Jul 2018 10:49

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.

jwrz
Posts: 18
Joined: Mon 22 Aug 2016 05:30

Re: Working with threads

Post by jwrz » Mon 16 Jul 2018 10:40

Thank you very much for the information. I'm looking forward.

anykey
Posts: 8
Joined: Mon 14 May 2018 14:39

Re: Working with threads

Post by anykey » Thu 16 Aug 2018 14:30

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?

jwrz
Posts: 18
Joined: Mon 22 Aug 2016 05:30

Re: Working with threads

Post by jwrz » Wed 22 Aug 2018 05:46

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?

MaximG
Devart Team
Posts: 1134
Joined: Mon 06 Jul 2015 11:34

Re: Working with threads

Post by MaximG » Fri 31 Aug 2018 13:37

..."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

MaximG
Devart Team
Posts: 1134
Joined: Mon 06 Jul 2015 11:34

Re: Working with threads

Post by MaximG » Fri 31 Aug 2018 13:38

... "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

jwrz
Posts: 18
Joined: Mon 22 Aug 2016 05:30

Re: Working with threads

Post by jwrz » Sun 02 Sep 2018 08:35

"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"
;)

MaximG
Devart Team
Posts: 1134
Joined: Mon 06 Jul 2015 11:34

Re: Working with threads

Post by MaximG » Wed 05 Sep 2018 13:16

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

Post Reply