1

我在 EJB 中有这段代码:

try{
    em.persist(joueur);
    em.persist(p);
    em.persist(astre);
    em.persist(planet);
    em.flush();         
}catch(Exception e){    
       logger.log(Level.FATAL, "test");
    // ExceptionManager.manageExceptions(e);             
}

这段代码必须抛出异常,因为我已经触发(通过 PostGreQSL)来检查一些约束,并且我使用测试值来启动我的触发器。

我有一个奇怪的结果,因为我的日志显示test,所以我输入了 catch 块,但我的代码没有捕获“全部”或“完整”异常,因为我的服务器日志显示所有异常堆栈,我不知道为什么......看起来就像这段代码在......之后抛出异常一样

我知道我必须调用flush()方法来立即将实体存储到我的数据库中,但看起来它在这里不起作用,并且异常仍然传播到我的代码中。

几周前这段代码有效,但我在另一个项目方面工作过,现在似乎我有某种回归,我不知道为什么......

所以我的问题是这个:EntityManager.flush() 总是立即执行持久操作吗?

编辑:这是我的日志(有点净化)(第一个->GF 日志,第二个->Log4j 个人日志):对不起,它是法语版本,但异常名称保持不变;)

[#|2013-01-10T15:03:28.218+0100|WARNING|glassfish3.1.2|org.eclipse.persistence.session.file:/E:/softs/serveurs/glassfish3_1122/glassfish/domains/domain1/applications/ear-0.0.1-SNAPSHOT/lib/entities-0.0.1-SNAPSHOT.jar_01|_ThreadID=72;_ThreadName=Thread-2;|Local 

Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): 



  org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: org.postgresql.util.PSQLException: ERREUR: le pseudo est déjà pris (player_name)=(hghghghggg)
    Error Code: 0
Call: INSERT INTO pretoria.pseudos (ID, CHANGEMENT, PSEUDO, VERSION, joueur_id) VALUES (?, ?, ?, ?, ?)
    bind => [5 parameters bound]
Query: InsertObjectQuery(com.sim.entities.Pseudo@190c460)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processExceptionForCommError(DatabaseAccessor.java:1494)
    ................
Caused by: org.postgresql.util.PSQLException: ERREUR: le pseudo est déjà pris (player_name)=(hghghghggg)
    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)
    ................
|#]

[#|2013-01-10T15:03:28.218+0100|WARNING|glassfish3.1.2|org.eclipse.persistence.session.file:/E:/softs/serveurs/glassfish3_1122/glassfish/domains/domain1/applications/ear-0.0.1-SNAPSHOT/lib/entities-0.0.1-SNAPSHOT.jar_01|_ThreadID=72;_ThreadName=Thread-2;|Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERREUR: la transaction est annulée, les commandes sont ignorées jusqu'à la fin du bloc
de la transaction
Error Code: 0
Call: INSERT INTO pretoria.joueurs (ID, BLOQUE, cle_validation, creation, EMAIL, LANGAGE, maj_temperament, NEWSLETTER, PASSWORD, POUSSIERE, SEL, TEMPERAMENT, VACANCES, VERSION, alliance_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [15 parameters bound]
Query: InsertObjectQuery(com.sim.entities.Joueur@1a21ad9)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processExceptionForCommError(DatabaseAccessor.java:1494)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:838)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:906)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:592)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1717)
    ................
Caused by: org.postgresql.util.PSQLException: ERREUR: la transaction est annulée, les commandes sont ignorées jusqu'à la fin du bloc
de la transaction
    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)
    ................
|#]

[#|2013-01-10T15:03:28.234+0100|WARNING|glassfish3.1.2|org.eclipse.persistence.session.file:/E:/softs/serveurs/glassfish3_1122/glassfish/domains/domain1/applications/ear-0.0.1-SNAPSHOT/lib/entities-0.0.1-SNAPSHOT.jar_01.transaction|_ThreadID=72;_ThreadName=Thread-2;|Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERREUR: la transaction est annulée, les commandes sont ignorées jusqu'à la fin du bloc
de la transaction
Error Code: 0
Call: INSERT INTO pretoria.joueurs (ID, BLOQUE, cle_validation, creation, EMAIL, LANGAGE, maj_temperament, NEWSLETTER, PASSWORD, POUSSIERE, SEL, TEMPERAMENT, VACANCES, VERSION, alliance_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [15 parameters bound]
Query: InsertObjectQuery(com.sim.entities.Joueur@1a21ad9)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processExceptionForCommError(DatabaseAccessor.java:1494)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:838)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:906)
    ................
Caused by: org.postgresql.util.PSQLException: ERREUR: la transaction est annulée, les commandes sont ignorées jusqu'à la fin du bloc
de la transaction
    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)
    ................
|#]

[#|2013-01-10T15:03:28.234+0100|WARNING|glassfish3.1.2|javax.enterprise.system.core.transaction.com.sun.jts.jta|_ThreadID=72;_ThreadName=Thread-2;|JTS5054: Unexpected error occurred in after completion
Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERREUR: la transaction est annulée, les commandes sont ignorées jusqu'à la fin du bloc
de la transaction
Error Code: 0
Call: INSERT INTO pretoria.joueurs (ID, BLOQUE, cle_validation, creation, EMAIL, LANGAGE, maj_temperament, NEWSLETTER, PASSWORD, POUSSIERE, SEL, TEMPERAMENT, VACANCES, VERSION, alliance_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [15 parameters bound]
Query: InsertObjectQuery(com.sim.entities.Joueur@1a21ad9)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processExceptionForCommError(DatabaseAccessor.java:1494)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:838)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:906)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:592)
    ................
Caused by: org.postgresql.util.PSQLException: ERREUR: la transaction est annulée, les commandes sont ignorées jusqu'à la fin du bloc
de la transaction
    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)
    ................
|#]

[#|2013-01-10T15:03:28.234+0100|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=72;_ThreadName=Thread-2;|EJB5184:A system exception occurred during an invocation on EJB AccountBean, method: public void com.sim.ejbs.stateless.AccountBean.createAccount(java.lang.String,char[],java.lang.String,com.sim.basics.enums.TemperamentEnum,com.sim.dtos.xml.universes.Universe,int,int,int,java.lang.String,java.lang.String) throws com.sim.basics.exceptions.SimRuntimeException|#]

[#|2013-01-10T15:03:28.234+0100|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=72;_ThreadName=Thread-2;|javax.ejb.EJBException: Transaction aborted
    at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5142)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)
    ................
Caused by: javax.transaction.RollbackException
    at com.sun.jts.jta.TransactionManagerImpl.commit(TransactionManagerImpl.java:334)
    at com.sun.enterprise.transaction.jts.JavaEETransactionManagerJTSDelegate.commitDistributedTransaction(JavaEETransactionManagerJTSDelegate.java:185)
    at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:861)
    at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5136)
    ... 53 more
|#]

Log4j 日志:

2013-01-10 15:03:28,218 FATAL   [com.sim.ejbs.stateless.AccountBean] 

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERREUR: le pseudo est déjà pris (player_name)=(hghghghggg)
Error Code: 0
Call: INSERT INTO pretoria.pseudos (ID, CHANGEMENT, PSEUDO, VERSION, joueur_id) VALUES (?, ?, ?, ?, ?)
    bind => [5 parameters bound]
Query: InsertObjectQuery(com.sim.entities.Pseudo@190c460)
2013-01-10 15:03:28,234 FATAL   [com.sim.web.beans.LoginBean] 123
4

2 回答 2

1

IIRC 某些数据库实际上可能会在事务提交时执行语句,因此请尝试调用 commit(而不是flush(),仅用于测试)并查看它是否也会发生。

编辑:另一方面,JPA 需要在完成后立即获取持久实体的 ID(如果生成策略是 IDENTITY 或 SEQUENCE),所以我上面所说的可能不正确......

于 2013-01-10T13:27:28.410 回答
0

是的,flush 总是会刷新。您可能没有执行您认为的代码,或者可能没有得到您期望的异常。启用最好的登录,并尝试调试。

于 2013-01-10T14:08:05.030 回答