mysql - Hibernate session fails to query after ConstraintViolationException leading to AssertionFailure -
i'm doing batch processing of records file in mysql through hibernate. i'm trying iterate on records , save them, on constraintviolationexception i'm trying update record in catch clause. how fails assertionfailure exception.
i'm bounded use sessions rather statelesssessions.
try { system.out.println("inserting "+key); session.save(mud); } catch(hibernateexception cve) { cve.printstacktrace(); //log.error("failed insert, record exists: "+key); system.out.println("failed insert, record exists: "+key); list res = session.createquery("from userdata ud ud.taskid = "+taskid+" , ud.userid= "+userid) .list(); system.out.println("query successfull"); if (!res.isempty()) { mud.setid( ((userdata) res.get(0)).getid() ); session.save(mud); continue; } }
here's stacktrace:
inserting 463531145008722 org.hibernate.exception.constraintviolationexception: not insert: [com.test.www.userdata] @ org.hibernate.exception.sqlstateconverter.convert(sqlstateconverter.java:96) @ org.hibernate.exception.jdbcexceptionhelper.convert(jdbcexceptionhelper.java:66) @ org.hibernate.id.insert.abstractreturningdelegate.performinsert(abstractreturningdelegate.java:64) @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:2345) @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:2852) @ org.hibernate.action.entityidentityinsertaction.execute(entityidentityinsertaction.java:71) @ org.hibernate.engine.actionqueue.execute(actionqueue.java:273) @ org.hibernate.event.def.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:320) @ org.hibernate.event.def.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:203) @ org.hibernate.event.def.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:129) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:210) @ org.hibernate.event.def.defaultsaveeventlistener.savewithgeneratedorrequestedid(defaultsaveeventlistener.java:56) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:195) @ org.hibernate.event.def.defaultsaveeventlistener.performsaveorupdate(defaultsaveeventlistener.java:50) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:93) @ org.hibernate.impl.sessionimpl.firesave(sessionimpl.java:713) @ org.hibernate.impl.sessionimpl.save(sessionimpl.java:701) @ org.hibernate.impl.sessionimpl.save(sessionimpl.java:697) @ com.test.www.csvtodb.loadcsvtodb_statelessnotransaction(csvtodb.java:92) @ com.test.www.csvtodb.run(csvtodb.java:52) @ com.test.www.csvtodb.main(csvtodb.java:175) caused by: com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception: duplicate entry '463531-145008722' key 'taskid' @ sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(nativeconstructoraccessorimpl.java:39) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(delegatingconstructoraccessorimpl.java:27) @ java.lang.reflect.constructor.newinstance(constructor.java:513) @ com.mysql.jdbc.util.handlenewinstance(util.java:411) @ com.mysql.jdbc.util.getinstance(util.java:386) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1041) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4190) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:4122) @ com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:2570) @ com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:2731) @ com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2818) @ com.mysql.jdbc.preparedstatement.executeinternal(preparedstatement.java:2157) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2460) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2377) @ com.mysql.jdbc.preparedstatement.executeupdate(preparedstatement.java:2361) @ org.hibernate.id.identitygenerator$getgeneratedkeysdelegate.executeandextract(identitygenerator.java:94) @ org.hibernate.id.insert.abstractreturningdelegate.performinsert(abstractreturningdelegate.java:57) ... 18 more failed insert, record exists: 463531145008722 org.hibernate.assertionfailure: null id in com.test.www.userdata entry (don't flush session after exception occurs) @ org.hibernate.event.def.defaultflushentityeventlistener.checkid(defaultflushentityeventlistener.java:82) @ org.hibernate.event.def.defaultflushentityeventlistener.getvalues(defaultflushentityeventlistener.java:190) @ org.hibernate.event.def.defaultflushentityeventlistener.onflushentity(defaultflushentityeventlistener.java:147) @ org.hibernate.event.def.abstractflushingeventlistener.flushentities(abstractflushingeventlistener.java:219) @ org.hibernate.event.def.abstractflushingeventlistener.flusheverythingtoexecutions(abstractflushingeventlistener.java:99) @ org.hibernate.event.def.defaultflusheventlistener.onflush(defaultflusheventlistener.java:50) @ org.hibernate.impl.sessionimpl.flush(sessionimpl.java:1216) @ com.test.www.csvtodb.loadcsvtodb_statelessnotransaction(csvtodb.java:98) @ com.test.www.csvtodb.run(csvtodb.java:52) @ com.test.www.csvtodb.main(csvtodb.java:175)
looking better (and checking hibernate code) seems problem during query.list() (in stacktrace query successfull missing) , automatic flush running before query execution. try change flushmode commit or session.flush() before query.
do @ spring-batch kind of job?
Comments
Post a Comment