English 中文(简体)
同步存储程序
原标题:Synchronized stored procedure execution in mysql

我的存储程序载于mysql,执行需要同步的任务,这样,如果两项申请要求所储存的程序,只有一人才能进入法典的一个部分来执行任务,使另一人受阻,直到任务完成后。

DELIMITER $$
CREATE PROCEDURE SP_GEN_ID(IN NAME VARCHAR(20))

BEGIN 
  DECLARE maxLen int default 0;
START TRANSACTION;
   #the section of code that needs to be synchronized
COMMIT
END;
$$

DELIMITER ;

因此,如果两项申请同时采用所储存的程序,任务必须同步进行。

a. But Start TRANSACTION and COMMIT did NOT synchronize the execution.

b) 而LOCK TABLES tableA不能用于储存程序以确保同步。

c) 我试图使所储存的程序达到应用水平。 I 旧

提升——内部处理范围:24小时(a);

该公司在提高1.41英寸方面做得很好。

但是,在1.34个图书馆中,锁定双向图书馆没有得到支持,而这正是我的案例。

是否有办法使所储存的法典程序部分同步,这样,当同时发出两条呼吁时,有人在对方被执行之前被阻断?

(added the following) edited code: to give an idea what I am trying to perform in the synchronized block of the stored procedure.

It gets the last assigned id, and increment it by one and check whether it is not used for someother name record. When a valid id is found, update the last assigned id record table and then associate that with the name given.

DELIMITER $$
CREATE PROCEDURE SP_GEN_ID(IN NAME VARCHAR(20))

BEGIN 
  DECLARE maxLen int default 0;
START TRANSACTION;
   #the section of code that needs to be synchronized
    SELECT lastid into lastgenerated FROM DB_last_id WHERE key =  NAME_ID ;    
findid_loop:
    LOOP
    set lastid = lastid + 1;
    #this is to check whether it was assigned with someother name before.
    IF not EXISTS (SELECT 1 FROM user_name_id WHERE name_id = lastgenerated) then
                     set nameid = lastgenerated;
                     set found = true;
                     LEAVE findid_loop;
            END IF;

            #for loop limit check
            IF (counter < loopLimit) then
                    set counter = counter + 1;
                    ITERATE findid_loop;
            ELSE
                    #reached the limit and not found.
                    LEAVE findid_loop;
            END IF;
    END LOOP findid_loop;

     #if a valid id, update last id and assign to name.
     IF (found) THEN
            #update the id.
            update DB_last_id  set lastid = nameid where key =  NAME_ID ;
            insert into user_name_id values (nameid ,name);
     ELSE
            #return an empty string for the application to take action on the failure.
            set nameid =   ;
    END IF;
#end transaction here.
COMMIT

END;
$$

DELIMITER ;
最佳回答

开始与《削减和限制进攻性战略武器条约》的交易实际上并未开始。 《削减和限制进攻性战略武器条约》之后的第一个表格是这样。 开放交易也意味着同意控制。 如果你需要,你可以依靠咨询锁系统MySQL通过<条码>GET_LOCK(、<条码>、RELEASE_LOCK()和其他一些相关职能。

目前通过交易实行一致控制的一种替代办法是依靠排他性锁。 由于<代码>SlectT的声明在InnoDB中不是锁定的,因此发出这种质询开始交易,但既不设任何锁,也不尊重原有的锁。 如果早先的交易是以相同信息(行文)进行,则须使用<代码>。 表格。 例如:

START TRANSACTION;
SELECT lastid into lastgenerated FROM DB_last_id WHERE key =  NAME_ID  FOR UPDATE;
...

With this construction there will never be two concurrent transactions operating on the same NAME_ID past the SELECT statement that was explicitly told to perform a locking read.

问题回答

如我上述评论所述,你认为交易足以满足大部分需要;然而,如果你需要明确等待其他呼吁的完成,则使用<<<<<<>>/a>:

三、使用<<<><><><>><>>><>>>/> >/><>/strong>的序号,使用<><><>><><>>>> 准时/>><>>>>>><>>>>>><>>>>>>/>>>。 如果成功获得1>,如果试图删除(例如,由于另一个客户此前已经锁定了名字),或NUL,如果发生错误(例如从记忆中排出或read在上rel=“诺referer”<>>>>>>>>>>>>>>>> GET_LOCK(),在您执行GET_LOCK() 与交易没有互动。 也就是说,进行交易不会释放交易期间获得的任何此类锁。

这一职能可用于实施申请锁或模拟记录锁。 姓名在全服务器上锁定。 如果一个客户锁定了名字,GET_LOCK(>/a> 阻止另一客户以相同名称提出的任何要求。 这使同意特定锁定名的客户能够使用姓名来进行合作咨询锁。 但应认识到,这还使不属于合作客户的客户能够无意或故意地打上名字,从而防止任何合作客户锁定这一名称。 减少这种可能性的一个办法是使用具体数据库或具体应用的锁名。 例如,使用<>>>> 代码>db_name.str><<>>>>>>>>>>> > > 名称、>

mysql> SELECT GET_LOCK( lock1 ,10);
        -> 1
mysql> SELECT IS_FREE_LOCK( lock2 );
        -> 1
mysql> SELECT GET_LOCK( lock2 ,10);
        -> 1
mysql> SELECT RELEASE_LOCK( lock2 );
        -> 1
mysql> SELECT RELEASE_LOCK( lock1 );
        -> NULL

第二个RELEASE_LOCK() 由于24小时 lock1 在第二GET_LOCK( 页: 1

If multiple clients are waiting for a lock, the order in which they will acquire it is undefined and depends on factors such as the thread library in use. In particular, applications should not assume that clients will acquire the lock in the same order that they issued the lock requests.

Note
Before MySQL 5.5.3, if a client attempts to acquire a lock that is already held by another client, it blocks according to the timeout argument. If the blocked client terminates, its thread does not die until the lock request times out.

这一功能对于基于声明的推广是不安全的。 从MySQL.5.1开始,如果你在binlog_format 时使用这一功能,就会发出警告。 页: 1 (Bug # 47995)





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签