3

I'm looking at porting a Java-Spring application from Oracle to PostgreSQL. I'm running into a big difference between the way the transactions are handled. I'm having a problem with this statement:

public void upsert(String username, String pName, String pValue) {
        Object[] args = { pValue, pName, username };

        try {
            jdbcTemplate.update(INSERT_USER_PREFERENCE, args);
        } catch (DuplicateKeyException e) {
            jdbcTemplate.update(UPDATE_USER_PREFERENCE, args);
        }
    }

This works fine in Oracle but in PostgreSQL this fails because PostgreSQL does not allow further commands in a transaction if there is a failure and it wants you to rollback or commit

(SQL state [25P02]; error code [0]; ERROR: current transaction is aborted, commands ignored until end of transaction block; nested exception is org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block)

This seems pretty unique to PostgreSQL. Is there a way to make PostgreSQL behave in the same way as Oracle (and other databases)?

Here is my datasource setup:

<bean id="portalDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="${test.database.url}"/>
    <property name="username" value="${test.database.username}"/>
    <property name="password" value="${test.database.password}"/>
</bean>


<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="portalDataSource"/>
</bean>
ryber
  • 4,537
  • 2
  • 26
  • 50
  • 1
    PostgreSQL is more strict in this way. You could take a look at [this question](http://stackoverflow.com/q/2370328/1288184) – Simo Kivistö May 04 '15 at 07:39

0 Answers0