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

How to declare connection properties with ComboPooledDataSource or BasicDataSource

如何使用 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>

解决方法

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

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 注解。

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

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