Re: JDBC and SQL - Mailing list pgsql-general

From Alexander Solianic
Subject Re: JDBC and SQL
Date
Msg-id 3b3896f8@news.telekom.ru
Whole thread Raw
List pgsql-general
Ed Murray wrote:

> Could anybody explain a good way of doing the following.
>     I would like to read an integer value from a database and then
> update the database  value  by adding a specified figure to it.
>     The addition must be done before any other query can else can access
> this value. i.e. preferably in the same operation.
>     I would like to do this using JDBC and try and keep it cross
> database compatible.
>
> Any help would be greatly appreciated.
>
> --
> Ed Murray
> Avenue Network Services
> hns@optushome.com.au
> + 61 2 94162140
>
>
>
>

Assume you have table `mytable' with two integer
columns: `x' and `y'. To atomicaly update `x' in row
with y == 123 you may use something like this
(just SQL, not JDBC):

select x from mytable where y=123 for update; /* locks row */
/* do something with `x' value, calculate `newvalue' */
update mytable set x=newvalue where y=123;
commit;

PS
autocommit must be `false'.

--
Regards,
Alexander Solianic

pgsql-general by date:

Previous
From: "Lincy"
Date:
Subject: Re: 7.1.2 ERROR: UNIQUE constraint matching given keys for referenced table ......
Next
From: "Joseph Bonnet"
Date:
Subject: plpgsql and COPY