Eclipselink,c3p0和Spring - 创建太多连接!

时间:2011-04-06 17:39:49

标签: java spring jpa eclipselink c3p0

我正在尝试使用 c3p0 将我的连接汇集到带有 Eclipselink 的MySQL数据库,但我遇到了问题。在启动 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&amp;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>

2 个答案:

答案 0 :(得分:2)

奇。您确定只创建了一个JpaTbRepository吗?也许你正在以某种方式创建多个创建的多个池。尝试添加一些调试,并在EclipseLink中启用最好的日志记录。

请注意,

<entry key="eclipselink.jdbc.read-connections.min" value="1" />
<entry key="eclipselink.jdbc.write-connections.min" value="1" />

在使用DataSource时不应该使用,应该删除。

答案 1 :(得分:1)

感谢James的回复和信息。您的初始回复非常准确!这是一个奇怪的。我设法将问题隔离到c3p0 JDBC MySQL URL。我发现使用辅助主机连接池不起作用,但是删除了辅助主机,它确实!

进一步调查我发现这个区域有一个错误,我使用的是Connector / J驱动程序版本(5.1.13)所以我更新了(到5.1.15)并且它与辅助主机一起工作!

修复错误的更新日志:http://dev.mysql.com/doc/refman/5.1/en/cj-news-5-1-14.html

结论:通过将Connector / J更新为v5.1.15

来解决