2

While deploying application on Tomcat 8.0.36 I am getting following exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eventsDAOImpl' Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType; at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:940) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1816) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType; at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.(PersistenceAnnotationBeanPostProcessor.java:653) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.lambda$buildPersistenceMetadata$0(PersistenceAnnotationBeanPostProcessor.java:416) at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:693) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:410) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:394) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:332) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1015) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ... 24 more

I am using eclipselink version 2.5.2 and added following jars in the lib folder of tomcat

  1. eclipselink-2.5.2.jar
  2. javax.persistence-api-2.2.jar
  3. ojdbc6-12.1.0.1.jar

dependencies.gradle is as follows

compile (
        'org.aspectj:aspectjrt:1.8.4',
        'org.aspectj:aspectjweaver:1.8.4',
        'org.springframework:spring-aspects:5.0.0.RELEASE',
        'org.springframework:spring-jdbc:5.0.0.RELEASE',
        'org.springframework:spring-jms:5.0.0.RELEASE',
        'org.springframework:spring-orm:5.0.0.RELEASE',
        'org.springframework:spring-oxm:5.0.0.RELEASE',
        'org.springframework:spring-web:5.0.0.RELEASE',
        'org.springframework:spring-webmvc:5.0.0.RELEASE',
        'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
    }
    provided (
        'com.oracle:ojdbc6:12.1.0.1',
        'org.eclipse.persistence:eclipselink:2.5.2'
    )

Edit 1 : Same application is running fine on Tomcat 9 but giving above exception while deploying on Tomcat 8

Edit 2 : After doing some research I found out that Tomcat 8 is loading PersistenceContext class from annotations-api.jar located in TOMCAT_HOME\lib folder instead of javax.persistence-api-2.2.jar and PersistenceContext class available in annotations-api.jar does not contain synchronization method as follows.

package javax.persistence;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PersistenceContext {
  String name() default "";

  String unitName() default "";

  PersistenceContextType type() default PersistenceContextType.TRANSACTION;

  PersistenceProperty[] properties() default {};
}

How to give preference in Tomcat 8 such that Tomcat 8 will load PersistenceContext class from javax.persistence-api-2.2.jar instead of annotations-api.jar

Pawan Patil
  • 1,067
  • 5
  • 20
  • 46
  • javax.persistence.PersistenceContext.synchronization() returns javax.persistence.SynchronizationType which was only added in JPA 2.1 so you must have the earlier persistence-api jar in the container in a way that the class loader is finding and using it instead of your 2.2 version. – Chris Sep 07 '19 at 15:02
  • Chris thanks for help I have updated the question . Kindly see the edit 2 part of my question – Pawan Patil Sep 09 '19 at 09:25

3 Answers3

1

may be you are having conflicts in your spring data dependency version One is vesion1 (or replication of two same jar you can say)another is version2 Please remove older version dependency either version one or version two from pom.xml or from build path jar

OR

may be there is no Spring data jar or dependency over there please kindly add it.

0

org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Finals delivers an OLD version of javax.persistance that lack of that API PersitanceContext().synchronization()

Solution: add java.persistence:javax.persistence-api:2.2 BEFORE hibernate-jpa-2.0-api in the pom.xml

        <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>

No idea about gradle, but at least you can try.

RSG
  • 384
  • 3
  • 7
-1

I see PersistenceContext.class with the synchronization() method in javax.persistence-api-2.2.jar. It looks like Apache has removed PersistenceContext (along with other classes) from annotations-api.jar as of Tomcat 7 which means it can't be loading this class from the Tomcat provided jar. Perhaps you have a another jar in your application that has an old version of PersistenceContext in it?

splashout
  • 537
  • 5
  • 11
  • I had the same idea and added java.persistence:javax.persistence-api:2.2 to the pom.xml but NOTHING CHANGED. Later I instrument to print the resource path for the PersistenceContext and both where loaded from org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Fiinals – RSG Nov 25 '20 at 21:42