neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

I have a web/database application. It uses Mason (DBI) and Apache:DBI to maintain database connections. If I have multiple users concurrently inserting data can the last_insert_id be trusted?

Neil Watson
watson-wilson.ca

  • Comment on Is last_insert_id for msyql reliable with DBI?

Replies are listed 'Best First'.
Re: Is last_insert_id for msyql reliable with DBI?
by dws (Chancellor) on Jun 04, 2004 at 17:10 UTC

    I have multiple users concurrently inserting data can the last_insert_id be trusted?

    The last_insert_id reflects the id of the last record inserted by that connection. I've never seen evidence, direct or otherwise, that it isn't reliable.

Re: Is last_insert_id for msyql reliable with DBI?
by tadamec (Beadle) on Jun 04, 2004 at 19:27 UTC

    I've worked on large (10,000 hits an hour) clustered web applications using Mason, mod_perl and Apache::DBI and never once had a problem with the last_insert_id function.

    MyISAM tables locking on every insert, now there's another problem... :-)

Re: Is last_insert_id for msyql reliable with DBI?
by drewbie (Chaplain) on Jun 05, 2004 at 13:44 UTC
    I've never once had a problem with last_insert_id. As a previous poster mentioned, it's per connection. Relevant quote from the docs below. There's also a note about SQL function LAST_INSERT_ID() if you happen to use it.

    From http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html
    Note that mysql_insert_id() returns 0 if the previous statement does not use an AUTO_INCREMENT value. If you need to save the value for later, be sure to call mysql_insert_id() immediately after the statement that generates the value.

    The value of mysql_insert_id() is affected only by statements issued within the current client connection. It is not affected by statements issued by other clients.