java - Handling exception in Service layer -
could please advice how overcome problem described below:
there application (it involves using hibernate , spring frameworks) 3 layers: dao, service , controller. want avoid saving duplicate entities in db. made using constraints @ db-level , in annotation in bean (@table(name = "artist", uniqueconstraints = {@uniqueconstraint(columnnames = "artist_name")})
). logic supposes if try add duplicate entity exception thrown. try process exception in service layer fails succeed. mean when explicitly notice exception thrown application can't proceed work because of exceptions. more clear cite code below:
dao-layer
@override public void saveentity(client client) { session session = sessionfactory.getcurrentsession(); session.save(client); }
service-layer
@transactional @override public boolean saveentity(artist entity) { boolean completedstate = false; try { //method dao-layer artistdao.saveentity(entity); } catch (constraintviolationexception | unexpectedrollbackexception e) { return completedstate; } return completedstate = true; }
controller
@requestmapping(value="addartist", method=requestmethod.post) public string processnewartistform(@modelattribute artist artist, model model) { //if duplicated artistname prompts saved saveentity(entity) returns "false". entity failed save. boolean completedstate = false; completedstate = artistservice.saveentity(artist); if (completedstate == true) { list<artist> listofartists = artistservice.getlistofentities(); model.addattribute("listofartists", listofartists); } else { model.addattribute("errormessage", "an artist under present name exists"); return "newartist"; } return "listofartists"; }
tomcat's notification when try save duplicate entity
type exception report message request processing failed; nested exception org.springframework.transaction.unexpectedrollbackexception: transaction rolled because has been marked rollback-only description server encountered internal error prevented fulfilling request. exception org.springframework.web.util.nestedservletexception: request processing failed; nested exception org.springframework.transaction.unexpectedrollbackexception: transaction rolled because has been marked rollback-only org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:978) org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:868) javax.servlet.http.httpservlet.service(httpservlet.java:644) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842) javax.servlet.http.httpservlet.service(httpservlet.java:725) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) root cause org.springframework.transaction.unexpectedrollbackexception: transaction rolled because has been marked rollback-only org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager.java:720) org.springframework.transaction.interceptor.transactionaspectsupport.committransactionafterreturning(transactionaspectsupport.java:521) org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:291) org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:96) org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:207) com.sun.proxy.$proxy29.saveentity(unknown source) web.artistslistcontroller.processnewartistform(artistslistcontroller.java:66) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) java.lang.reflect.method.invoke(method.java:606) org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:137) org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:110) org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlemethod(requestmappinghandleradapter.java:777) org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:706) org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:943) org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:877) org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:966) org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:868) javax.servlet.http.httpservlet.service(httpservlet.java:644) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842) javax.servlet.http.httpservlet.service(httpservlet.java:725) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) note full stack trace of root cause available in apache tomcat/8.0.9 logs.
could please advice if these architectural solution correct or not? thank in advance.
why not check duplications before saving?:
@transactional @override public void saveentity(client client) { session session = sessionfactory.getcurrentsession(); // session.get() query database , return null // if no rows found if (session.get(client.class,client.getid()==null) { session.save(client); //flush session cache session.flush(); } }
Comments
Post a Comment