douzhi7661 2014-02-13 16:26
浏览 143
已采纳

使用TRIGGER在mySQL INSERT上进行计时问题

For an iPad app I have created a web service which creates a new table row in my teams table when the user starts the app. The PHP generates a unique ID and fills some other fields as well.

Additionally I have created a trigger on the teams table in mySQL on insert where a teamnumber is generated automatically depending on the already inserted rows for that specific day, project and group.

Unfortunately I seem to have a timing issue when inserting new rows to my table sometimes. If two apps create a team in the same second (milisecond?) the result of the mySQL trigger will be the same teamnumber for both apps. So instead of having teamnumber 1 and 2 both apps have teamnumber 1.

My teamtable looks something like this:

TeamID | pProject | group | teamnumber | languagecode | created_at

And the trigger for mySQL:

CREATE TRIGGER after_team_insert BEFORE INSERT ON teams 

FOR EACH ROW
BEGIN

SELECT COUNT( * ) INTO @counter
FROM teams
WHERE DATE( created_at ) = DATE( NOW( ) )
AND teams.group = NEW.group
AND teams.pProject = NEW.pProject;

SET NEW.teamnumber = CAST(@counter AS UNSIGNED) + 1;

END;

I think my problem has something to do with parallel threads that insert rows into the database and therefor get a wrong count when the trigger is working.

Does anyone have a solution for this problem or do I have to use some kind of queue in PHP to prevent this doubling of teamnumbers?

Thanks in advance, Carsten.

  • 写回答

2条回答 默认 最新

  • dqy012345 2014-02-13 16:31
    关注

    I guess you can use a lock table ! http://dev.mysql.com/doc/refman/5.0/fr/lock-tables.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器