Using the same connection in multiple threads -> native exception

I found a peculiar thing today while working with SQL Mobile in a multithreaded application (VS2005, application for Pocket PC 2003).

I created a class which has one SqlCeConnection object. Every time I call a function to insert/select/delete something from the local db, I open the connection, execute the query an then close the connection again.

But when I'm calling a function from the db class in thread 1 and in the meantime call a different function (from the same db class of course) in thread 2, things go wrong. Because when function 1 wants to close the connection, function 2 is still using the connection and it will crash my application with a native exception (0xC0000005: access violation).

I can see why the error is happening, but shouldn't there be a nice .NET handled exception instead of a native exception which grinds my app to a hold

(A workaround I use now is to use multiple connection objects instead of one, but I thought I'd give this feedback anyway)



Answer this question

Using the same connection in multiple threads -> native exception

  • tony42472

    I am using SQL Mobile 2005 and .Net CF 2.0 but my multithreaded application which has separate connections for each of the thread, is throwing Native exception(Error - 0xc0000005 ).

    I have started a new thread for the same http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=983769&SiteID=1

    Any help in this regard would be highly appreciated.

    Nikhil


  • Melinda_MSFT

    1) You should have a connection (SqlCeConnection) for each thread separately.

    2) The error you are getting is general multi-threaded programming error. The same error you would get if you have a memory pointer between two threads. One thread is accessing it and while the other thread frees it. This is not a SQL Mobile DB problem.

    3) What SQL Mobile operations you are trying to do.

    Can you please elaborate your environment and operations in details. What does your app do

    Thanks,

    Laxmi Narsimha Rao ORUGANTI, MSFT, SQL Mobile, Microsoft Corporation



  • Devin G

    I thought I fixed it by using multiple connection objects, but as it turns out I still get the access violation error.

    Is there anything special I have to set for using a SQL Mobile db in a multithreaded environment


  • Yassir Zoheiri

    If you're using SQLCE V2 then you can only use one connection to the database period. I had to synclock my db access layer. so each call from each thread waits for the others to finish. Big Mess, luckily SQL Server 2005 Mobile allows multiple connections. If you're using VS2005 and writing to .NETV2 you may want to switch ot SQL Server 2005 mobile.

    If you are already using that version, sorry unknown issue :)

    Robert Brown


  • Using the same connection in multiple threads -> native exception