2011-04-06 82 views
2

我試圖使用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&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> 

回答

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

結論:通過更新解決了連接器/ J到v5.1.15

2

奇數。你確定只有一個JpaTbRepository被創建?也許你正在創建多個創建和多個池創建。嘗試添加一些調試,並在EclipseLink中啓用最好的日誌記錄。

。注意,

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

不應該使用一個數據源時,應去除使用,。

+0

感謝詹姆斯,你的信息幫我診斷的問題! – olan 2011-04-12 22:02:17