Home » Java » Yet another Mysql database encoding chars on windows versus linux

Yet another Mysql database encoding chars on windows versus linux

Posted by: admin July 12, 2018 Leave a comment

Questions:

The general problem is I have two mysql5 db’s. One is on windows machine, where I’m developing, the other is on remote linux (ubuntu) server.

I’m building a fat jar with spring/hibernate.

On local machine and local mysql db everything is fine. I parse some web page using jsoup and store my results in database, and data is stored with polish encoding for example:
“sprzedaż Warszawa, Żoliborz, Potocka”

The same on linux server is:
“sprzeda? Warszawa, ?oliborz, Potocka”

Both databases have the same collation : utf8_unicode_ci

Columns are varchar.

When I run below sql on both databases:

SELECT DISTINCT C.collation_name, T.table_name
FROM information_schema.tables AS T,
information_schema.collation_character_set_applicability AS C
WHERE C.collation_name = T.table_collation
AND T.table_schema = DATABASE();

I get the same result : that collation is utf8_unicode_ci.

The db string is as follows:
jdbc:mysql://localhost/dbname?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf-8

Hibernate configuration as below:

@Bean
public SessionFactory sessionFactory() throws Exception {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource());
    sessionFactoryBean.setPackagesToScan(new String[] { "x.domain" });
    Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.show_sql", false);
    hibernateProperties.put("hibernate.bytecode.use_reflection_optimizer", false);
    hibernateProperties.put("hibernate.check_nullability", false);
    hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
    hibernateProperties.put("hibernate.search.autoregister_listeners", false);        
    hibernateProperties.put("hibernate.connection.CharSet", "utf8");
    hibernateProperties.put("hibernate.connection.characterEncoding", "utf8");
    hibernateProperties.put("hibernate.connection.useUnicode", true);
    sessionFactoryBean.setHibernateProperties(hibernateProperties);
    sessionFactoryBean.afterPropertiesSet();

    return sessionFactoryBean.getObject();
}

I’ve spent hours trying to find a solution for this, unfortunately with no luck. I’ve changed collation to utf8_bin, changed column collation, db strings, hibernate config and nothing. Still the result is getting “?” instead of polish char on linux server. I would appreciate some advice. Thanks for reading.

Answers: