locked
Controling the EntityConnection behaviour?? (multi-threading) RRS feed

  • Question

  • Hi all,

    i've been using EF 4 for my organization's intranet.

     

    Application History/Context:

    I've started to used the Long running context approach since the beggining. The application is multi-threaded and i've managed to create a connection bridge in order to avoid having multiple thread accessing the context at the same time. It works 99% of the time, but sometimes the connection would fail and it's not very easy to find where.

    Well the best solution would be to reduce the context scope to be thread specific... but this would require some time to restructure my data access layer, which i dont have for the moment

    Question:

    A solution that came to my mind was to that if i somehow manage to change the EntityConnection Open/Close logic and put a mutex to lock anyone from trying to open the connection while a thread is currently querying the DB, then that would solves all my problems.

    But the problem is i don't think there's any ways to override those logic right?

    Is there any way to change the EF Open/Close logic? I see that the EntityConnection class is NotInheritable :/

     

    Any ideas?


    • Edited by SlickRickD Thursday, September 22, 2011 9:39 PM
    Thursday, September 22, 2011 9:35 PM

Answers

  • Well actually im currently in the process or redesigning the Data Layer of my application in order to reduce the lifetime of my context to be thread specific.

    Thanks to you guys for participating in my discussion :)

    • Marked as answer by SlickRickD Thursday, May 10, 2012 5:22 PM
    • Unmarked as answer by SlickRickD Thursday, May 10, 2012 5:23 PM
    • Marked as answer by SlickRickD Thursday, May 10, 2012 6:10 PM
    Tuesday, October 11, 2011 10:17 PM

All replies

  • Hi SlickRickD,

    Welcome!

    I think you can refer this link: http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.connection.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 23, 2011 3:52 AM
  • Thanks for pointing me to this, i've already read this before. But it made me think of a quick solution... I can keep the Connection open for the whole lifetime of the application and it does work.

    Althought im wondering whats the implication of doing this? Is it bad to keep the EntityConnection opened if the LifeTime of the application is very long? (The application can be opened for a couple of day/weeks)

    Can anything go wrong by doing this?

    Friday, September 23, 2011 4:35 PM
  • Hi,

    For a web app it is best to keep the connection open as short as possible. The idea is that there is no point in keeping a connection open if nobody uses this at this time. This way you are able to share connections (and pooling eliminates the connection creation overhead) allowing to server x users with y<<x connections.

    That said it's a bit hard to give specific advice as it seems you used an unusual architecture. I would suggest to rather investigate the initial issue. How does it fails ? Why is it difficult to find ? Do you have some logging to trace what happens ?

    It's a bit hard to suggest a fix when we don't really know what is the issue you have...

     

    Another option could be to inherit from your context so that you can control what happens on it even if using the new class type can require a bit of search/replace job.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, September 23, 2011 5:06 PM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 28, 2011 3:28 AM
  • Well actually im currently in the process or redesigning the Data Layer of my application in order to reduce the lifetime of my context to be thread specific.

    Thanks to you guys for participating in my discussion :)

    • Marked as answer by SlickRickD Thursday, May 10, 2012 5:22 PM
    • Unmarked as answer by SlickRickD Thursday, May 10, 2012 5:23 PM
    • Marked as answer by SlickRickD Thursday, May 10, 2012 6:10 PM
    Tuesday, October 11, 2011 10:17 PM