hibernate - spring transaction at service layer -


in our application applying spring declarative transactions using annotations @ service layer.

here not getting idea on how handle exceptions properly.

what requirement when dao layer throws hibernate exception rolling transaction, in 1 case getting invaliddataaccessresourceusageexception because there unique index violation happening. here want catch invaliddataaccessresourceusageexception exception @ service class , have rethrow application specific exception controller.

but whats happening here have transaction demarcation @ service layer class session flushing @ service layer(ie when tx commits) after executing method, result cant catch same method , directly propagating controller.

please suggest me work around on this.

also seeking 1 more clarification, suppose have method below

@transactional(value="transactionmanager",readonly = false, propagation    = propagation.required,rollbackfor = hibernateexception.class)   public somedto editobject(somedto somedto, string user)   throws        editexception    {              try{         /*           call dao.edit();          call anotherdao.addeditstoanothertable();         business logic*/         } catch(hibernateexception e){        } catch(invaliddataaccessresourceusageexception ie){}     } 

can catch exceptions above. note: not handling or throwing exceptions dao. there no session cache mechanisms flushmode.always etc @ dao layer flush during tx.commit().

by default @transactional rollback runtimeexception, , since hibernateexception runtimeexception , roll done automaticaly , don't have add rollbackfor = hibernateexception.class can handle exception way:

try{ }catch(invaliddataaccessresourceusageexception e){  throw new yourapplicationexceptionnotuniqueindex(); } 

and :

yourapplicationexceptionnotuniqueindex shoud extends runtimeexception way wil have rollback @ sevice layer , can catch exception @ controller .


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -