Solved

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

Posted on 2013-11-14
3
385 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
ID: 39650024
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
ID: 39650097
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
ID: 39650132
& 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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