Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 4.35 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. AOP can not catch the DAO exception in spring due to proxy side effect by spring injection
  2. <bean id="DBExceptions" class="com.dao.impl.DAOExceptionTranslator" />
  3.     <aop:config>
  4.         <aop:aspect id="dbExceptionsAspect" ref="DBExceptions">
  5.             <aop:after-throwing throwing="ex"
  6.                 pointcut="execution(* com.dao.impl.*.*(*))" method="doDAOActions" />
  7.         </aop:aspect>
  8.     </aop:config>
  9.        
  10. @Transactional
  11. public class UserDAOImpl extends GenericDAOImpl implements UserDAO {
  12.  
  13.     @PersistenceContext
  14.     protected EntityManager entityManager;
  15.  
  16.     @Override
  17.     public User findUserByUsername(String username) throws DAOException {
  18.         Query query = entityManager
  19.                 .createQuery("select u from User u where u.username=:username");
  20.         query.setParameter("username", username);
  21.  
  22.         Object userObject = query.getSingleResult();
  23.  
  24.         return (User) userObject;
  25.     }
  26.        
  27. private UserDAO userDAO;
  28. public User getUserById(int id) throws UserServiceException {
  29.         try {
  30.             Object user = userDAO.findById(User.class, id);
  31. ...
  32.        
  33. UserDAOImpl.findUserByUsername(String) line: 23
  34.     NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
  35.     NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
  36.     DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
  37.     Method.invoke(Object, Object...) line: 597  
  38.     AopUtils.invokeJoinpointUsingReflection(Object, Method, Object[]) line: 309
  39.     ReflectiveMethodInvocation.invokeJoinpoint() line: 183  
  40.     ReflectiveMethodInvocation.proceed() line: 150  
  41.     AspectJAfterThrowingAdvice.invoke(MethodInvocation) line: 55    
  42.     ReflectiveMethodInvocation.proceed() line: 172  
  43.     TransactionInterceptor.invoke(MethodInvocation) line: 110  
  44.     ReflectiveMethodInvocation.proceed() line: 172  
  45.     ExposeInvocationInterceptor.invoke(MethodInvocation) line: 89  
  46.     ReflectiveMethodInvocation.proceed() line: 172  
  47.     JdkDynamicAopProxy.invoke(Object, Method, Object[]) line: 202  
  48.     $Proxy17.findUserByUsername(String) line: not available
  49.     UserService.getUser(String) line: 74
  50.        
  51. at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:382)
  52.     at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
  53.     at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
  54.     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
  55.     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  56.     at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
  57.     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  58.     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
  59.     at $Proxy17.findUserByUsername(Unknown Source)
  60.     at com.service.UserService.getUser(UserService.java:74)
  61.        
  62. <aop:config>
  63.         <aop:aspect id="dbExceptionsAspect" ref="DBExceptions" order="1">
  64.             <aop:after-throwing throwing="ex"
  65.                 pointcut="execution(* com.dao.impl.*.*(*))" method="doDAOActions" />
  66.         </aop:aspect>
  67.     </aop:config>
  68.        
  69. DAOExceptionTranslator.doDAOActions(Exception) line: 12
  70.     NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
  71.     NativeMethodAccessorImpl.invoke(Object, Object[]) line: 39  
  72.     DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 25  
  73.     Method.invoke(Object, Object...) line: 597  
  74.     AspectJAfterThrowingAdvice(AbstractAspectJAdvice).invokeAdviceMethodWithGivenArgs(Object[]) line: 621  
  75.     AspectJAfterThrowingAdvice(AbstractAspectJAdvice).invokeAdviceMethod(JoinPointMatch, Object, Throwable) line: 603  
  76.     AspectJAfterThrowingAdvice.invoke(MethodInvocation) line: 59    
  77.     ReflectiveMethodInvocation.proceed() line: 172  
  78.     ExposeInvocationInterceptor.invoke(MethodInvocation) line: 89  
  79.     ReflectiveMethodInvocation.proceed() line: 172  
  80.     JdkDynamicAopProxy.invoke(Object, Method, Object[]) line: 202  
  81.     $Proxy17.findUserByUsername(String) line: not available
  82.     UserService.getUser(String) line: 74