Hibernate错误 - 当前事务被中止,命令被忽略直到事务块结束

时间:2013-06-13 08:34:25

标签: java hibernate

我在使用此代码时遇到了一些问题:

private EntityManager entityManager;

public EntityManager getEntityManager() {
    if (entityManager == null) {
        entityManager = Persistence.createEntityManagerFactory("pu-mypu").createEntityManager();
    }
    return entityManager;
}


public <T> T get(Long id, Class<T> type) {
    return getEntityManager().find(type, id);
}

尝试拨打时,MyPojo p = get(1, MyPojo.cass);

它抛出:

[ERROR] Caused by: org.hibernate.exception.GenericJDBCException: ERROR: current transaction is aborted, commands ignored until end of transaction block
[ERROR]     at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
[ERROR]     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
[ERROR]     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
[ERROR]     at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
[ERROR]     at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
[ERROR]     at $Proxy175.executeQuery(Unknown Source)
[ERROR]     at org.hibernate.loader.Loader.getResultSet(Loader.java:2031)
[ERROR]     at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
[ERROR]     at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811)
[ERROR]     at org.hibernate.loader.Loader.doQuery(Loader.java:899)
[ERROR]     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
[ERROR]     at org.hibernate.loader.Loader.doList(Loader.java:2516)
[ERROR]     at org.hibernate.loader.Loader.doList(Loader.java:2502)
[ERROR]     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
[ERROR]     at org.hibernate.loader.Loader.list(Loader.java:2327)
[ERROR]     at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
[ERROR]     at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
[ERROR]     at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
[ERROR]     at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1247)
[ERROR]     at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
[ERROR]     at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:283)
[ERROR]     ... 48 more

1 个答案:

答案 0 :(得分:4)

简短的回答是,您的数据库上的操作引发了一个早期异常,并且您没有回滚与该操作相关的事务。 跟随这个例子的时间越长。假设您有一个将对象保存到DB中的方法:

public Serializable saveObject(E object) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction trans = session.beginTransaction();
    try {           
        Serializable id = session.save(object);
        session.flush();
        trans.commit();
        return id;
    } catch (Exception e) {
        e.printStackTrace();            
        return null;
    }
}

现在,假设由于某些约束违规(例如,唯一的主键重复),上面的代码会抛出运行时异常。在catch块中,您可以看到我们没有回滚事务。从这时起,每次访问DB都会导致“错误:当前事务被中止,......”错误 现在您需要做的是回滚每个失败的事务,以便您的应用程序和数据库保持同步:

public Serializable saveObject(E object) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction trans = session.beginTransaction();
    try {           
        Serializable id = session.save(object);
        session.flush();
        trans.commit();
        return id;
    } catch (Exception e) {
        e.printStackTrace();
        session.flush();
        trans.rollback();
        return null;
    }
}