pg.ddx.io  pljava-dev@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Subject: [Pljava-dev] Savepoints and continuing from SQLExceptions [snapshot reference 0x19a1710 is not owned by resource owner TopTransaction]
Date: Wed, 17 Oct 2012 18:28:49 -0700
Message-ID: <96DBE277-8790-4B4A-A120-0408D97D8464@me.com> (raw)

So this problem has been driving me nuts and I think I've finally exhausted my googling abilities in searching for the answer.

The issue is that I'd like to recover from a SQLException, roll back the savepoint and then issue another query and then reissue the failing query.  The catch works, I roll back the save point, issue the additional query.  However, when I retry the original query, I get the error:

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException:
	 ERROR: snapshot reference 0x19a1710 is not owned by resource owner TopTransaction

I'm sure that I am doing something wrong here, as this is the first time I'm trying to use savepoints in this way and I'm doing it inside a PL/Java function...

Anyways, the code is below.  The error above occurs at the last "query.execute();" in the code below.

Any help would be appreciated....  Hopefully, I'm just missing something basic and it's an easy fix....

-Hal

Code:
____________________

    public void addId(Long value, String key) throws SQLException {
        String tempTableName = SHARED_TEMP + key;
        PreparedStatement query = connection.prepareStatement(String.format("INSERT INTO %s VALUES (?)",
                                                                            tempTableName));
        query.setLong(1, value);
        Savepoint savepoint = connection.setSavepoint();
        try {
            query.execute();
        } catch (SQLIntegrityConstraintViolationException e) {
            if (log.isLoggable(Level.INFO)) {
                log.log(Level.INFO, String.format("table %s already has %s",
                                                  tempTableName, value));
            }
        } catch (SQLException e) {
            if (log.isLoggable(Level.INFO)) {
                log.log(Level.INFO,
                        String.format("Creating table %s", tempTableName), e);
            }
            query.close();
            connection.rollback(savepoint);

            PreparedStatement create = connection.prepareStatement(String.format("CREATE TEMPORARY TABLE %s (id BIGINT PRIMARY KEY) ON COMMIT DROP",
                                                                                 tempTableName));
            try {
                create.execute();
            } finally {
                create.close();
            }
            if (log.isLoggable(Level.INFO)) {
                log.log(Level.INFO,
                        String.format("Retrying insert of %s into %s", value,
                                      tempTableName), e);
            }
            query = connection.prepareStatement(String.format("INSERT INTO %s VALUES (?)",
                                                              tempTableName));
            query.setLong(1, value);
            query.execute();
        } finally {
            query.close();
            connection.releaseSavepoint(savepoint);
        }
    }




Message-ID: <96DBE277-8790-4B4A-A120-0408D97D8464@me.com>
Permalink:  ../96DBE277-8790-4B4A-A120-0408D97D8464@me.com/
Also on:    postgresql.org/message-id/96DBE277-8790-4B4A-A120-0408D97D8464@me.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: pljava-dev@postgresql.org
  Subject: Re: [Pljava-dev] Savepoints and continuing from SQLExceptions [snapshot reference 0x19a1710 is not owned by resource owner TopTransaction]
  In-Reply-To: <96DBE277-8790-4B4A-A120-0408D97D8464@me.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