Announcement Announcement Module
Collapse
No announcement yet.
java.util.ConcurrentModificationException is thrown when using Spring AOP Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.util.ConcurrentModificationException is thrown when using Spring AOP

    I've written a web service using Jersey and Spring. I am using Spring AOP to perform entity validation for some service methods. Everything seems to work fine, except for every few hours or so, a java.util.ConcurrentModificationException is thrown from the Spring AOP code. I can't consistently reproduce the issue yet. More precisely, the exception is caused by code in the AspectJ libraries used by Spring AOP. I am using Spring AOP version 3.1.2.RELEASE and version 1.6.8 of the AspectJ libraries. I am not sure if there is an error in my code, or if there is a bug in Spring AOP, or the AspectJ libraries.

    I am using the @AspectJ style declaration for my aspect. Here is the code for it:

    Code:
    @Aspect
    @Component
    public class SpringAOPValidationAspect implements ValidationAspect
    {
        private Validator validator;
    
        @Autowired
        public void setValidator(Validator validator) {
            this.validator = validator;
        }
    
        @Pointcut("execution(* com.sensus.da.service.DomainEntityService.*(*)) && args(entity)")
        private void updateServiceMethod(Object entity) {}
    
        @Pointcut("execution(* com.sensus.da.data.core.IGenericRepository.save(*)) && args(entity)")
        private void createRepositoryMethod(Object entity) {}
    
        @Pointcut("execution(* com.sensus.da.ui.infrastructure.EntityAssembler.fromDto(*)) && args(dto)")
        private void fromDtoMethod(Object dto) {}
    
        @Pointcut("execution(* com.sensus.da.service.RemoteUpdateService.*(..)) && args(entity,..)")
        private void publishJobServiceMethod(Object entity) {}
    
        @Pointcut(value = "updateServiceMethod(entity) || createRepositoryMethod(entity) || fromDtoMethod(entity) || " +
                "publishJobServiceMethod(entity)", argNames = "entity")
        private void saveEntityMethod(Object entity) {}
    
        @Pointcut("execution(* com.sensus.da.data.core.IGenericRepository.delete(*)) && args(domainEntity)")
        private void deleteEntityMethod(IDomainEntity<?> domainEntity) {}
    
        @Before(value = "saveEntityMethod(entity)", argNames = "entity")
        public void validateSave(Object entity)
        {
            validate(entity);
        }
    
        @Before(value = "deleteEntityMethod(domainEntity)", argNames = "domainEntity")
        public void validateDelete(IDomainEntity<?> domainEntity)
        {
            validate(domainEntity, DeleteChecks.class);
        }
    
        public <T> void validate(T entity, Class<?>... groups)
        {
            final Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity, groups );
            if ( constraintViolations.size() > 0 )
            {
                Set<ConstraintViolation<?>> propagatedViolations =
                        new HashSet<ConstraintViolation<?>>( constraintViolations.size() );
                Set<String> classNames = new HashSet<String>();
                for ( ConstraintViolation<?> violation : constraintViolations )
                {
                    propagatedViolations.add( violation );
                    classNames.add( violation.getLeafBean().getClass().getName() );
                }
                StringBuilder builder = new StringBuilder();
                builder.append( "Validation failed for classes " );
                builder.append( classNames );
                builder.append( " during " );
                // builder.append( operation.getName() );
                builder.append( " time for groups " );
                //builder.append( toString( groups ) );
                builder.append( "\nList of constraint violations:[\n" );
                for (ConstraintViolation<?> violation : constraintViolations) {
                    builder.append( "\t" ).append( violation.toString() ).append("\n");
                }
                builder.append( "]" );
    
                throw new ConstraintViolationException(
                        builder.toString(), propagatedViolations
                );
            }
        }
    }
    The service and repository classes are fairly standard, so I don't think I need to show them here. I am using hibernate in the data access layer.

    I will post the exception and stack trace information in a follow up post to this, as it puts me over the character limit. I am fairly new to Spring AOP, so I may be missing something, even though I've read through all the documentation. Thanks in advance for your help.

    Brendan

  • #2
    Here is the exception and stack trace information that I get. It doesn't always fail when trying to autowire the same object. It can occur when injecting anything I have wired up. The problem is consistently in the same Spring AOP/AspectJ code, however.

    Thanks,
    Brendan

    Exception/Stack trace:
    Error creating bean with name 'profileDoubleBitBIPointDefinitionController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void com.sensus.da.ui.systemconfiguration.controller.Pr ofilePointDefinitionControllerBase.setPointDefinit ionRepository(com.sensus.da.data.repositories.IPro filePointDefinitionRepository); nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'profilePointDefinitionHibernateRepository' defined in URL [jar:file:/var/run/jetty-0.0.0.0-443-ui-3.0.1-SNAPSHOT.war-_-any-/webapp/WEB-INF/lib/fawkes-data-access-1.1.0-20121029.132054-51.jar!/com/sensus/da/data/repositories/ProfilePointDefinitionHibernateRepository.class]: Initialization of bean failed; nested exception is java.util.ConcurrentModificationException</pre></p><h3>Caused by:</h3><pre>org.springframework.beans.factory.BeanCrea tionException: Error creating bean with name 'profileDoubleBitBIPointDefinitionController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void com.sensus.da.ui.systemconfiguration.controller.Pr ofilePointDefinitionControllerBase.setPointDefinit ionRepository(com.sensus.da.data.repositories.IPro filePointDefinitionRepository); nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'profilePointDefinitionHibernateRepository' defined in URL [jar:file:/var/run/jetty-0.0.0.0-443-ui-3.0.1-SNAPSHOT.war-_-any-/webapp/WEB-INF/lib/fawkes-data-access-1.1.0-20121029.132054-51.jar!/com/sensus/da/data/repositories/ProfilePointDefinitionHibernateRepository.class]: Initialization of bean failed; nested exception is java.util.ConcurrentModificationException
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 287)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$2.getObject(AbstractBeanFactory.java:3 32)
    at org.springframework.web.context.request.AbstractRe questAttributesScope.get(AbstractRequestAttributes Scope.java:43)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:328 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:1102)
    at com.sun.jersey.spi.spring.container.SpringComponen tProviderFactory$SpringManagedComponentProvider.ge tInstance(SpringComponentProviderFactory.java:235)
    at com.sun.jersey.server.impl.component.IoCResourceFa ctory$PerRequestWrapper.getInstance(IoCResourceFac tory.java:150)
    at com.sun.jersey.server.impl.application.WebApplicat ionContext.getResource(WebApplicationContext.java: 238)
    at com.sun.jersey.server.impl.uri.rules.ResourceClass Rule.accept(ResourceClassRule.java:83)
    at com.sun.jersey.server.impl.uri.rules.RightHandPath Rule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceC lassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicat ionImpl._handleRequest(WebApplicationImpl.java:146 9)
    at com.sun.jersey.server.impl.application.WebApplicat ionImpl._handleRequest(WebApplicationImpl.java:140 0)
    at com.sun.jersey.server.impl.application.WebApplicat ionImpl.handleRequest(WebApplicationImpl.java:1349 )
    at com.sun.jersey.server.impl.application.WebApplicat ionImpl.handleRequest(WebApplicationImpl.java:1339 )
    at com.sun.jersey.spi.container.servlet.WebComponent. service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContai ner.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContai ner.service(ServletContainer.java:699)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:820)
    at org.eclipse.jetty.servlet.ServletHolder.handle(Ser vletHolder.java:652)


    ...had to cut out part of the trace here to make post fit the text size limit...

    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'profilePointDefinitionHibernateRepository' defined in URL [jar:file:/var/run/jetty-0.0.0.0-443-ui-3.0.1-SNAPSHOT.war-_-any-/webapp/WEB-INF/lib/fawkes-data-access-1.1.0-20121029.132054-51.jar!/com/sensus/da/data/repositories/ProfilePointDefinitionHibernateRepository.class]: Initialization of bean failed; nested exception is java.util.ConcurrentModificationException
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:313 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.resolvedCachedArgu ment(AutowiredAnnotationBeanPostProcessor.java:442 )
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.access$0(Autowired AnnotationBeanPostProcessor.java:435)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredMethodEle ment.resolveCachedArguments(AutowiredAnnotationBea nPostProcessor.java:603)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredMethodEle ment.inject(AutowiredAnnotationBeanPostProcessor.j ava:538)
    ... 56 more
    Caused by: java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification( AbstractList.java:372)
    at java.util.AbstractList$Itr.next(AbstractList.java: 343)
    at java.util.AbstractCollection.removeAll(AbstractCol lection.java:336)
    at org.aspectj.weaver.ReferenceType.findDerivativeTyp e(ReferenceType.java:1135)
    at org.aspectj.weaver.TypeFactory.createParameterized Type(TypeFactory.java:44)
    at org.aspectj.weaver.reflect.JavaLangTypeToResolvedT ypeConverter.fromType(JavaLangTypeToResolvedTypeCo nverter.java:79)
    at org.aspectj.weaver.reflect.Java15ReflectionBasedRe ferenceTypeDelegate.getSuperclass(Java15Reflection BasedReferenceTypeDelegate.java:148)
    at org.aspectj.weaver.ReferenceType.getSuperclass(Ref erenceType.java:891)
    at org.aspectj.weaver.ResolvedType$SuperClassWalker.n ext(ResolvedType.java:2193)
    at org.aspectj.weaver.ResolvedType$SuperClassWalker.n ext(ResolvedType.java:2171)
    at org.aspectj.weaver.Iterators$6.next(Iterators.java :293)
    at org.aspectj.weaver.patterns.KindedPointcut.fastMat ch(KindedPointcut.java:129)
    at org.aspectj.weaver.patterns.AndPointcut.fastMatch( AndPointcut.java:47)
    at org.aspectj.weaver.patterns.OrPointcut.fastMatch(O rPointcut.java:46)
    at org.aspectj.weaver.internal.tools.PointcutExpressi onImpl.couldMatchJoinPointsInType(PointcutExpressi onImpl.java:84)
    at org.springframework.aop.aspectj.AspectJExpressionP ointcut.matches(AspectJExpressionPointcut.java:249 )
    at org.springframework.aop.support.AopUtils.canApply( AopUtils.java:210)
    at org.springframework.aop.support.AopUtils.canApply( AopUtils.java:264)
    at org.springframework.aop.support.AopUtils.findAdvis orsThatCanApply(AopUtils.java:296)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.findAdvisorsThatCanApply (AbstractAdvisorAutoProxyCreator.java:117)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.findEligibleAdvisors(Abs tractAdvisorAutoProxyCreator.java:87)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.getAdvicesAndAdvisorsFor Bean(AbstractAdvisorAutoProxyCreator.java:68)
    at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.wrapIfNecessary(AbstractAutoPro xyCreator.java:359)
    at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.postProcessAfterInitialization( AbstractAutoProxyCreator.java:322)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyBeanPostProcessors AfterInitialization(AbstractAutowireCapableBeanFac tory.java:407)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1461)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
    ... 63 more

    Comment


    • #3
      java.util.ConcurrentModificationException is thrown when using Spring AOP

      Hi ,

      I am able to replicate the java.util.ConcurrentModificationException with Spring 3.2.0.RELEASE and Aspectj 1.7.1. It appears that the ReferenceType.java class uses an instance variable derivativeTypes.

      private final List<WeakReference<ReferenceType>> derivativeTypes = new ArrayList<WeakReference<ReferenceType>>();

      The variable is used in multiple places (used for iteration, used for adding, used for deletion)within the class. Since the List is not synchronized when one thread is modifying the List while another thread is iterating over it, the findDerivativeType() method at line 1122 of the org.aspectj.weaver.ReferenceType.java intermittently throws java.util.ConcurrentModificationException.

      Looking at your stracktrace, you are able to replicate the same side effect at line number 1135.

      Comment


      • #4
        java.util.ConcurrentModificationException is thrown when using Spring AOP

        Hi all,

        we are also running into this - we are using spring 3.2 and java 7.
        Is there a solution to avoid this problem?
        I have found a bug report at aspectj: https://bugs.eclipse.org/bugs/show_bug.cgi?id=408721

        Thanks for helping us.

        Thomas

        Comment


        • #5
          Hi Guys

          Originally posted by lfpommes View Post
          Hi all,

          we are also running into this - we are using spring 3.2 and java 7.
          Is there a solution to avoid this problem?
          I have found a bug report at aspectj: https://bugs.eclipse.org/bugs/show_bug.cgi?id=408721

          Thanks for helping us.

          Thomas
          Following Thomas's question, is there any work around for this issue?
          When exactly does it reproduce? I am experiencing it with prototype generic beans.

          Comment

          Working...
          X