Skip to content

DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl fails when run from a shaded jar [SPR-14246] #18819

Closed

Description

@spring-projects-issues

Andy Wilkinson opened SPR-14246 and commented

I'll open a PR with a sample that reproduces the problem. It boils down to a call to ClassLoader.getResource("") returning null when there are only jars on the classpath.

The failure is:

May 04, 2016 4:49:33 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@49c2faae: startup date [Wed May 04 16:49:33 BST 2016]; root of context hierarchy
May 04, 2016 4:49:33 PM org.springframework.context.annotation.AnnotationConfigApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManager' defined in com.example.ShadedClasspathRootApplication: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManager' defined in com.example.ShadedClasspathRootApplication: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
	at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
	at com.example.ShadedClasspathRootApplication.main(ShadedClasspathRootApplication.java:21)
Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
	at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:591)
	at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:443)
	at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:424)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:310)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
	... 11 more
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
	at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187)
	at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:588)
	... 18 more

This also affects an executable jar built by Spring Boot. We used to have a hack in Boot's custom class loader that worked around this issue, but that doesn't help people who are using shaded jars, and we would prefer not to have to reintroduce the hack.


Affects: 4.3 RC1

Reference URL: spring-projects/spring-boot#5842

Issue Links:

Referenced from: commits abfe3f2

1 votes, 7 watchers

Activity

  1. spring-projects-issues commented on May 4, 2016

    @spring-projects-issues
    CollaboratorAuthor
  2. spring-projects-issues commented on May 4, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Hmm, so what should we be using instead if there is no such root URL? After all, we have to provide a non-null root URL to the JPA persistence provider...

  3. spring-projects-issues commented on May 4, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Andy Wilkinson commented

    Could this be a regression of the fix for #13474?

  4. spring-projects-issues commented on May 4, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    The jar URL handling code from #13474 is still present in buildDefaultPersistenceUnitInfo... If it doesn't work in the above scenario, it is probably not a regression but rather never actually worked there.

  5. spring-projects-issues commented on May 4, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Andy Wilkinson commented

    I just came to the same conclusion. I think the difference here is that there are no entities being scanned so we never go into the matchesFilter(reader, readerFactory if block and set the persistence unit root url.

  6. spring-projects-issues commented on May 9, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Andy Wilkinson commented

    In this particular case the user is providing an orm.xml file. I'm not hugely familiar with the ordering of things while JPA is being initialized, but could the URL of the jar that contains that file be used as the root URL?

  7. spring-projects-issues commented on May 9, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Good idea...

  8. spring-projects-issues commented on May 26, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    We can resolve the persistence unit root from the orm.xml location now, if not set before. Please give it a try with a shaded jar...

  9. spring-projects-issues commented on May 27, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Andy Wilkinson commented

    Looks good, Juergen. Thanks for the fix.

  10. added
    in: data
    type: enhancement
    on Jan 11, 2019
  11. added this to the 4.3 GA milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions