Select .. for update question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jayson_13

    #1

    Select .. for update question

    Hi,

    I need to implement a counter and i face problem of locking so hope
    that u guys can help me.

    I try to do test like this :

    1st connection
    SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
    (when i execute this statement and i guess that this will lock the
    record and prevent other connection to update that record right?)


    2nd connection
    Update nextkey SET inextkey = inextkey + 1 WHERE tblname = 'PLCN';
    (I execute this in another connection after i have executed the
    select for update statement in the first connection. However, this
    statement does update the record!!!)

    My major concern is that how can i prevent 2 ppl getting the same
    counter. TQ!!!!!!!!


    I am using InnoDb table + Mysql 4.0.17
  • Chris Hope

    #2
    Re: Select .. for update question

    jayson_13 wrote:
    [color=blue]
    > Hi,
    >
    > I need to implement a counter and i face problem of locking so hope
    > that u guys can help me.
    >
    > I try to do test like this :
    >
    > 1st connection
    > SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
    > (when i execute this statement and i guess that this will lock the
    > record and prevent other connection to update that record right?)
    >
    >
    > 2nd connection
    > Update nextkey SET inextkey = inextkey + 1 WHERE tblname = 'PLCN';
    > (I execute this in another connection after i have executed the
    > select for update statement in the first connection. However, this
    > statement does update the record!!!)
    >
    > My major concern is that how can i prevent 2 ppl getting the same
    > counter. TQ!!!!!!!!
    >
    >
    > I am using InnoDb table + Mysql 4.0.17[/color]

    Google is your friend:


    The first entry is the page you want from the MySQL manual.

    --
    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • Heikki Tuuri

      #3
      Re: Select .. for update question

      Jayson,

      ----- Original Message -----
      From: "Chris Hope" <blackhole@elec trictoolbox.com >
      Newsgroups: mailing.databas e.mysql
      Sent: Friday, October 22, 2004 4:02 AM
      Subject: Re: Select .. for update question
      [color=blue]
      > jayson_13 wrote:
      >[color=green]
      >> Hi,
      >>
      >> I need to implement a counter and i face problem of locking so hope
      >> that u guys can help me.
      >>
      >> I try to do test like this :
      >>
      >> 1st connection
      >> SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
      >> (when i execute this statement and i guess that this will lock the
      >> record and prevent other connection to update that record right?)
      >>
      >>
      >> 2nd connection
      >> Update nextkey SET inextkey = inextkey + 1 WHERE tblname = 'PLCN';
      >> (I execute this in another connection after i have executed the
      >> select for update statement in the first connection. However, this
      >> statement does update the record!!!)
      >>
      >> My major concern is that how can i prevent 2 ppl getting the same
      >> counter. TQ!!!!!!!![/color][/color]

      you have probably not set

      SET AUTOCOMMIT=0

      and the lock by the 1st connection is released in the autocommit immediately
      after you execute the SELECT ... FOR UPDATE.
      [color=blue][color=green]
      >> I am using InnoDb table + Mysql 4.0.17[/color]
      >
      > Google is your friend:
      > http://www.google.com/search?q=mysql+lock+table
      >
      > The first entry is the page you want from the MySQL manual.[/color]

      Using LOCK TABLES is not a very good method for InnoDB tables, because
      MySQL's table locking mechanism does not have deadlock detection in
      combination with row locks inside InnoDB. We will soon implement LOCK TABLES
      TRANSACTIONAL that will work like in DB2, SQL Server, Oracle, and will be
      the recommended method for locking InnoDB type tables.
      [color=blue]
      > --
      > Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/[/color]

      Best regards,

      Heikki Tuuri
      Innobase Oy
      Foreign keys, transactions, and row level locking for MySQL
      InnoDB Hot Backup - a hot backup tool for InnoDB which also backs up MyISAM
      tables
      http://www.innodb.com/order.php

      Order MySQL technical support from https://order.mysql.com/


      Comment

      • jayson_13

        #4
        Re: Select .. for update question

        Hi Turri,

        I am testing it using mysql front 2.2. The following is how i
        test... please tell me what the hell i have done wrong... TQ !

        1st Connection
        SET Autocommit=0;
        SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
        (if i am not mistaken, at this point, the transaction is not yet
        completed. so the record supposed to be locked right?)

        2nd Connection
        Update nextkey SET inextkey = inextkey + 1 WHERE tblname =
        'PLCN';

        The 2nd connection still manage to update the record!

        Please help... i don't know what i did wrong ...

        TQ!!!!!!!








        "Heikki Tuuri" <Heikki.Tuuri@i nnodb.com> wrote in message news:<jj2ed.37$ fF4.5@read3.ine t.fi>...[color=blue]
        > Jayson,
        >
        > ----- Original Message -----
        > From: "Chris Hope" <blackhole@elec trictoolbox.com >
        > Newsgroups: mailing.databas e.mysql
        > Sent: Friday, October 22, 2004 4:02 AM
        > Subject: Re: Select .. for update question
        >[color=green]
        > > jayson_13 wrote:
        > >[color=darkred]
        > >> Hi,
        > >>
        > >> I need to implement a counter and i face problem of locking so hope
        > >> that u guys can help me.
        > >>
        > >> I try to do test like this :
        > >>
        > >> 1st connection
        > >> SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
        > >> (when i execute this statement and i guess that this will lock the
        > >> record and prevent other connection to update that record right?)
        > >>
        > >>
        > >> 2nd connection
        > >> Update nextkey SET inextkey = inextkey + 1 WHERE tblname = 'PLCN';
        > >> (I execute this in another connection after i have executed the
        > >> select for update statement in the first connection. However, this
        > >> statement does update the record!!!)
        > >>
        > >> My major concern is that how can i prevent 2 ppl getting the same
        > >> counter. TQ!!!!!!!![/color][/color]
        >
        > you have probably not set
        >
        > SET AUTOCOMMIT=0
        >
        > and the lock by the 1st connection is released in the autocommit immediately
        > after you execute the SELECT ... FOR UPDATE.
        >[color=green][color=darkred]
        > >> I am using InnoDb table + Mysql 4.0.17[/color]
        > >
        > > Google is your friend:
        > > http://www.google.com/search?q=mysql+lock+table
        > >
        > > The first entry is the page you want from the MySQL manual.[/color]
        >
        > Using LOCK TABLES is not a very good method for InnoDB tables, because
        > MySQL's table locking mechanism does not have deadlock detection in
        > combination with row locks inside InnoDB. We will soon implement LOCK TABLES
        > TRANSACTIONAL that will work like in DB2, SQL Server, Oracle, and will be
        > the recommended method for locking InnoDB type tables.
        >[color=green]
        > > --
        > > Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/[/color]
        >
        > Best regards,
        >
        > Heikki Tuuri
        > Innobase Oy
        > Foreign keys, transactions, and row level locking for MySQL
        > InnoDB Hot Backup - a hot backup tool for InnoDB which also backs up MyISAM
        > tables
        > http://www.innodb.com/order.php
        >
        > Order MySQL technical support from https://order.mysql.com/[/color]

        Comment

        • Heikki Tuuri

          #5
          Re: Select .. for update question

          Jayson,

          maybe MySQL-Front terminates the connection and commits the transaction
          after EVERY SQL statement that you run?

          Please test with the mysql command-line SQL client.

          Best regards,

          Heikki Tuuri
          Innobase Oy
          Foreign keys, transactions, and row level locking for MySQL
          InnoDB Hot Backup - a hot backup tool for InnoDB which also backs up MyISAM
          tables
          http://www.innodb.com/order.php

          Order MySQL technical support from https://order.mysql.com/

          "jayson_13" <jayson_13@yaho o.com> kirjoitti
          viestissä:856e7 de4.0410242228. 288dad8e@postin g.google.com...[color=blue]
          > Hi Turri,
          >
          > I am testing it using mysql front 2.2. The following is how i
          > test... please tell me what the hell i have done wrong... TQ !
          >
          > 1st Connection
          > SET Autocommit=0;
          > SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
          > (if i am not mistaken, at this point, the transaction is not yet
          > completed. so the record supposed to be locked right?)
          >
          > 2nd Connection
          > Update nextkey SET inextkey = inextkey + 1 WHERE tblname =
          > 'PLCN';
          >
          > The 2nd connection still manage to update the record!
          >
          > Please help... i don't know what i did wrong ...
          >
          > TQ!!!!!!!
          >
          >
          >
          >
          >
          >
          >
          >
          > "Heikki Tuuri" <Heikki.Tuuri@i nnodb.com> wrote in message
          > news:<jj2ed.37$ fF4.5@read3.ine t.fi>...[color=green]
          >> Jayson,
          >>
          >> ----- Original Message -----
          >> From: "Chris Hope" <blackhole@elec trictoolbox.com >
          >> Newsgroups: mailing.databas e.mysql
          >> Sent: Friday, October 22, 2004 4:02 AM
          >> Subject: Re: Select .. for update question
          >>[color=darkred]
          >> > jayson_13 wrote:
          >> >
          >> >> Hi,
          >> >>
          >> >> I need to implement a counter and i face problem of locking so hope
          >> >> that u guys can help me.
          >> >>
          >> >> I try to do test like this :
          >> >>
          >> >> 1st connection
          >> >> SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
          >> >> (when i execute this statement and i guess that this will lock the
          >> >> record and prevent other connection to update that record right?)
          >> >>
          >> >>
          >> >> 2nd connection
          >> >> Update nextkey SET inextkey = inextkey + 1 WHERE tblname = 'PLCN';
          >> >> (I execute this in another connection after i have executed the
          >> >> select for update statement in the first connection. However, this
          >> >> statement does update the record!!!)
          >> >>
          >> >> My major concern is that how can i prevent 2 ppl getting the same
          >> >> counter. TQ!!!!!!!![/color]
          >>
          >> you have probably not set
          >>
          >> SET AUTOCOMMIT=0
          >>
          >> and the lock by the 1st connection is released in the autocommit
          >> immediately
          >> after you execute the SELECT ... FOR UPDATE.
          >>[color=darkred]
          >> >> I am using InnoDb table + Mysql 4.0.17
          >> >
          >> > Google is your friend:
          >> > http://www.google.com/search?q=mysql+lock+table
          >> >
          >> > The first entry is the page you want from the MySQL manual.[/color]
          >>
          >> Using LOCK TABLES is not a very good method for InnoDB tables, because
          >> MySQL's table locking mechanism does not have deadlock detection in
          >> combination with row locks inside InnoDB. We will soon implement LOCK
          >> TABLES
          >> TRANSACTIONAL that will work like in DB2, SQL Server, Oracle, and will be
          >> the recommended method for locking InnoDB type tables.
          >>[color=darkred]
          >> > --
          >> > Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/[/color]
          >>
          >> Best regards,
          >>
          >> Heikki Tuuri
          >> Innobase Oy
          >> Foreign keys, transactions, and row level locking for MySQL
          >> InnoDB Hot Backup - a hot backup tool for InnoDB which also backs up
          >> MyISAM
          >> tables
          >> http://www.innodb.com/order.php
          >>
          >> Order MySQL technical support from https://order.mysql.com/[/color][/color]


          Comment

          • jayson_13

            #6
            Re: Select .. for update question

            Hi Turri,

            TQ! i got it work now !!! so stupid i am... :)







            "Heikki Tuuri" <Heikki.Tuuri@i nnodb.com> wrote in message news:<9Qyfd.100 1$722.915@read3 .inet.fi>...[color=blue]
            > Jayson,
            >
            > maybe MySQL-Front terminates the connection and commits the transaction
            > after EVERY SQL statement that you run?
            >
            > Please test with the mysql command-line SQL client.
            >
            > Best regards,
            >
            > Heikki Tuuri
            > Innobase Oy
            > Foreign keys, transactions, and row level locking for MySQL
            > InnoDB Hot Backup - a hot backup tool for InnoDB which also backs up MyISAM
            > tables
            > http://www.innodb.com/order.php
            >
            > Order MySQL technical support from https://order.mysql.com/
            >
            > "jayson_13" <jayson_13@yaho o.com> kirjoitti
            > viestissä:856e7 de4.0410242228. 288dad8e@postin g.google.com...[color=green]
            > > Hi Turri,
            > >
            > > I am testing it using mysql front 2.2. The following is how i
            > > test... please tell me what the hell i have done wrong... TQ !
            > >
            > > 1st Connection
            > > SET Autocommit=0;
            > > SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
            > > (if i am not mistaken, at this point, the transaction is not yet
            > > completed. so the record supposed to be locked right?)
            > >
            > > 2nd Connection
            > > Update nextkey SET inextkey = inextkey + 1 WHERE tblname =
            > > 'PLCN';
            > >
            > > The 2nd connection still manage to update the record!
            > >
            > > Please help... i don't know what i did wrong ...
            > >
            > > TQ!!!!!!!
            > >
            > >
            > >
            > >
            > >
            > >
            > >
            > >
            > > "Heikki Tuuri" <Heikki.Tuuri@i nnodb.com> wrote in message
            > > news:<jj2ed.37$ fF4.5@read3.ine t.fi>...[color=darkred]
            > >> Jayson,
            > >>
            > >> ----- Original Message -----
            > >> From: "Chris Hope" <blackhole@elec trictoolbox.com >
            > >> Newsgroups: mailing.databas e.mysql
            > >> Sent: Friday, October 22, 2004 4:02 AM
            > >> Subject: Re: Select .. for update question
            > >>
            > >> > jayson_13 wrote:
            > >> >
            > >> >> Hi,
            > >> >>
            > >> >> I need to implement a counter and i face problem of locking so hope
            > >> >> that u guys can help me.
            > >> >>
            > >> >> I try to do test like this :
            > >> >>
            > >> >> 1st connection
            > >> >> SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update;
            > >> >> (when i execute this statement and i guess that this will lock the
            > >> >> record and prevent other connection to update that record right?)
            > >> >>
            > >> >>
            > >> >> 2nd connection
            > >> >> Update nextkey SET inextkey = inextkey + 1 WHERE tblname = 'PLCN';
            > >> >> (I execute this in another connection after i have executed the
            > >> >> select for update statement in the first connection. However, this
            > >> >> statement does update the record!!!)
            > >> >>
            > >> >> My major concern is that how can i prevent 2 ppl getting the same
            > >> >> counter. TQ!!!!!!!!
            > >>
            > >> you have probably not set
            > >>
            > >> SET AUTOCOMMIT=0
            > >>
            > >> and the lock by the 1st connection is released in the autocommit
            > >> immediately
            > >> after you execute the SELECT ... FOR UPDATE.
            > >>
            > >> >> I am using InnoDb table + Mysql 4.0.17
            > >> >
            > >> > Google is your friend:
            > >> > http://www.google.com/search?q=mysql+lock+table
            > >> >
            > >> > The first entry is the page you want from the MySQL manual.
            > >>
            > >> Using LOCK TABLES is not a very good method for InnoDB tables, because
            > >> MySQL's table locking mechanism does not have deadlock detection in
            > >> combination with row locks inside InnoDB. We will soon implement LOCK
            > >> TABLES
            > >> TRANSACTIONAL that will work like in DB2, SQL Server, Oracle, and will be
            > >> the recommended method for locking InnoDB type tables.
            > >>
            > >> > --
            > >> > Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
            > >>
            > >> Best regards,
            > >>
            > >> Heikki Tuuri
            > >> Innobase Oy
            > >> Foreign keys, transactions, and row level locking for MySQL
            > >> InnoDB Hot Backup - a hot backup tool for InnoDB which also backs up
            > >> MyISAM
            > >> tables
            > >> http://www.innodb.com/order.php
            > >>
            > >> Order MySQL technical support from https://order.mysql.com/[/color][/color][/color]

            Comment

            Working...