SQL CE fails after hibernate.

Good afternoon.

My development stats are VS2005 SP1 (problem still ocurred with no SP), WM 5.0, .NET 2, Symbol MC70 device.

My application opens and keeps open a connection to an onboard SQL CE database. I chose not to simply open and close the connection with each request as I have two threads, UI and worker, which both access the same DB through the same open connection (and there can only be one open connection on an SQL CE DB). Changing this behaviour is unfortunately not an option (and I hope this doesn't cripple me!!)

The problem is that if the device hibernates and wakes up, the very next call to the Read() method of an SqlCeDataReader throws an exception. The (very helpful) error message is "Unspecified Error".

I have added code to ensure that the connection still has a state of Open after the device comes back on but this did not help. I tried to debug the problem, but the device will not fully suspend when it is in the cradle so I cannot reproduce it in debug mode.

I might add the problem does not occur with other PDAs.

Does anyone know of the solution to my problem

Thanks in advance for any help.

Lee


Answer this question

SQL CE fails after hibernate.

  • Mjaiswal

    There are two possibilities:

    1. OS closes your application.

    2. Your application or anything it uses (NETCF, SQL Mobile) is on the storage card. As you already know, sleep/wake might invalidate file handles on a storage cards so applicaion moduless are no longer accessible and application terminates (or crashes on NETCF V1). See this.



  • Selectis

    Thanks for the very fast reply Ilya.

    You're right, I am using SQL Mobile, I don't know why I wrote SQL Server CE (force of habit ). And yeah, the DB is on a Storage Card.

    Now that you're telling me that SQL Mobile handles simultaneous connections, I might have to re-think my code a little.

    I've tried re-jigging things a little, so I Open() and Close() the SqlCeConnection on each request. Would you mind enlightening me on one point though; if you generate an SqlCeDataReader from an Open() SqlCeConnection(), is it incorrect to Close() the connection before you have Read() the reader

    I tried it that way but was informed the reader was already closed when I tried to Read() it. Later I'll try to Close() only the reader.

    And while you're here, you wouldn't have a favourite link on the topic of writing a Power Safe app would you

    Thanks mate for help here and my other posts. My app (and knowledge!) is all the better for it.

    Lee

  • kennm

    Aha. Thanks again Ilya. I should have guessed that it's not only SQL files on the Storage Card that might cause it to fail. All fixed.

  • Mosesm

    You can close connection after you done with reader. However, connection must remain opened while reader is in use (e.g. you're reading from it).

    See this for power aware applications.

    By the way, SQL Mobile has been renamed back to SQL CE. :)



  • jamesIEDOTNET

    That is expected if your database is on the storage card or internal none-volatile storage. Most storage card drivers would invalidate all opened file handles after wake/sleep cycle and your database would become inaccessible. You'd need to handle that appropriately, e.g. by catching the exception and reopening the connection or by making your application power aware. As to unclear error message, there might be more info in error collection.

    By the way, you could not really use SQL CE 2.0 with NETCF V2, it simply won’t compile. Either you're on NETCF V1 or you're using SQL Mobile which allows for multiple connections.



  • damonh78

    Well, having fixed my problems relating to a waking SQL CE (by moving my DB back to main memory), I seem to have introduced another bug.

    Now when my device wakes from hibernation, the app just suddenly exits with no exceptions thrown. It just disappears and the user is taken back to file explorer. I'm not even getting the exception I used to get when the device wakes.

    After reading a post about a similair problem, where the runtime will close the app under anomalous conditions, I have tried catching the Closing event on some of my forms, but this event doesn't fire. Even my handler for OpenNetCF's DeviceManagement.DeviceWake event doesn't fire.

    I just wondered what other things might be affected by the device waking up, apart from
    SQL CE connections My app is multithreaded and makes extensive use of SQL. There is no P/Invoke used so that can be discounted.

  • SQL CE fails after hibernate.