Jump to content

Is LOCK IN SHARE MODE a good solution here ?


Go to solution Solved by vinny42,

Recommended Posts

Hi all,

 

I am trying to create an event, that does an insert if a condition is met. For this insert, I initially wanted to set some locks. Unfortulately, I get a message saying " LOCK is not allowed in stored procedures". So I redesigned the event as follows. Would this work ? If so, would the LOCK IN SHARE MODE statement apply until after the IF (blablabla > 0) structure has been processed ?

CREATE DEFINER=`root`@`localhost` EVENT `event_name` ON SCHEDULE EVERY 1 MINUTE STARTS '2013-08-10 22:31:56' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN 	
	DECLARE blablabla
	DECLARE blablabla
        DECLARE ...


	SET blablabla
	SET blablabla
        SET ...

	IF (condition_met) THEN


		SELECT SQL_CALC_FOUND_ROWS .....from one table... LOCK IN SHARE MODE;
		SELECT FOUND_ROWS( ) into blablabla;

		IF (blablabla > 0)
			THEN

				INSERT INTO ........another table.....;


		END IF ;



	END IF ;

END


Thanks in advance for your help!

  • Solution

Locks should apply until you release them, ie: either you explicitely release the locks, or you end the transaction. The lock *should* last until the end of the procedure.

 

That waid, I' curious about what you are doing here, selecting a count to decide what to do, and locking records in a table that you are not updating sounds like you are trying to do something like:

 

INSERT INTO anothertable SELECT * FROM the_table_you_are_lcoking WHERE condition_for_the_locking_query;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.