当前位置: 首页 > 知识库问答 >
问题:

使用连接池

张可人
2023-03-14

我正在用vaadin和spring开发一个Web应用程序java,比如容器和eclipse链接,比如持久化框架。现在我想在我的应用程序中使用一个连接池。我在谷歌上读了一些我还没读到的东西。这是我的应用程序配置:

我的春日文脉

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

共有1个答案

谭嘉容
2023-03-14

您所做的不是配置连接池的最佳实践。

您所做的是配置EclipseLink附带的连接池,而不是使用Tomcat连接池或Hikaricp之类的专用连接池。

使用Spring数据源和连接池设置初始配置非常容易(请参阅现有的许多文章中的this,this,this,this,this,this,this,this,this,this,this,this)

<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>
 类似资料:
  • 本文向大家介绍sqlalchemy 使用连接,包括了sqlalchemy 使用连接的使用技巧和注意事项,需要的朋友参考一下 示例 您可以使用上下文管理器打开连接(即从池中请求一个连接): 有无,但必须手动将其关闭:            

  • Tomcat在使用后不释放连接的原因可能是什么? 这是我的配置

  • 我正在使用Node js和Postgresql,并试图在连接实现中达到最高效率。< br >我看到pg-promise构建在node-postgres之上,node-postgres使用pg-pool来管理池。< br >我还读到“一次超过100个客户端是一件非常糟糕的事情”(node-postgres)。 我正在使用pg-promise,想知道: 对于非常大的数据负载,推荐的poolSize是什

  • 我第一次使用带有IISExpress的新Visual Studio 2013(以前 ASP.net VS2010上的开发服务器使用)。我在尝试调试项目时遇到问题。 这是我在Chrome看到的: 无法与服务器建立安全连接。这可能是服务器的问题,也可能需要您没有的客户端身份验证证书。错误代码:ERR_SSL_PROTOCOL_ERROR 我更新了我的计划—— 与localhost的连接中断。错误代码:

  • 我正在尝试连接到Gmail帐户(通过OAuth 2.0上的ruby Gmail gem、Gmail gem、Gmail_xoauth gem,但每个方法都使用imap.rb),并获得: c:/ruby 21-x64/lib/ruby/2 . 1 . 0/net/IMAP . Rb:1045:在“initialize”中:由于目标计算机主动拒绝连接,因此无法建立连接。-连接(2)用于“imap.gm

  • 另一个宽大处理--B: 这里要注意,实体A和B之间没有隐式关系,B表中的a_id是手工处理的,因此--关系类似于一对多(A-->B),但不是jpa-hibernate关系。而且,我的FullDto有A的所有属性和B的列表: 现在,我想从A的存储库接口中提取所有内容(A和B表一次完成),如下所示: