Solved

SAS locks

Posted on 2011-09-26
8
2,556 Views
Last Modified: 2013-11-16
SAS 9.2 ts2m3.
Windows XP SP3
AIX 6.1

I am trying to understand the locking mechanism in SAS. We have a problem with our batch updates failing if a user is querying a dataset while we are trying to reload it. I would like to identify the lock, send an email and loop instead of having my process fail.

I tried this test and get unexpected results.
1) Start PCSAS
2) Connect to my SAS server on an AIX box.
3) Create a libname to the server.
4) Lock my dataset successfully. Lock libname.dataset;  From the log: "NOTE: MLQ_WKL.DAILYSALES.DATA is now locked for exclusive access by you."

5) Create a second PCSAS instance
6) Connect to the same AIX box.
7) Create the same libname as above.
8) Test the lock. Lock libname.dataset list; This returns the message "NOTE: MLQ_WKL.DAILYSALES.DATA is not locked or in use by you or any other users.
"

If I try to open the dataset in SAS Explorer I get the message below.

Why do I get the message in #8? And can I query SAS successfully for a lock to prevent my process from failing?

Sincerely,
T-Bone SAS message example
0
Comment
Question by:sanofi-aventis
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Accepted Solution

by:
d507201 earned 200 total points
ID: 36599429
I've never seen the lock function, and it's not in the 9.1 onl-line documentation's list of functions.  So I can't comment on what's going on there.  

I do know that unlike a true RDBMS, SAS applies file-level locking during update activity.  Go to the onl-line documentation and search for 'locking'... lots of useful hits.  To allow reading while writing, you need SAS/SHARE.  It lets SAS to apply record-level locking during updates.

To test if a dataset is locked, use the OPEN and CLOSE functions.  I haven't tested it, but I'm pretty sure that the OPEN function will fail if a dataset is locked.  So try to open the dataset and if the return code is zero then the open failed and you can send an email. and then abort the job  Non-zero means it worked--then CLOSE it and continue processing.

Something like this--

%MACRO TEST_IF_LOCKED(_datasetName);
%let dsid=%sysfunc(open(&_datasetName,i));
%if (&dsid = 0) %then %do;
  ** send email. ;
  abort;
%end;
%else %do;
  %close(&dsid);
%end;
%MEND;
0
 
LVL 11

Assisted Solution

by:theartfuldazzler
theartfuldazzler earned 200 total points
ID: 36599469
Hi

I would look to use the SYSLCKRC macro variable...  but the best is to look at the following link:

http://www.lexjansen.com/pharmasug/2005/posters/po33.pdf

Regards

D
0
 
LVL 7

Expert Comment

by:d507201
ID: 36599715
Very nice solution.  I'm always pleasantly surprised at the willingness of the folks at d-Wise to share their tips and clues.

I'd say that testing availability after opening and closing the dataset is maybe a bit much.  But if there's a chance of a timing error, I'd just put a 1-second SLEEP function immediately after the CLOSE function and not deal with locking and unlocking the dataset.  I use the sleep function quite often when I'm concerned about timing.

"Since the DATA step did some preliminary checking, it is likely that the lock statement will now succeed. However, due to split-second timing it is still possible that the lock statement will fail."

0
 
LVL 2

Author Comment

by:sanofi-aventis
ID: 36600128
Thanks guys. I've google this solution myself. I understand the concept of trying a file level open prior to using the file. I do not consider this querying SAS for a lock status. The Lock statement seems to be OK. Even though I have to query my log file to get the result, it does give me the process id that has it locked. I'm surprised the Proc Dataset, or one of the other procedures or even the Dictionary views, does not have an option to return the lock status.

At this time, I don't even plan to lock the dataset prior to using it, I just want to know if someone has it locked prior to truncating it and reloading.

and still not sure why the second PCSAS session returns the message "NOTE: MLQ_WKL.DAILYSALES.DATA is not locked or in use by you or any other users.
"
when I cannot open the dataset in that session's SAS Explorer. I get the message in the picture above.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Assisted Solution

by:Aloysius Low
Aloysius Low earned 100 total points
ID: 36647187
if it's PCSAS you are talking about (vs connecting to the same SAS instance on the server), a possible explanation is because it's by a different SAS instance, hence the lock status is not recognised? but when you try to open the dataset, it tries to get the dataset locked but only to find out that it was actually already locked...

i stand corrected here - i'm just speculating, making sense of what has happened...
0
 
LVL 11

Expert Comment

by:theartfuldazzler
ID: 36707699
Hi T-Bone

I must admit - I don't know AIX.

I have done this on a Windows server using www.sysinternals.com commands such as handle.exe, processExplorer and pslist.  In theory there are similar commands in Unix - but you would need to post this question (or a related one) on the IBM AIX UNIX Zone.

Despite my lack of AIX knowlegde, I did some more googling, and came across this link:
http://stackoverflow.com/questions/688343/reference-for-proper-handling-of-pid-file-on-unix

In theory, one would just need to find the PID file (I would assume it sits in the same directory as the SAS dataset), read in the PID file as text, and get the process id number that way?

I have no way to test this - if you gon't come right - try this question on the AIX Zone.

D
0
 
LVL 2

Author Comment

by:sanofi-aventis
ID: 36709376
All,

Thank you for your comments. But neither get me any further than assumptions already attained.

You would think that if the engine allows locks and can give a message like in the picture above that this information would be available to the programmer/analyst other than just an error code when trying to access the resource.

I will keep this question open for now.

Sincerely,
T-Bone
0
 
LVL 2

Author Closing Comment

by:sanofi-aventis
ID: 36815937
Thanks all.
You can lock files in SAS.
You cannot passively identify if there is a lock on a dataset.
Only by trying to lock it and querying the SYSLCKRC macro variable will you know if you can have a lock or not.
I will need to use the operating system to know who has it locked. For Unix I am going to try the fuser & ps command to get the users id. fuser for the pid then ps for the user id that has it closed.
I still need to figure out how to get the email name from the users id.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now