2

I uploaded my web application to CloudFoundry, but my native language was broken as below.

enter image description here

I have checked that the DB has broken data.

DB uses ClearDB MySQL Database, which CloudFoundry provides as a service.

I ran the following query.

SHOW VARIABLES LIKE 'c%'

character_set_client        utf8
character_set_connection    utf8
character_set_database      utf8
character_set_filesystem    binary
character_set_results       utf8
character_set_server        latin1
character_set_system        utf8
character_sets_dir          /usr/share/mysql/charsets/
collation_connection        utf8_general_ci
collation_database          utf8_general_ci
collation_server            latin1_swedish_ci
completion_type             NO_CHAIN
concurrent_insert           AUTO
connect_timeout             10

I suspect that the part that is 'latin' is guessing, but I do not know how to change that.

Is there someone you can help with this problem?

Thank you.

khhan
  • 21
  • 4
  • Did you try to set the character_set_server option on your ClearDB MySQL service instance? e.g. like it is described in http://stackoverflow.com/a/6995056/281829 ? – Anatoly Kern Mar 25 '17 at 10:52
  • Thank you for your reply. If character_set_server is applied, utf8 changes for a while, but after a few minutes it becomes latin again. The value entered in the DB also does not change. – khhan Mar 25 '17 at 12:03
  • 1
    It is a likely a problem with ClearDB MySQL configuration, which should be set by default to utf8, so contacting ClearDB support on that would be the best approach. Can you use/test it on MySQL for PCF for example? – Anatoly Kern Mar 25 '17 at 13:00

1 Answers1

2

Depending on the particular ClearDB database server, there will be cases when your database's characters set is latin1 (the default configuration for MySQL older than 8.1).

We have contacted the ClearDB support team, but they were not willing to change this configuration because it could affect other customers hosted on the same server and they offered us a more expensive solution: to have our own dedicated servers.

The good point is that the character set can be configured per connection bases, so we decided to override the default DataSource, autoconfigured by the Java buildpack used by the Cloud Foundry.

In order to override the DataSource, we needed to do several things:

  1. Add Maven dependency for Spring Cloud connectors,
  2. Turn-off the default autoconfiguration of the DataSource bean,
  3. Configure the DataSource bean by using connection parameters provided by the environment.

To add Spring Cloud connectors, add this dependency to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cloud-connectors</artifactId>
</dependency>

In order to turn off the auto-configuration, be sure to create Cloud bean in spring-boot cloud profile:

@Configuration
@Profile("cloud")
public class DataSourceConfiguration {

    @Bean
    public Cloud cloud() {
        return new CloudFactory().getCloud();
    }
}

Finally, to use database connection parameters hosted within VCAP_SERVICES environment variables, you can reference them in the application-cloud.yml in this way:

spring.datasource.url: ${cloud.services.mysql.connection.jdbcurl}&characterEncoding=utf-8
spring.datasource.username: ${cloud.services.mysql.connection.username}
spring.datasource.password: ${cloud.services.mysql.connection.password}
spring.datasource.testOnBorrow: true

When it comes to customizing DataSource configuration, it is important to pay attention to spring.datasource.url that ends with characterEncoding=utf-8: this is where connections are configured to use the UTF-8 encoding.

Also, mysql is the name of the PCF service instance in our case and can be different in your environment.

Finally, to learn more about binding to data services with Spring Boot, you can read this excellent article: https://spring.io/blog/2015/04/27/binding-to-data-services-with-spring-boot-in-cloud-foundry.