我试图使用c3p0将我的连接集中到一个MySQL数据库Eclipselink,但我遇到了一个问题。在启动服务器Virgo服务器时,会创建正确数量的c3p0 initialPoolSize连接,但每次使用EntityTransaction时,都会创建一个进一步的连接 - 甚至超出c3p0 set 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包被使用,以便在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>
感谢詹姆斯,你的信息帮我诊断的问题! – olan 2011-04-12 22:02:17