Spring 3.1 + Hibernate 4.1 JPA, Entity manager factory is registered twice

后端 未结 2 674
我在风中等你
我在风中等你 2020-12-09 12:23

I\'m using Spring Framework 3.1 with Hibernate 4.1 as a JPA provider, and I have a fully functional setup, but every time the web app is started I see this warning message:<

相关标签:
2条回答
  • 2020-12-09 12:54

    How are you initializing your Spring Application Context? Are you using Spring MVC?

    I've seen sometimes Spring MVC XML configurations importing the other app. context XML, causing instancing twice some beans because they are declared in the application context and the web application context.

    0 讨论(0)
  • 2020-12-09 12:56

    I came across the same issue but in a different scenario. The EntityManagerFactoryRegistry produces the same HHH000436 warning, when executing multiple tests in the same run (i.e. the same JVM) started from my IDE.

    The problem can surface in case there are at least two test classes using the SpringJUnit4ClassRunner to load different Spring test application contexts each containing an EntityManagerFactory.

    The root cause is that Hibernate maintains a static registry of EntityManagerFactory instances, where the creation of the second instance may cause the collision the log message is about. So why isn't the first instance deregistered after the first test finished executing? It normally would when the app context containing that EntityManagerFactory instance gets destroyed. The reason it doesn't happen during test execution is that the Spring test context framework caches all loaded contexts in order to avoid re-loading the exact same context potentially needed by multiple tests. As a result, beans in these contexts doesn't get destroyed until after the last test finished executing, and Hibernate will just collect all the EntityManagerFactory instances ever created.

    It's really a non-issue, but if someone is really annoyed by the warning message, there are a few possible ways to avoid seeing it:

    1. Make sure the EntityManagerFactory instances get a different name (they are keyed by name in the registry). Chek the constructor of EntityManagerFactoryImpl on how the name is derived.
    2. Use @DirtiesContext on the test class to cause the SpringJUnit4ClassRunner to close the context and remove it from its context cache immediately after executing the test class.
    3. Simply set the logging level of EntityManagerFactoryRegistry to error...

    Hope this helps someone.

    0 讨论(0)
提交回复
热议问题