Solved

MySQL concurrent procedures messing with same rows

Posted on 2016-07-18
2
73 Views
Last Modified: 2016-07-19
I have a couple long running SQL PROCs, on the order of 2 to 8 minutes and I need to have the operator able to run both simultaneously.

In the attached image you can see that I finally caught it in doing what I knew it would someday do.

My situation is USUALLY:

Check if a record exists via COUNT(*)
If not
  Create
else
  Update

In both cases, I also sometimes INS/UPD other tables.

This last criteria of affecting more than the one table, by my understanding rules out using SELECT FOR UPDATE as I need the other tables also synchronized.
So I'm using START TRANSACTION WITH CONSISTENT READ
(Yes, Innodb is in the correct transaction mode to support this.)

My questions.
1) Confirming my approach vs. FOR UPDATE or SHARED READ approaches.

The more interesting question:
2) I also have intensive auditing going on inside that IF/THEN within the TRANSACTION.

The auditing is done via a CALL to AuditThis(...) PROC.
2a)  Are procedure boundaries irrelevant WRT Transactions?

If relevant, then can I do the transaction in the primary PROC and remain in it once returned from AuditThis() ?


If irrelevant, it seems I need to not do a COMMIT within AuditThis().
Correct?

2b)  If so, what's a good way to keep AuditThis() informed whether it is within a transaction?

The simple answer is 'do not commit within AuditThis()'.
But earlier I was having the two primary PROCs both calling AuditThis() and getting irregularities...

I may just skip any COMMITs w/in AuditThis() and do a COMMIT on return (if not in a Transaction),
    but if it ends out I can't get away with that at other times
    because calls to AuditThis() aren't always w/in a transaction,
I may have to conditionally COMMIT within AuditThis().

IFF I have to do that, I was thinking of using something like:
SELECT NOT COUNT(*) INTO CommitHere
FROM zWithinTransaction ;

# Then
IF CommitHere THEN COMMIT ; END IF ; 

Open in new window


Have I gone over the edge on a goose chase?

Thanks all!
Finally-Caught-it-.PNG
0
Comment
Question by:Ralph
2 Comments
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 41719227
I don't get what the screenshot has to do with your question. What do the two procs you talk of do? 1.1 What is the intent? 1.2 What is the reality? and 1.3 What is the problem you see.

From what you describe in general I assume your audit does not match what you see in tables, but it's hard to guess what the problem is from your screenshot alone.

In general, and that's not only true for MySQL, transactions do work for a single session and the isolation level of OTHER transactions determines whether they see changes not yet commited or not. Your own transaction usually isolates it's own changes from other transactions only and not from itself. In a "consistent read" snapshot mode you also don't read your own changes, you read the values from the snapshot time.

Transactions are not only valid for a single proc and stack level of your code, called functions also take part in them, so eg an Audit storing a new record also is rolled back, when you finally not commit but ROLLBACK, unless you audit by writing to a txt log file, for example. If you do the auditing calls to be able to track and profile what's going on including things finally rolled back, your auditing can't take part in transactions. In a certain sense it's correct the auditing of something not committed also disappear, indeed the ROLLBACK action also has to be audited to see what was audited was not committed.

Bye, Olaf.
0
 

Author Closing Comment

by:Ralph
ID: 41719861
In case the other comment does not get saved, re-posted here.


Yes, I definitely should have taken a break and re-read what my over-focused mind was trying to convey...

Because function/proc invocations ARE within the scope of a transaction started before the call and commited after the call, I found out that I also have to suppress the COMMITs within those modules.

But sometimes, elsewhere in the code I WILL call the AuditThis() Proc when not in a transaction, so I had to work around that so that that (and others) will do the commits it needs.
I did that by inserting a record ,(COUNT(*) <> 0), into a 'flag' table and within the PROC using that to determine if I need to do a commit there or not.
Amazingly it does work!

My (1) question was whether I should use transactions or select for update/shared read.  I'd answered that myself and went for Transactions.

Thanks Olaf,
Ralph
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
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…

708 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

19 Experts available now in Live!

Get 1:1 Help Now