By d123

2013-01-10 18:47:42 8 Comments

I am connecting to MySQL over SSL from my Java application. I have configured MYSQL to support SSL and generated client certificates. I have imported server CA certificate and client certificate into keystore. This is how my code currently looks like

    String url = "jdbc:mysql:// verifyServerCertificate =true&useSSL=true&requireSSL=true"


    con = DriverManager.getConnection(url, user, password);

I want to use spring with C3p0 to connect to MYSQL over SSL.This is my spring configuration file which reads parameters from

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>

How can I configure spring to set properties verifyServerCertificate =true

Also is it possible to set keyStore and trustStore values in spring config file.


@Mark Norman 2019-01-04 03:41:49

You can configure the useSSl, requireSSL, and verifyServerCertificate properties of a DataSource by using Java based configuration. The addDataSourceProperty method of the DataSource class gives you the ability, as shown in the below code snippet (you can replace HikariDataSource with a C3p0 instance)

MySQL Connector/J exposes configuration properties for key stores (e.g. trustCertificateKeyStoreUrl), so I assume that addDataSourceProperty can be used for these properties too.

I do not know if the XML configuration schema provides a tag that corresponds to addDataSourceProperty.

public DataSource createPslDataSource(final MyDataSourceProperties myDataSourceProperties) {

    HikariDataSource dataSource = new HikariDataSource();

    dataSource.addDataSourceProperty("useSSL", true);
    dataSource.addDataSourceProperty("requireSSL", true);
    dataSource.addDataSourceProperty("verifyServerCertificate", true);


    return dataSource;

@gavenkoa 2018-08-16 14:16:17

It is not necessary to pass keyStore and trustStore to java program or set any system properties as it can be achieved via connection properties per connection!

So you can use different certificated for different connections (and applications if you are in app server).

Original answer: Relevant part:


It is documented:

@Marcel Stör 2013-01-10 22:04:04

The value for jdbc.url in has to be


Those parameters must be added directly to the URL for MySQL. The parameters for keyStore and trustStore should be passed to the JVM at start like so:

You can use Spring to set system properties but I'd never use it, it's too cumbersome.

Related Questions

Sponsored Content

4 Answered Questions

29 Answered Questions

13 Answered Questions

2 Answered Questions

I cannot connect to MySQL database

  • 2017-08-29 09:16:19
  • Karen
  • 1501 View
  • 0 Score
  • 2 Answer
  • Tags:   mysql jdbc

2 Answered Questions

[SOLVED] Java: SSL Connection to MySQL

2 Answered Questions

[SOLVED] Kettle, JDBC, MySQL, SSL: Could not Connetct to database

1 Answered Questions

Sponsored Content