why would an update only work if done twice? - Mailing list pgsql-jdbc

From C O'Connor
Subject why would an update only work if done twice?
Date
Msg-id Pine.SOL.4.05.10109281422410.26935-100000@sis.bris.ac.uk
Whole thread Raw
Responses Re: why would an update only work if done twice?  ("Dave Cramer" <Dave@micro-automation.net>)
List pgsql-jdbc
I know this is a mistake I have made, and I am pretty sure it is a
mistake that has been occuring ever since I caught an SQL exception
(tried to insert value with already existing primary key).

At a guess I'd say that the db didn't recover properly because I
didn't rollback when i caught that exception.

THE PROBLEM
When I do an insert/update on the db using my jdbc code it goes through
correcty and does NOT raise any errors. However an examination of the db
using psql reveals that the table hasn't been updated at all. This is
scary, as it seems I cannot guarantee that any data has ever been
committed (I DO CALL COMMIT).

Furthermore if while my tomcat instance is still running (I have the
struts pooled connection running on top of it) I try and modify that
table in the database from within psql it hangs. It will only complete
when tomcat is stopped (presumably meaning that tomcat is hanging
somewhere despite not throwing an exception or hanging itself).

The SCARY thing is that I can fix this problem, with this code :-


      conn = dataSource.getConnection();
      if (auform.getOldUserName().equals("") | auform.getOldUserName()
== null) {
        if (adminUser.getAdminAccountAccess()) {
          System.out.println("inserting admin user");
          auuBean.insertAdminUser(conn, saveAdminUser);
        } else
          errors = true;
      } else {
        System.out.println("updating admin user");
        auuBean.updateAdminUser(conn, saveAdminUser,
auform.getOldUserName());
      }
      conn.commit();
      conn.close();

// CODE INSERTED TWICE!!!!!!!!!!!!
      conn = dataSource.getConnection();
      if (auform.getOldUserName().equals("") | auform.getOldUserName()
== null) {
        if (adminUser.getAdminAccountAccess()) {
          System.out.println("inserting admin user");
          auuBean.insertAdminUser(conn, saveAdminUser);
        } else
          errors = true;
      } else {
        System.out.println("updating admin user");
        auuBean.updateAdminUser(conn, saveAdminUser,
auform.getOldUserName());
      }
      conn.commit();
      conn.close();
// END CONFUSION

      conn = null;
    } catch (SQLException e) {
      while (e != null) {
        System.out.println("First Error Message: " + e.getMessage());
        e = e.getNextException();
      }
      if (conn != null) {
        try {
          conn.rollback();
        } catch (SQLException f) {
          System.out.println("heavy shit rolling back db transaction");
        }
      }
      errors = true;
    } finally {
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException e) {
          System.out.println("heavy shit losing a db pool connection");
          errors = true;
        }
        conn = null;
      }
    }

A separate utility bean actually does the update or insert and throws
the exception for the action servlet to catch here.

My fix is the extra update/insert. For some reason this will update the
database whereas NOTHING else will.
I have tried vacuuming, reindexing and they don't seem to change
anything.

I am running the latest version sid version of postgresql 7.1.3-4, and
connecting with a struts 1.0 pooled connection through tomcat. The code
above has worked before, but stopped working after it caught an SQL
exception (and before I put the rollback code in).

Can anyone suggest,
a) what the problem could be?
b) how i could fix it?
c) how to make sure it never happens again?

Caoilte


pgsql-jdbc by date:

Previous
From: Grant Edwards
Date:
Subject: Errors when building jdbc dirver jar file
Next
From: "Dave Cramer"
Date:
Subject: Re: why would an update only work if done twice?