使用连接池

时间:2014-09-18 13:50:51

标签: java spring jpa eclipselink connection-pooling

我正在开发一个带有vaadin的web-app java和像容器一样的弹簧和像持久性框架一样的eclipse链接。现在我想在我的应用程序中使用连接池。我在google上读了一些东西,但是我已经加入了。这是我的应用程序配置:

我的春天背景

        <?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>

我的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>

这是配置池连接的正确方法吗? 如何检查我的池连接是否正确配置并正常工作?

UPDATE1

我试图遵循xxxx的建议,并且我以这种方式更改了我的应用程序上下文:

<?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>

和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>

但我得到以下例外:

    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

2 个答案:

答案 0 :(得分:1)

您正在执行的操作不是配置连接池的最佳做法。

您正在做的是配置EclipseLink附带的连接池,而不是使用Tomcat Connection PoolHikariCP等专用连接池。

使用Spring数据源和连接池设置初始配置非常容易(请参阅thisthisthisthis和{{3}在众多文章中存在)

<强>更新

使用您更新的配置,您需要更改

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

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

答案 1 :(得分:1)

在查看错误时,我发现了导致问题的异常

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

当我们不添加此元素标记

时会发生这种情况
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

在我们的applicationContext.xml文件中 或者你只能添加

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

PersistenceAnnotationBeanPostProcessor内部由<context:component-scan />标记元素激活。

然后你需要更新

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

使用

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

希望这对你有用。