Hey everybody,
I need to catch the exception that occurs when a user tries to insert a duplicate role into my system. For that, I catch ConstraintViolationException. Now, this used to work. After I quit working on the project for a couple of days and went back to my system, it stopped working.
Now, in the case that the database reports a violation of the unique constraint, I get a JDBCException. As far as I know, that is not the correct behavior. What am I doing wrong? My Hibernate is configured to use the appropriate MySQLDialect (apart from that, the Hibernate configuration didn't change).
Any ideas? I'm puzzled and out of clues.
Hibernate version:
2.1
Mapping documents:
Code:
<hibernate-mapping>
<!-- The Role entity -->
<class name="de.iu.myIU.common.entities.Role" table="role">
<id name="id" column="id" type="integer" unsaved-value="null">
<generator class="native" />
</id>
<property name="name" column="name" type="string"
length="255" not-null="true" unique="true" />
<property name="standard" column="standard" type="boolean"
not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session sess = SessFactHolder.newSession();
Role newRole = new Role();
newRole.setName(this.name);
newRole.setStandard(false);
try {
sess.saveOrUpdate(newRole);
} catch (ConstraintViolationException e) {
// can only be a duplicate since this is the only constraint on
// this table
sess.close();
return ERR_DUPLICATE_NAME;
}
sess.close();
return SUCCESS;
Full stack trace of any exception that occurs:Code:
net.sf.hibernate.JDBCException: could not insert: [de.iu.myIU.common.entities.Role]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:558)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
at de.iu.myIU.admin.beans.RolesBean.addNewRole(RolesBean.java:73)
at org.apache.jsp.admin.roles_005fadd_jsp._jspService(roles_005fadd_jsp.java:216)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:54)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Duplicate entry 'Test 1' for key 2"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
at com.mchange.v2.sql.filter.FilterPreparedStatement.executeUpdate(FilterPreparedStatement.java:71)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:528)
... 48 more
Name and version of the database you are using:MySQL 4.0.23
The generated SQL (show_sql=true):Code:
insert into role (name, standard) values (?, ?)
Debug level Hibernate log excerpt:
Quote:
23:01:46,200 DEBUG [SessionImpl] opened session
23:01:46,201 DEBUG [SessionImpl] saveOrUpdate() unsaved instance
23:01:46,201 DEBUG [SessionImpl] saving [de.iu.myIU.common.entities.Role#<null>]
23:01:46,201 DEBUG [SessionImpl] executing insertions
23:01:46,201 DEBUG [EntityPersister] Inserting entity: de.iu.myIU.common.entities.Role (native id)
23:01:46,201 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
23:01:46,202 DEBUG [SQL] insert into role (name, standard) values (?, ?)
23:01:46,202 INFO [STDOUT] Hibernate: insert into role (name, standard) values (?, ?)
23:01:46,203 DEBUG [BatcherImpl] preparing statement
23:01:46,203 DEBUG [EntityPersister] Dehydrating entity: [de.iu.myIU.common.entities.Role#<null>]
23:01:46,204 DEBUG [StringType] binding 'Test 1' to parameter: 1
23:01:46,205 DEBUG [BooleanType] binding 'false' to parameter: 2
23:01:46,211 DEBUG [JDBCExceptionReporter] SQL Exception
java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Duplicate entry 'Test 1' for key 2"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
at com.mchange.v2.sql.filter.FilterPreparedStatement.executeUpdate(FilterPreparedStatement.java:71)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:528)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
at de.iu.myIU.admin.beans.RolesBean.addNewRole(RolesBean.java:73)
at org.apache.jsp.admin.roles_005fadd_jsp._jspService(roles_005fadd_jsp.java:216)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:54)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
23:01:46,243 WARN [JDBCExceptionReporter] SQL Error: 1062, SQLState: 23000
23:01:46,244 ERROR [JDBCExceptionReporter] Duplicate key or integrity constraint violation message from server: "Duplicate entry 'Test 1' for key 2"
23:01:46,244 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
23:01:46,250 DEBUG [BatcherImpl] closing statement
23:01:46,250 DEBUG [JDBCExceptionReporter] SQL Exception
java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Duplicate entry 'Test 1' for key 2"