From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Mark Dzmura <mdz(at)digital-mission(dot)com> |
Cc: | "pgsql-interfaces(at)postgreSQL(dot)org" <pgsql-interfaces(at)postgresql(dot)org> |
Subject: | Re: JDBC squirrely transaction behavior?? |
Date: | 2000-06-02 16:11:27 |
Message-ID: | 15290.959962287@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-interfaces |
Mark Dzmura <mdz(at)digital-mission(dot)com> writes:
> try
> {
> insert into foos (foo_name) values ('foo test value #1');
> }
> catch (SQLException e)
> {
> system.out.println("attempt to insert duplicate foo ... not a problem.");
> }
Can't do it that way inside a transaction: the insert failure forces
the whole transaction to be aborted. The fact that you caught the
exception doesn't affect the fact that the server believes the
transaction must now be aborted; it won't process any more statements
until COMMIT or ROLLBACK.
I'd suggest something like
rs = select foo_id from foos where foo_name='foo test value #1';
if (rs is empty)
{
insert into foos (foo_name) values ('foo test value #1');
rs = select foo_id from foos where foo_name='foo test value #1';
Assert(rs is not empty);
}
This will be a little slower if the common case is that an insert is
needed, but faster if the common case is that no insert is needed.
Another possibility is to automate the sequence with a rule or trigger
on the database side. For example I think you could define a view
"foos_magic" with a rule such that you can just unconditionally do an
"select from foos_magic where ..." and an insert will be done for you
iff there's no matching record.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Roderick A. Anderson | 2000-06-02 23:05:14 | Re: ODBC driver for Windows - Problems |
Previous Message | Tom Lane | 2000-06-02 16:01:40 | Re: Long atributes |