SQL Server database connection broken after a forced computer shutdown

1

My application uses SQL Server 2012 Express and an .mdf database to store data. Sometimes the end-user shuts down Windows and the computer (tablet) by keeping the power button pressed for 5 seconds. This is done while my application is still running. Note that the application starts full screen on startup replacing the Windows shell, so no access to Windows GUI, but there's a dedicated shut down menu button to use to shut down Windows and the computer safely.

When the forced shutdown is executed, the next time my application is started, it reports a database connection error. I don't have the exact exception message because it is not displayed, but connection to the database cannot be created.

Here's my connection string if it provides any help:

"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=true;Database=MeasDatabase"

I think the Database=MeasDatabase is just a relic from older days. You can probably ignore that. Works fine with it included, though.

Most of the time it helps if you shutdown the system the recommended way from the main menu. I.e., next time the database connection is created without any problems. But there has been at least one occasion where rebooting the system does not help. Had to re-install SQL Server and the related software.

So it looks like something (file handles etc.) is left open when the forced shutdown is executed, and the pieces are cleaned up the next time a proper Windows shutdown is executed.

Do you have any idea what might be causing this?

Is there something I could try code-wise (C#) to clean up any open database files etc., if the connection cannot be created?

I could try trigger a system restart if the connection could not be created, but not sure if that's a foolproof solution.

The end-users have no knowledge to do anything to fix the system, so it's either making the application handle the situation, or the computer has to be sent back for fixing. Currently the only solution is to emphasize the end-users not to force a system shutdown.

Added new info

The funniest thing... I tested forced shutdown a few time with a VirtualBox virtual machine. I was finally able to cripple the system so that the application did no longer start. The screen stayed black after the shell was loaded. I.e., my application failed to start so no shell to show.

Then I reverted the shell back to explorer.exe and restarted the virtual machine. Once the Windows desktop was loaded, I started my application. The process was stopped after a few seconds. There were error messages in the event viewer, which did not provide any help.

When I build the application into a debug version, copied the binaries to the virtual machine and started the debug version, it started without any problems. Then I started the original release version with administrator privileges (i.e. run as administrator), and it started without any problems. However, it failed when I started it normally (double-clicking).

The Windows user/account being used has only User privileges.

So this problem seems also to include Windows account/user privileges, as well. Looks like I need to test this some more with the virtual machine...

Added new info

Here are the event viewer entries (one Information and two Errors).


Fault bucket , type 0 Event Name: APPCRASH Response: Not available Cab Id: 0

Problem signature: P1: MainApp.exe P2: 5.2.0.28 P3: 5b0ffa7b P4: KERNELBASE.dll P5: 10.0.10240.16384 P6: 559f38c3 P7: e0434352 P8: 000000000002a1c8 P9: P10:

Attached files:

These files may be available here: C:\ProgramData\Microsoft\Windows\WER\ReportQueue\AppCrash_MainApp.exe_413415e952ca4992397ea1ba8821d95738eff9fd_1ae84d98_07bd340d

Analysis symbol: Rechecking for solution: 0 Report Id: a86731b4-87b7-4001-8491-142c67c13f5e Report Status: 4102 Hashed bucket:


Faulting application name: MainApp.exe, version: 5.2.0.28, time stamp: 0x5b0ffa7b Faulting module name: KERNELBASE.dll, version: 10.0.10240.16384, time stamp: 0x559f38c3 Exception code: 0xe0434352 Fault offset: 0x000000000002a1c8 Faulting process id: 0xdfc Faulting application start time: 0x01d3f8e55c5cac83 Faulting application path: C:\Program Files (x86)\Company\Application\MainApp.exe Faulting module path: C:\Windows\system32\KERNELBASE.dll Report Id: a86731b4-87b7-4001-8491-142c67c13f5e Faulting package full name: Faulting package-relative application ID:


Application: MainApp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.TypeInitializationException Stack: at MainApp.App.Main()


Looks like the application no longer starts with User privileges. I put a message box at the very beginning, but it didn't show up. Only when I ran the application with Administrator privileges.

Not sure if this virtual machine issue is a whole another story. The main focus should probably be on the database connection problem after a forced shutdown. This virtual machine issue does not go away with a safe restart, like the database connection problem does (according to the users).

Added new info

As a side-note, the problem with starting the application in the virtual machine appears to be some sort of security issue, or something related to version number.

I uninstalled the non-starting version, built a new version/installer with an incremented version number, and after installing on the virtual machine, it started normally. Then I uninstalled the new version, build another version/installer but with the original non-starting version number, and after installing, it didn't start. Go figure...

FYI, at least. Share the knowledge and so forth...

Added new info

Looks like killing the virtual machine corrupted the user.config file of my application. It is located in the AppData folder (e.g. C:\Users\[YourName]\AppData\Local\[CompanyName]\[ApplicationName].exe_[RandomString]\[VersionNumber]). The file was full of spaces. I deleted the file, and it was re-created when I started the application - and without Administrator privileges.

I thought it was weird that the application was terminated before it even got the main method. Felt more like an under-the-hood issue, and it was. So at least that got solved, event though it wasn't the main issue. :)

Maybe I'll implement that automatic computer restart if the connection to the database cannot be created. Makes the user's life a bit easier, even though he's the culprit. They pay your bills...

Hopefully it will be enough, but I'm still interested to hear if you get any other ideas to handle this.

c#
sql-server
shutdown
sql-server-2012-express
mdf
asked on Stack Overflow May 31, 2018 by SanApp • edited Jun 1, 2018 by SanApp

1 Answer

0

You can try the following within your application

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Verify that we're not being closed because windows is shutting down.
    if (e.CloseReason == CloseReason.WindowsShutDown)
    {
    //close all db connections 
    }
}

But, even with this it could be that force shutdown is doing something to SQL Server express outside of your application's control

answered on Stack Overflow May 31, 2018 by Eric Yang

User contributions licensed under CC BY-SA 3.0