Java 当前事务被中止,在使用hibernate的postgresql的事务块结束之前忽略命令

Java 当前事务被中止,在使用hibernate的postgresql的事务块结束之前忽略命令,java,postgresql,hibernate,transactions,Java,Postgresql,Hibernate,Transactions,我正在使用hibernate和postgresql方言,我正在运行一个事务错误。这是我的主要应用程序: public class AppControl { public static void main(String[] args) { //Configuration Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); S

我正在使用hibernate和postgresql方言,我正在运行一个事务错误。这是我的主要应用程序:

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();
    }

}
}
我遇到以下错误

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
这是我的学生桌:

sno(pk)| sname


感谢您的帮助。谢谢

前面的查询失败,您可能在某个地方吞下了异常,因此它不是输出。或者您需要检查日志以查找第一个错误。

模式定义了PK限制。
student.setSno(2)
是主键,因此不能复制。这就是第一轮通过的原因。您可以尝试将
2
更改为其他值,也可以从PostgreSQL表中删除记录,然后再次尝试运行代码

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


异常
由以下原因引起:org.postgresql.util.PSQLException:错误:当前事务被中止,在事务块结束之前忽略命令
说明了它的意思-当前运行的事务中出现了一些故障,并且在事务中执行的任何命令都将无效。您需要调用
回滚
连接上的事务。

已经查看了这个,我认为这个堆栈跟踪可能有更多内容?这可能是因为没有为学生设置
zip
课程号
?我第一次运行这个程序时,它工作了,但从第二次开始就出现了这个异常。这就是为什么我确实更改了主键每次运行时。我尝试在连接时回滚事务,但也没有帮助。如果有更多信息发生了什么,您是否尝试打印捕获块中捕获的RuntimeException?