Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Error checking in SQL Jobs Expand / Collapse
Author
Message
Posted Friday, September 13, 2013 6:30 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 13, 2013 3:42 PM
Points: 23, Visits: 36
Due to some data refresh and application requirements, we need to roll a database forward to our production server on a nightly basis. The issue is that since we now have some many connections getting to the DB, we can't gain exclusive access to the DB to run the restore. Then the DB gets stuck in Single User Mode and our applications get hosed. I wrote error checking code to try and prevent this from happening, but I guess I didn't write the code correctly because it didn't work.

There are a total of 4 steps to the job, and I think one of the steps might be an issue, not sure, so looking for insight, and that is outside of the error checking issue.

Any insight would be appreciated. Thanks!

Step 1: Kill Pids
Step 2: Change to single user mode
Step 3: Kill Pids again, in case there are any one hanging out there (this might be an issue, as it kills the pid that runs the job itself, so it seems.

Step 4: Restore the DB. Here is my code. There is a move and some sync users code in there, but this give you the idea of what I was attempting to do.

use master
go

IF @@ERROR <> 3101 --Error 3101 is for not obtaining exclusive access to DB.
BEGIN
RESTORE DATABASE [XXX] FROM DISK = N'\\mynetworkpath_mydb.bak' WITH FILE = 1,


END
ELSE
BEGIN
ALTER DATABASE [HRSADW] SET MULTI_USER WITH ROLLBACK IMMEDIATE
END;



Post #1494556
Posted Friday, September 13, 2013 9:05 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:12 PM
Points: 15,517, Visits: 27,896
Instead of changing it to single user mode, what about changing it to restricted user mode. Then only sa or dbo will be an issue and if you've set up security right, your users shouldn't have that level of access.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1494613
Posted Friday, September 13, 2013 9:57 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 13, 2013 3:42 PM
Points: 23, Visits: 36
Hi thanks - that will help with one of the issues we are having but I guess I didn't ask my question correctly.

What I am trying to understand is why my error checking code isn't preventing the DB from getting stuck in Single User mode?



Post #1494640
Posted Friday, September 13, 2013 10:01 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:12 PM
Points: 15,517, Visits: 27,896
Cause in the microsecond from you finishing killing all the sessions and then setting the database to single_user, another connection came in and took it over.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1494641
Posted Wednesday, September 18, 2013 6:04 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 13, 2013 3:42 PM
Points: 23, Visits: 36
Hi -

One more follow up question I am trying to understand how jobs really work. When I had this job as 'all in one step' it worked most of the time. but then I thought based on the increase in failures I was getting that I needed to redesign it. So I decided to redesign it and break it into 4 steps inside the Job. With it broken into 4 steps, and with making the suggested change, it still had the same issue, it couldn't kill the PIDS and would then fail.

So now, I thought, maybe put all the code into one step, if that code fails the job fails. But I don't understand how the change I made impacts how SQL Server Jobs work? Why does a job with multiple steps fail, but the same code in one step works?



Post #1495880
Posted Wednesday, September 18, 2013 6:37 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:12 PM
Points: 15,517, Visits: 27,896
If by working and not working you mean that putting in single user mode sometimes works better with a single batch than with multiple batches it's down to time and processing power on the CPU. If you multiple batches, there is just more time between commands than if we're talking about all the commands in a single batch. But, even in a single batch, there is still time in between commands. It's that time in between that you get a connection slip in on you and the single_user doesn't work.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1495894
Posted Wednesday, September 18, 2013 6:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 13, 2013 3:42 PM
Points: 23, Visits: 36
Hi -

OK so when I say step(s) you say batch(s) because that is how SQL executes it as batches. As far as the Single User Mode, I took your suggestion and changed it to RESTRICTED.

All that being said, since our own IIS server is creating the connections that don't allow the restore to happen, if it is in Multiple Batch mode, I tried turning off the offending App Pools via timer a minute before the SQL Job runs. Then 6 minutes later, the App Pools get turned back on. It is kind of clumsy because that job runs on a separate server inside Windows Tasks, but based on my limited knowledge of getting SQL Server A to talk to Web Server Z, that is the best I can come up with.

But what you are telling me about the execution of batches on SQL makes sense. Each batches timing is based on how long that server takes to run it. but if the batch is 'all inclusive' the timing is less, therefore the connections don't have as much time to re-establish themselves.

Since this is a production environment that I can't mimic it is hard for me to test all of this. But last night when the job ran at 1 AM I happened to be awake so I checked it. It ran successfully with the modifications I made. Gave a few errors of PIDS not being available to be killed, but that maybe because they were already killed and my Proc has them stored in a temp table to iterate through them, and when the execute of the KILL statement happens might have to be reviewed.

Thanks for the help.



Post #1495904
Posted Wednesday, September 18, 2013 7:04 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:12 PM
Points: 15,517, Visits: 27,896
I'm not a fan of killing sessions, but, instead of loading them into a temp table and then, presumably, using a cursor to clean them out, what about just using WITH ROLLBACK_IMMEDIATE on the SET DATABASE command. You can read more about it here. It's going to be much, much more efficient. But, be warned, it's going to be very efficient. Setting it by accident causes all sorts of problems.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1495911
Posted Wednesday, September 18, 2013 7:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 13, 2013 3:42 PM
Points: 23, Visits: 36
ok since our connections are read only web connections killing sessions doesn't hurt anything, IMHO. I do use the roll back immediate in my code to alter the DB, but if I can't get exclusive access to it, how can I alter it? that is the crux of the issue?



Post #1495917
Posted Wednesday, September 18, 2013 7:18 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 12:12 PM
Points: 15,517, Visits: 27,896
You shouldn't have to get that for restricted user in order to use the immediate rollback.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1495920
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse