如何使用ComboPooledDataSource或BasicDataSource声明连接属性

时间:2015-03-30 08:16:28

标签: java hibernate persistence.xml

如何使用Hibernate声明useFetchSizeWithLongColumn=true?在属性bean配置文件中的com.mchange.v2.c3p0.ComboPooledDataSourceorg.apache.commons.dbcp.BasicDataSource下?

我已经尝试过:

<bean id="oracle_prop" lazy-init="false"
      class="org.springframework.util.StringUtils"
      factory-method="collectionToDelimitedString">
    <constructor-arg index="0">
        <list>
            <value>oracle.jdbc.useFetchSizeWithLongColumn=true</value>
        </list>
    </constructor-arg>
    <constructor-arg value=";" index="1" type="java.lang.String"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${database.driver}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
    <property name="initialSize" value="100"/>
    <property name="maxIdle" value="100"/>
    <property name="maxActive" value="1000"/>
    <property name="connectionProperties" ref="oracle_prop"/>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="properties" ref="dataSourceProperties"></property>
    <property name="driverClass" value="${database.driver}"/>
    <property name="jdbcUrl" value="${database.url}"/>
    <property name="user" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
    <property name="initialPoolSize" value="100"/>
    <property name="minPoolSize" value="100"/>
    <property name="maxPoolSize" value="1000"/>
    <property name="maxIdleTime" value="1800"/> 
    <property name="maxStatements" value="50"/> 
</bean>

<bean id="dataSourceProperties" class="java.util.Properties">
    <constructor-arg>
        <props>
            <prop key="oracle.jdbc.useFetchSizeWithLongColumn">true</prop>
        </props>
    </constructor-arg>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${database.driver}"/>
    <property name="jdbcUrl" value="${database.url}"/>
    <property name="properties">
        <props>
            <prop key="user">${database.username}</prop>
            <prop key="password">${database.password}</prop>     
            <prop key="useFetchSizeWithLongColumn">true</prop>                  
        </props>
    </property>
    <property name="initialPoolSize" value="100"/>
    <property name="minPoolSize" value="100"/>
    <property name="maxPoolSize" value="1000"/>
    <property name="maxIdleTime" value="1800"/> 
    <property name="maxStatements" value="50"/> 
</bean>

2 个答案:

答案 0 :(得分:0)

解决方法

查找所有长数据类型并手动更改每种类型。

Select * from user_tab_columns c where c.DATA_TYPE = 'LONG';

不幸的是useFetchSizeWithLongColumn不会工作的每一次。 So if you want to use this property, make sure that the LONG columns you are retrieving are not too big or you may run out of memory.

对于JPA Hibernate,您还可以添加@Lob Annotation。

答案 1 :(得分:0)

<property name="connectionProperties" ref="oracle_prop"/>

这将无效,因为它会尝试将“object”或“property”类型对象绑定到connectionProperties,而setConnectionPropeties(String args)期望字符串参数。尝试使用值更改ref,它应该将属性注入String数据类型。否则,我们也可以传递用“;”分隔的属性。