-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Question on exceptions on duplicate entries
PostPosted: Mon Dec 05, 2005 10:27 pm 
Newbie

Joined: Tue Sep 27, 2005 4:20 am
Posts: 6
Hi I'm trying to post again because it didn't seem to work the first time apologies if this is a duplicate entry

I was trying to make the application more 'real' but adding a primary key containt to the Student table on login I actually want to do it on User but I digress

The idea is that I would catch PK violation and control the output unfortunately the error propagates to the browser and no amount of try catch in the RegisterStudentController seems to catch the error presumable because things are only happening after when hibernate flushes the data.

Any ideas how to trap this kind of error?

RegisterStudentController
protected void doSubmitAction(Object command)
throws Exception {

Student student = (Student) command;

LOGGER.debug("firstName=[" + student.getFirstName() + "]");

LOGGER.debug("**************************** Enrolling student");

try {

studentService.registerStudent(student);

} catch(Throwable e) {
e.printStackTrace();
throw (Exception)e;
}

LOGGER.debug("**************************** Student enrolled.");
}



[DEBUG] 21:58:08 RegisterStudentController - Displaying new form
[DEBUG] 21:58:08 RegisterStudentController - Called formBackingObject
[DEBUG] 21:58:08 RegisterStudentController - Creating new command of class [com.springinaction.training.model.Student]
[DEBUG] 21:58:08 RegisterStudentController - Called initBinder
[DEBUG] 21:58:20 RegisterStudentController - Called formBackingObject
[DEBUG] 21:58:20 RegisterStudentController - Creating new command of class [com.springinaction.training.model.Student]
[DEBUG] 21:58:20 RegisterStudentController - Called initBinder
[DEBUG] 21:58:20 RegisterStudentController - No errors -> processing submit
[DEBUG] 21:58:20 RegisterStudentController - firstName=[ghg]
[DEBUG] 21:58:20 RegisterStudentController - **************************** Enrolling student
[DEBUG] 21:58:20 StudentDaoImpl - .create() BEGIN
[DEBUG] 21:58:20 StudentDaoImpl - org.springframework.orm.hibernate3.HibernateTemplate
com.springinaction.training.dao.StudentDaoImpl.create() END
[DEBUG] 21:58:20 RegisterStudentController - **************************** Student enrolled.
Hibernate: insert into Users (password, login) values (?, ?)
Hibernate: insert into Student (firstName, middleName, lastName, address1, address2, city, state, zip, phone, email, login) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[ WARN] 21:58:20 JDBCExceptionReporter - SQL Error: 2627, SQLState: 23000
[ERROR] 21:58:20 JDBCExceptionReporter - Violation of PRIMARY KEY constraint 'PK_Student'. Cannot insert duplicate key in object 'Student'.
[ERROR] 21:58:20 AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [com.springinaction.training.model.Student]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:299)
at org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor.postHandle(OpenSessionInViewInterceptor.java:183)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:690)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:355)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:84)
at net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter.doFilter(SecurityEnforcementFilter.java:182)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at net.sf.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:305)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at net.sf.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:225)
at net.sf.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: Violation of PRIMARY KEY constraint 'PK_Student'. Cannot insert duplicate key in object 'Student'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:364)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2754)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2195)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:620)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:483)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:445)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:402)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1856)
... 46 more


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 05, 2005 10:46 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
what happens when you commit the transaction inside the try block?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.