我尝试使用 c3p0 通过 Eclipselink 将我的连接集中到 MySQL 数据库,但遇到了问题。启动 Virgo 服务器时,会创建正确数量的 c3p0 initialPoolSize 连接,但每次使用 EntityTransaction 时,都会创建更多连接 - 甚至超出 c3p0 设置的 maxPoolSize 。
显然这是一个问题,因为很快就达到了最大连接数,但对于此设置来说相对较新,我发现很难确定错误所在。我已附上我正在使用的配置文件,希望你们中的一个人可以看到我在哪里引入了错误!
用于保存对象的JPA是:
public class JpaTbRepository {
private final EntityManagerFactory emf;
public JpaTbRepository(EntityManagerFactoryBuilder builder,
Map<String, Object> properties) {
this.emf = builder.createEntityManagerFactory(properties);
}
public JpaTbRepository(EntityManagerFactory entityManagerFactory) {
this.emf = entityManagerFactory;
}
@Override
public void save(TbObject tb) {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
TbObjectEntity tbEntity = TbObjectEntity.valueOf(tb);
em.persist(tbEntity);
em.getTransaction().commit();
} finally {
em.close();
}
}
}
应用程序上下文是:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">
<!--
Activates various annotations to be detected in bean classes: Spring's
@Required and @Autowired, as well as JSR 250's @PostConstruct,
@PreDestroy and @Resource (if available) and JPA's @PersistenceContext
and @PersistenceUnit (if available).
-->
<context:annotation-config />
<context:property-placeholder properties-ref="config" ignore-unresolvable="true" system-properties-mode="NEVER" />
<osgix:cm-properties id="config" persistent-id="org.olanb" />
<bean id="databaseConfig" class="org.olanb.ConfigurationPropertyPlaceholderConfigurer">
<property name="queryPath" value="Database" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" />
</bean>
<bean id="connPoolConfig" class="org.olanb.ConfigurationPropertyPlaceholderConfigurer">
<property name="queryPath" value="Database/ConnectionPool" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" />
</bean>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://${PrimaryHost},${SecondaryHost}/${Schema}?autoReconnect=true&failOverReadOnly=false"
p:user="user"
p:password="pass"
p:initialPoolSize="5"
p:minPoolSize="5"
p:maxPoolSize="10"
p:maxStatements="20" />
<bean id="tbRepository" class="org.olanb.JpaTbRepository">
<constructor-arg ref="entityFactoryBuilder" />
<constructor-arg ref="databaseProperties" />
</bean>
<util:map id="databaseProperties" map-class="java.util.HashMap">
<entry key="javax.persistence.nonJtaDataSource" value-ref="dataSource"/>
<entry key="eclipselink.target-database" value="MySQL" />
<entry key="eclipselink.jdbc.read-connections.min" value="1" />
<entry key="eclipselink.jdbc.write-connections.min" value="1" />
<entry key="eclipselink.jdbc.batch-writing" value="JDBC" />
<entry key="eclipselink.ddl-generation" value="create-tables" />
<entry key="eclipselink.ddl-generation.output-mode" value="database" />
<entry key="eclipselink.logging.level" value="INFO" />
<entry key="eclipselink.logging.thread" value="false" />
<entry key="eclipselink.logging.session" value="false" />
<entry key="eclipselink.logging.exceptions" value="true" />
<entry key="eclipselink.logging.timestamp" value="false" />
<entry key="eclipselink.cache.shared.default" value="false" />
</util:map>
</beans>
此外,它正在 OSGi bundle 中使用,因此 OSGi 上下文 xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:bean="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<service ref="tbRepository" interface="org.olanb.api.tbRepository"/>
<service ref="catalog" interface="org.olanb.Catalog" />
<reference id="entityFactoryBuilder"
interface="org.osgi.service.jpa.EntityManagerFactoryBuilder"
filter="(osgi.unit.name=olanb.jpa)">
</reference>
最后 persistence.xml 看起来像这样:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="olanb.jpa">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.olanb.tbEntity</class>
</persistence-unit>
</persistence>
最佳答案
奇怪。您确定只创建了一个 JpaTbRepository 吗?也许您正在以某种方式创建多个创建的池和多个池。尝试添加一些调试,并在 EclipseLink 中启用最佳日志记录。
请注意,
<entry key="eclipselink.jdbc.read-connections.min" value="1" />
<entry key="eclipselink.jdbc.write-connections.min" value="1" />
在使用数据源时不应使用,应将其删除。
关于java - Eclipselink、c3p0 和 Spring - 创建太多连接!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5570641/