title: Is the return of last_insert_id always correct?
body: <p>If I have the 2 following functions:</p>
  
  <pre><code>int AccessDb::InsertColValue(string tableName, string col, string val)
  {
      try
      {
          sql::Statement *stmt;
          bool ret;
  
          if ((nomTable != "") &amp;&amp; (col != "") &amp;&amp; (val != ""))
          {
              string query = "INSERT INTO " + tableName + "(" + col + ") values (";
                      query += val + ");";
  
              stmt = con-&gt;createStatement();
              ret = stmt-&gt;execute(query);
          }
  
          delete stmt;
  
          return 0;
      }
      catch (sql::SQLException &amp;e)
      {
          return -1;
      }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>long AccessDb::LastInsertId()
  {
      try
      {
          sql::Statement *stmt;
          sql::ResultSet *res;
  
          string query = "SELECT LAST_INSERT_ID() AS LAST_ID";
  
          stmt = con-&gt;createStatement();
          res = stmt-&gt;executeQuery(query);
  
          delete stmt;
  
          long lastId;
          while (res-&gt;next())
          {
              lastId = res-&gt;getInt("LAST_ID");
          }
  
          return lastId;
      }
      catch (sql::SQLException &amp;e)
      {
        return -1;
      }
  }
  </code></pre>
  
  <p>Can I be sure that the return of LastInsertId() will always give me the correct id if I write the following lines and if the id is auto generated by the database?</p>
  
  <pre><code>AccessDb adb; // initialize the connexion with the db
  int ret = adb.InsertColValue("people", "name", "John");
  if (ret == 0)
      long lastId = adb.LastInsertId();
  </code></pre>
  
  <p>If the previous code is called somewhere else at the same time, can I have a wrong value in my lastId variable ? If yes, do I have to use locks and unlocks on my table to avoid that or another solution ?</p>