Solved

Could the php mssql_query function be locking tables in the SQL database?

Posted on 2013-11-14
3
369 Views
Last Modified: 2013-11-14
Hi,  I am using the following code to query a remote SQL server with a select statement.  Could this be locking that database?

Thanks for any direction you can give!

$link = @mssql_connect($myServer,$myUser,$myPass);
mssql_select_db($myDB) or die('Couldn\'t load database');
$qry = "Select cast(GUIDProduct as varchar(36)) as myProductGUID, DATALENGTH(ProductPicture) as myImageSize, *
		from Product";
$rs = mssql_query($qry);
$RowCount = mssql_num_rows($rs);
if($RowCount>0){
	while($rows = mssql_fetch_assoc($rs)){
		//do something with recordset
	}
}

Open in new window

0
Comment
Question by:lthames
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
It could, yes.

Typically when a query is executed in mssql that query places a shared lock on the table(s) so other operations requiring an exclusive lock on the table(s) involved would need to wait until the shared lock is released.
If READ_COMMITTED_SNAPSHOT is set to OFF (the default), the Database Engine uses shared locks to prevent other transactions from modifying rows while the current transaction is running a read operation.
http://technet.microsoft.com/en-us/library/ms173763(v=sql.105).aspx

This behaviour can be modified e.g:

turning on the database READ_COMMITTED_SNAPSHOT option
including "with nolock" into the query sql

Note these are NOT the same thing, using "with nolock" can return as yet uncommitted records that subsequently might be rolled back.
0
 

Author Comment

by:lthames
Comment Utility
Thank you for your quick response.  

This definitely explains what's going on.  They are trying to import products into the database (which in that app requires exclusive lock during the import)  and at the same time my php script is syncing the products to their webstore . . . . . and their internet connection is so slow that a query that normally takes 1-2 seconds is taking 3 minutes!!!!!!!!!!
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
& Thanks for the quick grading :)

You will need to discuss this with the client then. If "syncing" I'd assume you only want valid data on the website, so using "with nolock" might be risky given that:
> an import might fail
> you are also being slowed down by connection speed.

They may be willing to use READ_COMMITTED_SNAPSHOT on perhaps?

Good luck with this project.

Paul
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

10 Experts available now in Live!

Get 1:1 Help Now