Nuno Lucas wrote:
> On Wed, Jun 3, 2009 at 11:22 PM, Nikolaus Rath <nikol...@rath.org> wrote:
>   
>> Nuno Lucas <ntlu...@gmail.com> writes:
>>     
>>> On Wed, Jun 3, 2009 at 2:41 AM, Nikolaus Rath <nikol...@rath.org> wrote:
>>>       
>>>> Nuno Lucas <ntlu...@gmail.com> writes:
>>>>         
>>>>> On Tue, May 26, 2009 at 5:17 PM, Nikolaus Rath <nikol...@rath.org> wrote:
>>>>>           
>>>>>> Hello,
>>>>>>
>>>>>> How can I determine the rowid of the last insert if I am accessing the
>>>>>> db from different threads? If I understand correctly,
>>>>>> last_insert_rowid() won't work reliably in this case.
>>>>>>             
>>>>> It should work if you do:
>>>>>
>>>>> BEGIN
>>>>> INSERT ...
>>>>> last_insert_rowid()
>>>>> END
>>>>>           
>>>> That would be very nice. But does "it should work" mean that you know
>>>> that it works (and it is documented and guaranteed)? The above sounds a
>>>> bit uncertain to me...
>>>>         
>>> It just means I'm too old to assume anything is certain. The Universe
>>> is always conspiring against you ;-)
>>>
>>> What I mean is that if it doesn't work, then you found a bug, most
>>> probably in your own code.
>>>       
>> Well, now you are in direct contradiction to Igor who says that it does
>> not work:
>>     
>
> Let's try end this confusion...
>
> If you open a sqlite shell and do:
>
> SQLite version 3.6.14.2
> Enter ".help" for instructions
> Enter SQL statements terminated with a ";"
> sqlite> begin;
> sqlite> begin;

Since we are discussing threads here, the example is meaningless.  
"begin" is a piece of text which is parsed into opcodes and then 
processed by a complied virtual machine.  Threads switch at the machine 
code level, so a thread could switch between the processing of the 'g' 
and the 'i' of the first 'begin'.

IMHO, if you are programming in threads, maintain control in the 
application.  Threads are already non-deterministic and potentially 
buggy.  The last thing one wants is to add places of uncertainty.

Pseudocode:

entryPoint
  grabMutex
    db.execute('begin;')
    db.execute('insert...
    lastInsertRowId = db.last_insert_rowid
  releaseMutex

Anything else "might" work, but I wouldn't want to waste time losing 
sleep over it.

FWIW

John
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to