2

I'm developing a web-app java with vaadin and spring like container and eclipse link like persistence framework. Now I want to use an connection pool inside my application. I read something on google my I counfused yet. This is my application configuration:

My spring context

        <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd" default-autowire="byName">


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" 
        p:entityManagerFactory-ref="entityManagerFactory">
        <property name="persistenceUnitName" value="MyUnit" />
    </bean>

    <bean id="eclipseLinkJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:persistenceUnitName="MyUnit"
        p:jpaVendorAdapter-ref="eclipseLinkJpaVendorAdapter">
        <property name="jpaPropertyMap">
            <map>
                <entry key="eclipselink.cache.shared.default" value="false" />
                <entry key="eclipselink.weaving" value="false" />
            </map>
        </property>
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <tx:annotation-driven />
    <context:annotation-config/>
    <context:component-scan base-package="it.myPackage"/>

    <aop:aspectj-autoproxy /> 


</beans>

My persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0">
    <persistence-unit name="MyUnit">

        <properties>
         <property name="javax.persistence.jdbc.driver" value="${db.driverClass}" />
         <property name="javax.persistence.jdbc.url" value="${db.connectionURL}" />
         <property name="javax.persistence.jdbc.user" value="${db.username}" />
         <property name="javax.persistence.jdbc.password" value="${db.password}" />

         <!-- Used to configuring connection pool -->
         <property name="eclipselink.connection-pool.default.initial" value="10" /> 
         <property name="eclipselink.connection-pool.node2.min" value="10"/> 
         <property name="eclipselink.connection-pool.node2.max" value="20"/> 
         <property name="eclipselink.connection-pool.node2.url" value="${db.connectionURL}"/> 

        </properties>
        <shared-cache-mode>NONE</shared-cache-mode>
    </persistence-unit>
</persistence>

Is this the correct way to configure an pool connection? How can I check that my pool connection is correctly configured and work?

Update1

I'm trying to follow the suggestion of xxxx and the I changed my application context in this way:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd" default-autowire="byName">


    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
        <property name="driverClassName" value="myDriver" />
        <property name="url" value="myUrl" />
        <property name="username" value="myUsername" />
        <property name="password" value="myUsername" />
        <property name="initialSize" value="5" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
        <property name="minIdle" value="2" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="eclipseLinkJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="dataSource" 
        p:persistenceUnitName="myUnit"
        p:jpaVendorAdapter-ref="eclipseLinkJpaVendorAdapter">
        <property name="jpaPropertyMap">
            <map>
                <entry key="eclipselink.weaving" value="false" />
            </map>
        </property>
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <tx:annotation-driven />

</beans>

and the persistence.xml:

  <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0">
    <persistence-unit name="MyUnit">

         //List of classes 


        <shared-cache-mode>NONE</shared-cache-mode>
    </persistence-unit>
</persistence>

but i get the following exception:

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyBean': FactoryBean threw exception on object creation; nested exception is javax.persistence.PersistenceException: java.lang.StackOverflowError
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:872)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:814)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:731)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
    ... 56 more
Caused by: javax.persistence.PersistenceException: java.lang.StackOverflowError
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:766)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getMetamodel(EntityManagerFactoryDelegate.java:637)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:548)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:376)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:517)
    at com.sun.proxy.$Proxy20.getMetamodel(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:176)
    at com.sun.proxy.$Proxy29.getMetamodel(Unknown Source)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:60)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:149)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:87)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:70)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:137)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 64 more
Caused by: java.lang.StackOverflowError
Skizzo
  • 2,883
  • 8
  • 52
  • 99
  • Error report says "Error creating bean with name 'MyBean':" but i dont see any mean configured with name 'MyBean'. you didnt add the code which is causing the problem. – varun Sep 24 '14 at 11:53

2 Answers2

1

What you are doing is not a best practice for configuring a connection pool.

What you are doing is configuring the connection pool that comes with EclipseLink, instead of using a dedicated connection pool like Tomcat Connection Pool or HikariCP.

It's very easy to setup an initial configuration with a Spring data source and a connection pool (see this, this, this, this and this among the many articles that exist)

Update

Using your updated configuration you need change

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="dataSource" ref="dataSource"/>
</bean>

to

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190
1

While going through your errors I found this exception which was causing the problem

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)

Which actually happen when we do not add this element tag

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

in our applicationContext.xml file or you can add only

<context:component-scan base-package="your.base.package" />

The PersistenceAnnotationBeanPostProcessor internally activated by the <context:component-scan /> tag element.

Then you need to update

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="dataSource" ref="dataSource"/>
</bean>

With

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

Hope this will work for you.

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
  • Why, Do I have change *dataSource* with *entityManagerFactory*, can you explain to me the difference? – Skizzo Sep 26 '14 at 09:07
  • Spring JPA allows a configured JpaTransactionManager to expose a JPA transaction to JDBC access code that accesses the same JDBC dataSource. Please refer this link http://stackoverflow.com/questions/2421339/how-to-inject-jpa-entitymanager-using-spring – Qadir Hussain Sep 26 '14 at 10:50