spring - LazyInitializationException in AOP logger aspect -
i have app build spring, jsf jpa , hibernate.
have aspect "watches" every update*, create*,delete* method service layer of application. aspect logs method params, apply tostring every param , them log them in database. problem use domain objects in jsf , when try update* lazyinitializationexception when tostring() applied method param.
1 solution remove tostring params represents other objects operation has no sense not log details interests me.
ie. have entity called price has dependency pricelist:
public class price extends baseentity implements serializable { private static final long serialversionuid = 1l; @column(name = "price") private double price; //bi-directional many-to-one association telcopricelist @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "price_list_id") private pricelist pricelist; ..................... }
and price used addpriceform.xhtml, jsf form. jsf addpricemb has reference priceservice performs actual update.
priceservice "monitored" aspect:
@aspect @named("auditlogaspectimpl") public class auditlogaspectimpl implements auditlogaspect { @inject private userservice userservice; @inject private auditlogservice auditlogservice; @override @afterreturning(pointcut = "execution(* com.videanuadrian.core.impl.services..*.save*(..)) or execution(* com.videanuadrian.core.impl.services..*.update*(..)) or execution(* com.videanuadrian.core.impl.services..*.delete*(..))", returning = "retval") public boolean afterlogevent(joinpoint joinpoint,object retval) { string classname = joinpoint.gettarget().getclass().getname(); string methondname = joinpoint.getsignature().getname(); ................................................................... stringbuffer logmessage = new stringbuffer(); object[] args = joinpoint.getargs(); //for login action username , hash password if (methondname.compareto("login") == 0){ logmessage.append(args[0]); }else { int argslen = args.length; //else log parameters (int = 0; < argslen; i++) { // exception occurs !!!!!!!!!!!!!!!!!!!!!!!!!!! logmessage.append(args[i]).append(","); } if (argslen > 0) { logmessage.deletecharat(logmessage.length() - 1); } } //some save/update methods return boolean boolean status = false; if (retval instanceof boolean){ status = (boolean) retval; } //some return id of object inserted,and if these methods return integer status true, if return null, status si false if (retval instanceof integer){ if (retval!=null) status = true; } auditlogservice.addauditlogevent(uid, classname+"."+methondname, status,logmessage.tostring()); return true; }
on prior version did not had problem because had used dto objects , conversion between dto , domain objects performed in service layer. error clear, session closed long ago compared time perform tostring() operation. idea how can achieve without using opensessioninview or extended persistence context? or maybe approach not good....
Comments
Post a Comment