[web2py] Re: question about db.mytable.update_or_insert()

2014-08-13 Thread Niphlod
that's basic versioning theory of databases. 
If you do two operations in two separate transactions that, in "english" 
can be translated as "if you don't find this record, please insert it", the 
two will happily succeed one after another if they are concurrent. 
That's why unique constraints are a builtin feature that every backend has.

On Tuesday, August 12, 2014 10:24:48 PM UTC+2, Jack Kuan wrote:
>
> Hi,
>
> When using  update_or_insert(), say for example:  
> db.mytable.update_or_insert(q, name='test')
>
> Let's say the query, q, results in not finding any row in mytable, is it 
> possible that in another transaction in another request that when the same 
> update_or_insert(...) is called that it also doesn't find any row and thus 
> at the end, the two transcations both try to commit an insert, which could 
> result in duplicated rows(with different id's) or an integrity error(eg, if 
> there's an unique constraint)? Or is web2py smart enough to generate an 
> insert that is safe?
>
> Thanks
> Jack
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: question about db.mytable.update_or_insert()

2014-08-13 Thread Leonel Câmara
Couldn't he use a _for_update argument there to make this work in some 
adapters?

db.mytable.update_or_insert(q, name='test', _for_update=True)

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: question about db.mytable.update_or_insert()

2014-08-13 Thread Niphlod
the problem in concurrency (andduplication) isn't the update part, it's the 
insert part

On Wednesday, August 13, 2014 1:39:56 PM UTC+2, Leonel Câmara wrote:
>
> Couldn't he use a _for_update argument there to make this work in some 
> adapters?
>
> db.mytable.update_or_insert(q, name='test', _for_update=True)
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: question about db.mytable.update_or_insert()

2014-08-13 Thread Jack Kuan
How does one usually deal with such conflict? (two identical(except for 
id's) inserts causing a violation of an unique constraint)

Isolating the insert into its own transaction and then do a db.commit(), 
catch any exception and then ignore?

Thanks

On Wednesday, August 13, 2014 10:57:13 AM UTC-4, Niphlod wrote:
>
> the problem in concurrency (andduplication) isn't the update part, it's 
> the insert part
>
> On Wednesday, August 13, 2014 1:39:56 PM UTC+2, Leonel Câmara wrote:
>>
>> Couldn't he use a _for_update argument there to make this work in some 
>> adapters?
>>
>> db.mytable.update_or_insert(q, name='test', _for_update=True)
>>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: question about db.mytable.update_or_insert()

2014-08-13 Thread Niphlod
there's no other way around it, in every programming language. You want to 
do something in a transaction, the db won't let you do it, you catch the 
exception (rollbacking) and deal with it.

On Wednesday, August 13, 2014 7:30:58 PM UTC+2, Jack Kuan wrote:
>
> How does one usually deal with such conflict? (two identical(except for 
> id's) inserts causing a violation of an unique constraint)
>
> Isolating the insert into its own transaction and then do a db.commit(), 
> catch any exception and then ignore?
>
> Thanks
>
> On Wednesday, August 13, 2014 10:57:13 AM UTC-4, Niphlod wrote:
>>
>> the problem in concurrency (andduplication) isn't the update part, it's 
>> the insert part
>>
>> On Wednesday, August 13, 2014 1:39:56 PM UTC+2, Leonel Câmara wrote:
>>>
>>> Couldn't he use a _for_update argument there to make this work in some 
>>> adapters?
>>>
>>> db.mytable.update_or_insert(q, name='test', _for_update=True)
>>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.