The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
DBI Locking/Blocking
Discuss DBI Locking/Blocking in the Perl Programming forum on Dev Shed. DBI Locking/Blocking Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 18th, 2013, 10:50 AM
|
|
|
|
DBI Locking/Blocking
I have a script that is run from a web page. I want to be sure that multiple scripts cannot access the database concurrently and cannot proceed until previously running script is complete. My thinking is to use table locking to accomplish both. If I Iock a table in one script and another starts, will that script be blocked until the table lock is cleared and will scripts queue up properly? The first thing the script does is make a DBI connection (MySQL). If that is successful, it then does a query and that is where I want it to block.
Is my thinking correct and will a simple LOCK TABLE do the trick? TIA.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

January 18th, 2013, 04:34 PM
|
|
|
|
It really depends on your database, but, usually, when you lock a record in write mode, other processes trying to access the same record will wait for the lock to be freed. This is done by the database engine, not by the Perl script.
|

January 18th, 2013, 05:02 PM
|
|
|
Quote: | If I Iock a table in one script and another starts, will that script be blocked until the table lock is cleared and will scripts queue up properly? |
You will need to explicitly code the logic in each of your scripts to accommodate the queuing otherwise, the query from the script which does don't own the lock will fail without retrying/queuing.
|

January 19th, 2013, 07:37 AM
|
|
|
Quote: | Originally Posted by FishMonger You will need to explicitly code the logic in each of your scripts to accommodate the queuing otherwise, the query from the script which does don't own the lock will fail without retrying/queuing. |
Thanks, that is what I was looking for. So perl will error rather than wait for a lock to clear. I'll have to read up on how to get the blocking behavior. I assume that is buried somewhere in the DBI docs.
|

January 19th, 2013, 09:22 AM
|
 |
!~ /m$/
|
|
Join Date: May 2004
Location: Reno, NV
|
|
Quote: | The first thing the script does is make a DBI connection (MySQL). If that is successful, it then does a query and that is where I want it to block. |
Just to be clear, are you going to block simple read access, or are you blocking updates?
If you are just querying, I wouldn't attempt blocking at all. If you are updating, you can use the transaction methods provided by DBI to request atomic changes (depends on your db engine as well). Even there, transactions appear best suited to wrap multiple related changes.
Just always check the DBI return codes for errors on any operation.
|

January 19th, 2013, 12:01 PM
|
|
|
|
There are really 2 aspects involved somewhat independent. One is normal transaction processing. However, the other is to prevent 2 scripts running at the same time. My hope was to use table locking for both which would minimize rewriting the script. However, it sounds like I really need to bite the bullet and rewrite the script using semaphore then the transaction aspect will be built-in.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|