Announcement Announcement Module
Collapse
No announcement yet.
Lazy Initialization of LocalContainerEntityManagerFactoryBean. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy Initialization of LocalContainerEntityManagerFactoryBean.

    Hi

    I am trying to do lazy initialization of LocalContainerEntityManagerFactoryBean, but it fails. In my unit test case, I have removed all reference to EntityManagerFactory and EntityManager, yet I can see in the logs, LocalContainerEntityManagerFactoryBean is being instantiated. Below is my test context.xml file.

    Regards
    Jay Gaba

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schem...ng-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd http://ehcache-spring-annotations.go...ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
    <contextroperty-placeholder location="classpath*:test*.properties"/>
    <context:spring-configured/>
    <context:component-scan base-package="com.xyz"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean" lazy-init="true">
    <property name="jpaVendorAdapter">
    <bean class="com.xyz.spring.CustomJpaVendorAdapter" lazy-init="true"/>
    </property>
    </bean>
    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" lazy-init="true"/>
    <tx:annotation-driven />
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager" lazy-init="true">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    </beans>

  • #2
    Hello

    use code tags when you are inserting code to the post

    I am trying to do lazy initialization of LocalContainerEntityManagerFactoryBean, but it fails.
    I assume you are receiving an error, therefore post the complete error stack trace, to have a clear idea

    In my unit test case, I have removed all reference to EntityManagerFactory and EntityManager, yet I can see in the logs, LocalContainerEntityManagerFactoryBean is being instantiated. Below is my test context.xml file.
    Post your Test class

    Comment


    • #3
      Are you trying to test the "lazy-init" method of this bean?

      Comment


      • #4
        Hi

        my implementaiton of javax.sql.datasouce derives from
        Code:
        org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
        which looks up actual datasource with key that is available when user's request arrives.

        But I can see DataSource.getConnection called while deploying war file. That makes me think about getting entityManagerFactory lazy-initialized. Right now entityManagerFactory is declared like below.

        Code:
        <bean id = "dataSourceFactory" class = "com.my.datasource.DataSourceFactory" lazy-init="true">
                <constructor-arg ref="registry"/>
                <constructor-arg ref="zooKeeperFacade"/>
            </bean>
        
            <bean id="dataSource" factory-bean="dataSourceFactory" factory-method="create" lazy-init="true" />
        
        	<bean id="queryOptimizer" class="com.my.hibernate.QueryOptimizer" />
        	
        	<bean id="entityManagerFactory"
        		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" lazy-init="true">
        		<property name="dataSource" ref="dataSource" />
        		<property name="jpaVendorAdapter">
        			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        				<property name="showSql">
                            <bean factory-bean="registry" factory-method="getLogShowSql"/>
                        </property>
        				<property name="generateDdl" value="false" />
        				<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
        			</bean>
        		</property>
        		<property name="jpaPropertyMap" ref="hibernatePropertyMap"/>
        	</bean>
        Where should I look to find out how to change the startup behavior e.g. not calling datasource.getConnection unless user's request arrives ?
        Last edited by uastan; May 30th, 2013, 07:20 AM.

        Comment

        Working...
        X