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

I am currently updating the application from Hibernate 3.2 to Hibernate 3.3. Although I would stick to the default connection pool (Hibernate changed the default value from Commons DBCP to c3p0 ), since I have no good reason to choose a pool other than the standard one. At least, but have not used DBCP before.

The update went quite a lot, without any problems. The only thing I can not get is to pass the properties to the underlying MySQL JDBC4Connection . So far I have used DBCP BasicDataSource.addConnectionProperty (String, String) to pass properties (useUnicode = true, characterEncodin = UTF-8, characterSetResults = UTF-8, zeroDateTimeBehavior = convertToNull).

However, I cannot find a way to do the same with c3p0, except to include them in the JDBC URL . (I would like to avoid this because I want the URL to be configured without forcing users to enable these settings.)

So far, I tried to use ConnectionCustomizer without success. Any other suggestions?

+4
source share
2 answers

Once again, the question I answer myself (another student? Yes, please!):

com.mchange.v2.c3p0.ComboPooledDataSource has the property " properties ". Interestingly, setting properties after the user and password override them. But setting the pre-user and password properties works as expected.

+4
source

Watch the answer to yourself. An example of a spring setup method:

Bean data source:

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

Bean Properties:

 <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> 
+1
source

Source: https://habr.com/ru/post/1305213/


All Articles