pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mark Dzmura <mdz@digital-mission.com>
To: pgsql-interfaces@postgreSQL.org <pgsql-interfaces@postgreSQL.org>
Subject: JDBC squirrely transaction behavior??
Date: Wed, 31 May 2000 23:48:11 +0000
Message-ID: <3935A4BB.C9EF9337@digital-mission.com> (raw)

dumb JDBC/SQL question:

I have just moved some code over from pg 6.5.2 to pg 7.0 and am starting to modify an existing code base
to use transactions.

I need to add some records to masterfile tables in a particular order (due to foreign key dependencies) and
want to use a transaction to allow aborting the whole shebang in the event any of the inserts fails.

Below is a simple sample of code which shows the problem.

---------------------------------------------

create table foos (foo_id serial primary key, foo_name varchar(64) unique);

create table bars(bar_id serial primary key, foo_id int4 references foos, bar_len int4, bar_width int4);

...

connection.setAutoCommit(false);

while (true)
    {
    try
        {
        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.");
            }
        // get the foo_id for this foo;
        rs = select foo_id from foos where foo_name='foo test value #1');
        if (rs.next()
            {
            int foo_id = rs.getInt(1);
            insert into bars (foo_id, bar_len, bar_width) values (foo_id, 25, 99);
            connection.Commit();
            }
        else
            {
            connection.Rollback();
            System.out.println("error: unable to lookup foo_id");
            throw new Exception("internal error...");
            }
        }
    catch (SQLException e)
        {
        System.out.println("bad things a-happenin");
        e.printStackTrace();
        }
    }

This code will run for a number of insertions, then fail because the select of the foo_id
returns no records.  This should be impossible, because either (1) the foo_id was
just added to the foos table, or (2) it had previously been added.  Either way,
there should be a record with a matching namefield.

I then commented out the transaction stuff, and verified that the code ran properly.

My first thought was that statement #2 was unable to see changes effected by
statement #1, but because they are within the scope of the same transaction,
they should be able to....strangely, this works for some records, but not others.

Is the pg or jdbc support for transactions broken??

Finally, the API does not seem to offer support for nested transactions, which
sometime come in handy...  But even the JavaSoft-approved JDBC book
written by the authors of JDBC does a lame job of covering transactions
(and many other things...)

Thanks and best regards,
Mark Dzmura

----------------------------------------------------

On a related note,

Any guesses as to when the postgresql jdbc driver will support the JDBC 2.x extensions
for record insert/update/delete via the special record in a result set ??

Regards,
Mark Dzmura

--
)))) This email routed via a wireless gateway!! ((((

Attachments:

mdz.vcf (text/x-vcard, 279B) download begin:vcard n:Dzmura;Mark tel;fax:412-429-3361 tel;work:412-429-6001 x-mozilla-html:FALSE org:Digital Mission, LLC adr:;;230 E. Main St., Suite 210;Carnegie;PA;15106;USA version:2.1 email;internet:mdz@digital-mission.com title:President x-mozilla-cpt:;0 fn:Mark Dzmura end:vcard

view thread (7+ messages)  latest in thread

Message-ID: <3935A4BB.C9EF9337@digital-mission.com>
Permalink:  ../3935A4BB.C9EF9337@digital-mission.com/
Also on:    postgresql.org/message-id/3935A4BB.C9EF9337@digital-mission.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: mdz@digital-mission.com
  Subject: Re: JDBC squirrely transaction behavior??
  In-Reply-To: <3935A4BB.C9EF9337@digital-mission.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox