quarks
quarks

Reputation: 35346

Hibernate error - current transaction is aborted, commands ignored until end of transaction block

I'm having some problem with thie code:

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

When trying to call, MyPojo p = get(1, MyPojo.cass);

It throws:

[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

Upvotes: 2

Views: 3436

Answers (1)

Laabidi Raissi
Laabidi Raissi

Reputation: 3333

The short answer, is that you have an earlier exception thrown by an operation on your database and that you did not rollback the transaction related to that operation. The longer one is following this example. Assume you hve a method that saves objects into 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;
    }
}

Now, assume that because of some constraints violation (unique, primary key duplicates for example), the above code throws a Runtime Exception. In the catch block you can see that we are not rolling back the transaction. From this moment, every access to DB will cause the "ERROR: current transaction is aborted,..." error.
Now what you have to do is to rollback every failed transaction, so that your application and DB remain synchronized:

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

Upvotes: 6

Related Questions