开发者

Eclipselink, c3p0 and Spring - creating too many connections!

I'm trying to use c3p0 to pool my connections to a MySQL DB with Eclipselink, but I've encountered a problem. On starting the Virgo server, the correct number of c3p0 initialPoolSize connections are created, but everytime an EntityTransaction is used a further connection is created - even up and beyond the c3p0 set maxPoolSize.

Obviously this is a issue as the max connections is quickly met, but being relatively new to this setup I'm finding it difficult to identify where the error is.. I've attached the configuration files I am using, hoping that one of you can see where I'm introducing an error!

The JPA used to save an object is:

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();
        }
    }
}

The application context is then:

<?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"/>
开发者_Go百科        <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>

Also, this is being used in an OSGi bundle so the OSGi context xml is:

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

And finally the persistence.xml looks something like:

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


Odd. Are you sure that only one of your JpaTbRepository is being created? Maybe you are getting multiple created and multiple pools created somehow. Try adding some debug, and enable logging on finest in EclipseLink.

Note that the,

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

should not be used when using a DataSource, and should be removed.


Thanks James for the reply and info. Your initial response was pretty accurate! This was an odd one. I managed to isolate the problem to the c3p0 JDBC MySQL URL. I found that with the secondary host the connection pooling didn't work, but with the secondary host removed it did!

Looking into it further I found there was a bug in this area with the Connector/J driver version I was using (5.1.13) so I updated (to 5.1.15) and it worked with the secondary host!

Changelog which fixes the bug: http://dev.mysql.com/doc/refman/5.1/en/cj-news-5-1-14.html

Conclusion: Solved by updating Connector/J to v5.1.15

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜