问题描述
私人EntityManager entityManager;
$ b $ public EntityManager getEntityManager(){
if(entityManager == null){
entityManager = Persistence.createEntityManagerFactory(pu-mypu)。createEntityManager();
}
返回entityManager;
}
public< T> T get(Long id,Class< T> type){
return getEntityManager()。find(type,id);
尝试调用时, MyPojo p = get 1,MyPojo.cass);
引发:
由于:org.hibernate.exception.GenericJDBCException:错误:当前事务被中止,命令被忽略,直到事务结束
[ERROR]在org.hibernate.exception.internal。 StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
[错误]在org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
[错误]在org.hibernate。 engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
[错误]在org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
$ ERROR]
[ERROR] at $ Proxy175.executeQuery(Unknown Source)
[错误] at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) org.hibernate.loader.Loader.getResultSet(Loader.java:2031)
[ERROR] at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
[org.hibernate] at org .buernate.loader.Loader.executeQueryStatement(Loader.java:1811)
[错误]在org.hibernate.loader.Loader.doQuery(Loader.java:899)
[错误]在org.hibernate .loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
[错误]在org.hibernate.loader.Loader.doList(Loader.java:2516)
[错误]在org.hibernate.loader .Loader.doList(Loader.java:2502)
[错误]在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
[错误]在org.hibernate.loader.Loader .list(Loader.java:2327)
[错误]在org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
[错误]在org.hibernate.hql.internal .ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
[错误]在org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan .java:195)
[org.hibernate.internal.SessionImpl.list(SessionImpl.java:1247)上的[错误]
[错误]在org.hibernate.internal.QueryImpl.list(QueryImpl.java :101)
[错误] at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:283)
[错误] ... 48 more
简短的回答是,您的数据库操作引发了一个较早的异常,并且您不回滚与该操作相关的事务。
这个例子的时间越长。假设你有一个将对象保存到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();
返回null;
$ b现在,假设由于某些约束违规(独特的,例如主键重复),上面的代码会引发运行时异常。在catch块中,您可以看到我们没有回滚事务。从这一刻起,对数据库的每次访问都将导致错误:当前事务中止......错误。
现在您需要做的是回滚每个失败的事务,以便您的应用程序和DB保持同步:
$ b $ pre $ 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();
返回null;
}
}
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
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;
}
}
这篇关于Hibernate错误 - 当前事务中止,直到事务块结束时忽略命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!