current transaction is aborted, commands ignored until end of transaction block for postgresql with hibernate

时间:2017-07-19 00:34:54

标签: java postgresql hibernate transactions

I am using hibernate with postgresql dialect and I am running through a transaction error. This is my main app:

public class AppControl {

public static void main(String[] args) {
    //Configuration
    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    Session session = null;
    Transaction transaction = null;

    //Session Factory
    SessionFactory sessionFactory = configuration.buildSessionFactory();

    try {
        //Session
        session = sessionFactory.openSession();
        //Begin transaction
        transaction = session.beginTransaction();
        //DB operations

        Student student = new Student();
        student.setSno(2);
        student.setSname("Venky");
        session.save(student);
        session.flush();
        transaction.commit();
    }catch (RuntimeException e) {
        try {
            session.flush();
            transaction.rollback();
        }catch(Exception ex) {
            System.out.println("Cannot rollback transaction");
        }
    } finally {

        session.close();
        sessionFactory.close();
    }

}
}

I encounter following error

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:103)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:461)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:347)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at com.demo.app.AppControl.main(AppControl.java:32)
Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
    ... 13 more

This is my student table:

sno(pk)|sname

Any help is appreciated. Thank You

2 个答案:

答案 0 :(得分:0)

之前的查询失败了,您可能会在某处吞下异常,因此无法输出。或者您需要在日志中查看以查找第一个错误。

答案 1 :(得分:0)

架构定义了PK限制。 student.setSno(2)是主键,因此无法复制。这就是第一次运行的原因。您可以尝试将2更改为其他值,也可以从PostgreSQL表中删除该记录并尝试再次运行该代码。

如果将System.out.printStackTrace(e)添加到catch块}catch (RuntimeException e) {,我认为您将看到问题的根源(例如,使用相同的主键插入新记录的问题)。

异常Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block说明了什么 - 当前正在运行的事务中存在一些失败,并且在事务中完成的任何命令都将无效。您应该在连接上调用rollback交易。