3,5 postgresql, how can I use select for update?

Posts   
 
    
miloszes
User
Posts: 222
Joined: 03-Apr-2007
# Posted on: 22-Aug-2012 16:14:07   

Hi,

is there a possibility to fetch a row from a db using select for update clause?

Best Regards, MiloszeS

miloszes
User
Posts: 222
Joined: 03-Apr-2007
# Posted on: 22-Aug-2012 16:32:44   

Maybe I describe why I need to use it.

We have some task queuing mechanism which is responsible to run tasks in specific order. We have a few applications which are responsible for executing such tasks according to order, and of course one task can be run only once. What I found in postgresql documentation the serializable transaction doesn't block a transaction during a read when it was read by other transaction, but instead it checks during an update whether entity which we want to update wasn't modified by other transaction. If it was modified in the meantime it throws an exception with the following message "could not serialize access due to concurrent update".

To achieve a transaction block during a read we need to use select for update statement. How can I achieve that?

Best Regards, MiloszeS

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Aug-2012 22:52:19   

The following is an old thread discussing this issue, but concepts are still applied: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=8501

miloszes
User
Posts: 222
Joined: 03-Apr-2007
# Posted on: 23-Aug-2012 16:11:48   

Yep, but there is no answer about how to lock this table. I cannot modify that table - I should use select for update statement. Can I inherit some class and override some method to achieve that ?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 23-Aug-2012 19:32:47   

You can derive from DataAccessAdapter and override any of the fetch methods to modify the query to be executed, and don't forget to call the base method passing the modified query.

Methods to override: - ExecuteMultiRowRetrievalQuery - ExecuteMultiRowDataTableRetrievalQuery - ExecuteActionQuery - ExecuteScalarQuery - ExecuteSingleRowRetrievalQuery

So you can pick what you want, or better override the CreateSelectDQ method. Anyway you should use the derived class when needed.