开发者

How to configure MySQL connection properties with Spring, Hibernate 3.3 and c3p0?

I am currently in the process of upgrading an application from Hibernate 3.2 to Hibernate 3.3. I though I'd stick with the default connection pool (Hibernate changed its default from Commons DBCP to c3p0) as I don't have any good reason to choose a non-default pool. At least non but having used DBCP before.

The upgrade went pretty much without any problems so far. The only thing I can't get to work is passing properties to the underlying MySQL JDBC4Connection. Up to now, I used DBCP's BasicDataSource.addConnectionProperty(String,String) to pass properties (useUnicode=true, characterEncodin=UTF-8, characterSetResults=UTF-8, zeroDateTimeBehavior=convertToNull).

However, I can't find any way to do the same with c3p0 other than including them in the JDBC URL. (That's something I'd like to avoid as I wanna keep the URL configurable without forcing users to include those parameters.)

So开发者_开发问答 far, I've tried to use a ConnectionCustomizer without success. Any other suggestions?


Once again a question I answer myself (another self-learner? yes, please!):

com.mchange.v2.c3p0.ComboPooledDataSource has a property "properties". Interestingly, setting properties after user and password overrides them. But setting properties before user and password works as expected.


Follow up for the self answer. An example of a spring way of configuring this:

The Data source bean:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="properties" ref="mysqlConnectionProperties"></property>
    <property name="driverClass" value="${jdbc.driver}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <!-- c3p0 combo pooled data source settings -->
    <property name="initialPoolSize" value="3" />
    <property name="minPoolSize" value="3" />
    <property name="maxPoolSize" value="50" />
    <property name="maxIdleTime" value="7200" />
    <property name="maxStatements" value="200" />
    <property name="idleConnectionTestPeriod" value="270" />
    <property name="preferredTestQuery">
        <value>SELECT 1</value>
    </property>
</bean>

The properties bean:

<bean id="mysqlConnectionProperties" class="java.util.Properties">
    <constructor-arg>
        <props>
            <prop key="useTimezone">true</prop>
            <prop key="serverTimezone">America/Chicago</prop>
                <!-- add any other properties you have -->
        </props>
    </constructor-arg>
</bean>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜