0

I am using Hibernate 5.3.7.Final as persistence provider on Weblogic 12.2.1.3.

My problem is, that when I deploy my application which contains the persistence unit, the entity manager factory is initialized twice by Weblogic.

First in the application prepare phase and then again in the application activate phase. As a result my whole DB schema is initialized twice (hibernate.hbm2ddl.auto = create).

I have attached the relevant parts of method call chains of both phase.

First one (prepare phase):

HibernatePersistenceProvider.createContainerEntityManagerFactory(PersistenceUnitInfo, Map) line: 141    
BasePersistenceUnitInfo.initializeEntityManagerFactory(boolean, BeanManager) line: 614  
BasePersistenceUnitInfo.init() line: 202    
BaseJPAIntegrationProvider.createPersistenceUnitInfo(PersistenceUnitBean, Object, GenericClassLoader, String, URL, URL, String, ApplicationContextInternal) line: 54    
ModulePersistenceUnitRegistry(AbstractPersistenceUnitRegistry).storeDescriptors(Map, Map) line: 422 
ModulePersistenceUnitRegistry(AbstractPersistenceUnitRegistry).loadPersistenceDescriptors(boolean) line: 128    
ModulePersistenceUnitRegistry.<init>(GenericClassLoader, ApplicationContextInternal, Module, boolean) line: 56  
WebAppInternalModuleExtension$PersistenceExtension.setupPersistenceUnitRegistry() line: 197 
WebAppInternalModuleExtension$PersistenceExtension.access$300(WebAppInternalModuleExtension$PersistenceExtension) line: 118 
WebAppInternalModuleExtension.prePrepare() line: 56 
ExtensibleModuleWrapper$PrepareStateChange.next(ExtensibleModuleWrapper$DrivenObject) line: 293 
ExtensibleModuleWrapper$PrepareStateChange.next(Object) line: 285   
StateMachineDriver<StateMachine>.nextState(StateChange<StateMachine>, StateMachine[]) line: 45  
ExtensibleModuleWrapper.prepare() line: 109

Second one (activate phase):

HibernatePersistenceProvider.createContainerEntityManagerFactory(PersistenceUnitInfo, Map) line: 141    
BasePersistenceUnitInfo.initializeEntityManagerFactory(boolean, BeanManager) line: 614  
BasePersistenceUnitInfo.reInitEntityManagerFactoryPerhaps() line: 785   
BasePersistenceUnitInfo.activate(Context) line: 750 
WebAppInternalModuleExtension$PersistenceExtension.activatePersistenceUnit() line: 160  
WebAppInternalModuleExtension$PersistenceExtension.access$700(WebAppInternalModuleExtension$PersistenceExtension) line: 118 
WebAppInternalModuleExtension.postActivate() line: 81   
ExtensibleModuleWrapper$ActivateStateChange.next(ExtensibleModuleWrapper$DrivenObject) line: 321    
ExtensibleModuleWrapper$ActivateStateChange.next(Object) line: 313  
StateMachineDriver<StateMachine>.nextState(StateChange<StateMachine>, StateMachine[]) line: 45  
ExtensibleModuleWrapper.activate() line: 121

I think Weblogic makes his decision in method

BasePersistenceUnitInfo.reInitEntityManagerFactoryPerhaps()

whether to reinitialize the entity manager factory again upon persistence unit activation.

I have searched a lot, but have not found any settings so far to somehow fine tune this bootstrapping behaviour.

Does anybody have an idea how to avoid the duplicate DB schema initialization?

(I have a rather large schema and import.sql so the process takes a lot of time.)

Here is my persistence.xml for reference:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="abPersistenceUnit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>jdbc/abDS</jta-data-source>

        <jar-file>ab.jar</jar-file>
        <jar-file>lib/cd.jar</jar-file>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.default_schema" value="${hibernate.default_schema}" />
            <property name="hibernate.cache.second_level_cache" value="false" />
            <property name="hibernate.jdbc.batch_size" value="50" />
            <property name="hibernate.order_inserts" value="true" />
            <property name="hibernate.order_updates" value="true" />
            <property name="hibernate.jdbc.batch_versioned_data" value="true" />
            <property name="hibernate.search.autoregister_listeners" value="false" />
            <property name="hibernate.current_session_context_class" value="jta" />
            <property name="hibernate.transaction.coordinator_class" value="jta" />
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform" />
            <property name="hibernate.id.new_generator_mappings" value="true" />
        </properties>
    </persistence-unit>
</persistence>

I really appreciate any thoughts or comments!

1 Answers1

0

Encountered the exact same problem. The only recommendation i can think of is to not use CDI in the module as the code seems to suggest that it will only reinitialize the persistence unit if the module is CDI enabled.

s

 public void reInitEntityManagerFactoryPerhaps() throws EnvironmentException {
        BeanManager bm = null;
        if (this.appCtx != null) {
            bm = this.getBeanManager(this.getCDIArchiveID(this.persistenceArchiveId));
        }

        if (this.isDataSourceReInitializationNeeded()) {
            this.close();
            this.initializeEntityManagerFactory(true, bm);
        } else if (!this.isKodoPersistenceUnit() && bm != null) {
            this.close();
            this.initializeEntityManagerFactory(true, bm);
        }

    }

I set a breakpoint in the wlfullclient and did some remote debugging. This code is from EJBModule inside weblogic.

  • Thank you for your reply! Could you tell me, from where have you managed to obtain the Weblogic related (BasePersistenceUnitInfo.java, etc.) source files? I would also check them. – Gergely Tóth Dec 09 '19 at 10:20