Windows Server Remote Desktop Problem
I have a problem with remote desktop.
A user logs in and opens a particular program. If the user closes the connection with out closing the program then the program continues to run until the server kills the process. This causes an un-graceful shutdown of the application that locks what ever the user was doing in the database. The solution so far has been to go into the database and unlock everything manually. This event occurs too often to do this on a regular basis. I'm not sure even where to begin. I have googled, "how do I capture the remote desktop close event". But haven't gotten much good info yet.
Anyone have any suggestions?
Comments
In your app if someone clicks the close button or goes to File->Quit then you are executing the code to stop your database activity and close your database connection.
If someone clicks Start->Log Out on a modern Windows system, they don't actually close their session. They aren't supposed to. They are supposed to be able to switch user again and resume from where they left off. That even includes any processing. So I shoudl be able to login to a computer as Scott, start a video encoding, then switch user to Rym and play a game, then switch back to scott later and the video encode will be finished.
The real problems here are why is the database locked just because someone left the app open? Why does your app crash just because it is open for a long time? Neither of those things should happen ever regardless of any remote desktop or logging out.
A crappy duct tape solution is to do this. Every time the application needs to interact with the database it starts a new connection, does its work, then closes the connection. This way even if the app is running, it won't be connected to the database unless it is actively doing some task.
Switch user = lock current session an allow another session to also log in. It's not crashing. People are closed the RD session, which locks the session rather than log it out. However by default there's a 10 minute time out at which point windows will force close everything in that session and log it out. That's why his process is getting killed. If you allow sessions to persist then you can log back in and close it properly.
If that is not the case, then the problem might be simpler than that. Your app just needs to listen for the log off/shutdown event and when it sees that event, shut down safely.
My impression is that the session stays open for X seconds if you disconnect improperly and then the server kills the session after X seconds. This process kill doesn "shutdown" apps running in the session it just kills them meaning that don't run their shutdown processes to resolve what they are doing.
I think you are trying to solve the problem of one user writing, then another user writing on old data blowing away the first user's changes. That problem is much better solved many other ways.
On way is to use something like couchdb, which is built to handle that exact situation.
With a relational Database, you probably want to do things Wordpress style. In WordPress when you write a blog post it actually has two tables. One table is for the posts and the other table is for revisions. When you create the post you get a row in each table, obviously with a foreign key from revision to post. Every time you edit the post, it inserts another row into the revision table. When you visit the blog, you always see the data from the newest active revision on the post. You never update any data in the db, you only ever insert new rows. This way no data is ever lost.
On top of that you want all of these inserts to be performed in transactions, so they are atomic and always happen in order. Then when someone submits a new revision they include the id of the previous revision. If the id they submit is not the id of the latest revision, send them back telling them that data has been changed while they were editing. Give them a screen showing them the newer data and the data they submitted allow them to reconcile and submit again. This doesn't work if you have a ton of people writing the same thing simultaneously, but that shouldn't be a problem.
Also, you might want to take a look at how Wikipedia handles this problem. They basically use typical diff/merge version control if multiple people edit the same wiki page simultaneously.
user configuration => admin templates => windows components => terminal services (Remote desktop if 2K8) => sessions
So when someone closes a remote desktop connection with the program running, it will continue to run because it thinks the user will come back at some point. After X amount of time, the server will terminate the remote desktop session and kill all programs running in it. This leaves the documents in a locked state that can only be unlocked if the user logs back on and unlocks them manually or someone who can access the database changes their locked status. Note: Even if the documents are locked, they can be read, just not written to.
I know this program sucks, but there is nothing I can do about it. My professor essentially is dumping his bitch work on me in the name of my independent study and holding my grade hostage so I can solve his problems.
But whatever, this problem is solvable I think.