jpa“脏”交易

时间:2012-08-22 16:42:51

标签: java hibernate jpa transactions

我使用java,jpa(和hibernate)编写了一个桌面应用程序。

我有一个页面,用户可以编辑bean字段并将其保存到数据库。 bean的字段codice是唯一的。该数据库有2行,一行codice = 0,第二行codice = 00

用法是编辑第一个bean 0并将codice字段更改为00,并尝试保留它。 entityManager.commit()将引发异常:

ERROR: Duplicate entry '00' for key 'codice'

该异常被捕获并显示给用户,它将codice字段修改为11(尚不存在)并尝试保留。

entityManager.commit()再次提出同样的异常

(ERROR: Duplicate entry '00' for key )

喜欢它再次尝试坚持旧项目。

这里有一个案例:

EntityManager em = Persistence.createEntityManagerFactory(
            "testNewDataModel").createEntityManager();
Articolo a = em.find(Articolo.class, 1L);
EntityTransaction tx = em.getTransaction();
a.setNome("albi8");
// ---- 1 ---- just a normal commit, no errors
tx.begin();     
a = em.merge(a);        
tx.commit();        
System.out.println("---- 1 ----");
// ---- 2 ---- will cause Duplicate entry error
tx.begin();     
a.setCodice("00");  //  already exist an item with codice = 00 -> Duplicate entry '00' for key 'codice'
a = em.merge(a);        
try {
    System.out.println("---- 2 ---- pre commit");
    tx.commit();
    System.out.println("---- 2 ---- after commit");
} catch (Exception e) {
    System.out.println("Duplicate entry '00' for key 'codice'");
}

// ---- 3 ---- trying to correct the previous error 

tx.begin();

a.setCodice("11"); //this code don't exist yet
a.setNome("alby9");

a = em.merge(a);

try {
    System.out.println("---- 3 ---- pre commit");
tx.commit();
    System.out.println("---- 3 ---- after commit");
} catch (Exception e) {
    e.printStackTrace(); //Duplicate entry '00' for key 'codice'
}

和她的输出:

org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
22-ago-2012 18.41.02 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select articolo0_._id as column1_12_0_, articolo0_.id_categoria as id10_12_0_, articolo0_.codice as codice12_0_, articolo0_.coefficiente_prezzo as coeffici3_12_0_, articolo0_.descrizione as descrizi4_12_0_, articolo0_.id_foto as id5_12_0_, articolo0_.nome as nome12_0_, articolo0_.posizione as posizione12_0_, articolo0_.prezzo_imponibile as prezzo8_12_0_, articolo0_.prezzo_vendita as prezzo9_12_0_, articolo0_1_.quantita as quantita13_0_ from inventario4.articolo articolo0_ left outer join inventario4.magazzino articolo0_1_ on articolo0_._id=articolo0_1_._id_articolo where articolo0_._id=?
---- 1 ----
---- 2 ---- pre commit
Hibernate: update inventario4.articolo set id_categoria=?, codice=?, coefficiente_prezzo=?, descrizione=?, id_foto=?, nome=?, posizione=?, prezzo_imponibile=?, prezzo_vendita=? where _id=?
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '00' for key 'codice'
Duplicate entry '00' for key 'codice'
---- 3 ---- pre commit
Hibernate: update inventario4.articolo set id_categoria=?, codice=?, coefficiente_prezzo=?, descrizione=?, id_foto=?, nome=?, posizione=?, prezzo_imponibile=?, prezzo_vendita=? where _id=?
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
22-ago-2012 18.41.02 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '00' for key 'codice'
javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:90)
    at org.lelmarir.test.Main.main(Main.java:79)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Duplicate entry '00' for key 'codice'
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:78)
    ... 1 more
Caused by: org.hibernate.exception.ConstraintViolationException: Duplicate entry '00' for key 'codice'
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at $Proxy21.executeUpdate(Unknown Source)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3006)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2908)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3237)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:113)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:187)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:315)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73)
    ... 1 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '00' for key 'codice'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
    ... 18 more

最后一个问题:为什么交易仍然试图与codice = 00保持一致?

2 个答案:

答案 0 :(得分:0)

我怀疑问题是您在事务之间重用了相同的对象。我要clear实体管理器,或使用find加载新对象。但我不确定,我很久没有使用过JPA了。

答案 1 :(得分:0)

尝试在提交失败后显式回滚事务

try {
System.out.println("---- 2 ---- pre commit");
tx.commit();
System.out.println("---- 2 ---- after commit");
} catch (Exception e) {
    System.out.println("Duplicate entry '00' for key 'codice'");

    tx.rollback(); // HERE

}