gpt4 book ai didi

java - 使用连接池

转载 作者:行者123 更新时间:2023-11-30 09:02:35 24 4
gpt4 key购买 nike

我正在使用 vaadin 和类似 spring 的容器以及类似持久性框架的 eclipse 链接开发一个 web 应用程序 java。现在我想在我的应用程序中使用连接池。我在谷歌上读了一些我还很困惑的东西。这是我的应用程序配置:

我的 Spring 背景

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

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

更新1

我正在尝试遵循 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

最佳答案

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

你所做的是配置EclipseLink自带的连接池,而不是使用像Tomcat Connection Pool这样的专用连接池。或 HikariCP .

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

更新

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

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

关于java - 使用连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com