0

Developing web application I'd like to use an embedded database to store data. However instead of proposed in-memory databases I`d like to prefer to use custom one. My choice is MariaDB. I have been ruled with this article how to get MariaDB as embedded database in my project, everything is fine, database appears, except one thing: I cannot change an encoding for my tables values.

I tried to set additional application properties in my application.yml file, tried to complete connection string with parameters of specifying encoding, but all those stuff didn't work unfortunately :( May any of you have ever faced with such problem and can help me? Thank you in advance!

My current application.yml is following:

mariaDB4j:
  dataDir: ./localMariaDB
  port: 3307
  databaseName: embeddedDB
spring:
  datasource:
    url: jdbc:mariaDB://localhost:3307/
    username: root
    password:
    driver-class-name: org.mariadb.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    hibernate:
      ddl-auto: create

And finally I get exception bellow:

Caused by: java.sql.SQLException: Incorrect string value: '\xD0\x92 \xD1\x80\xD0...' for column 'current_status' at row 1
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readErrorPacket(AbstractQueryProtocol.java:1688) ~[mariadb-java-client-2.6.2.jar:na]
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readPacket(AbstractQueryProtocol.java:1550) ~[mariadb-java-client-2.6.2.jar:na]
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.getResult(AbstractQueryProtocol.java:1513) ~[mariadb-java-client-2.6.2.jar:na]
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQuery(AbstractQueryProtocol.java:318) ~[mariadb-java-client-2.6.2.jar:na]
... 121 common frames omitted

By the way, when I run the application, the connection string which I get looks like jdbc:mysql://localhost:3307/embeddedDB. I am confused why there is :mysql: instead of :mariadb: as specified in my connection properties. Does it have an influence on my database behavior?

  • MariaDB is a MySQL fork. It's possible that the "mysql" constant has stuck around in some places (intentionally or not). – Joachim Sauer Sep 01 '20 at 07:42
  • @JoachimSauer, so it is not a big deal that such changes appear and they don`t influence for database, right? – Aleksey Dobroshtan Sep 01 '20 at 07:48
  • I don't think it's an issue, but switching to the [Hibernate MariadDB dialect](https://stackoverflow.com/questions/37066024/what-is-the-mariadb-dialect-class-name-for-hibernate) might be a good idea. – Joachim Sauer Sep 01 '20 at 07:57
  • @JoachimSauer your advice may be good idea, but not a question resolution( nothing happened.. – Aleksey Dobroshtan Sep 01 '20 at 08:04

1 Answers1

1

So, I have found the resolution to my issue. It appeared as usual very simple :) To change standard character encoding, which MariaDB proposes, I had to specify additional configuration properties before setting up the MariaDB's DataSource. As this source provides to configure MariaDBSpringService entity to further use in DataSourceconfiguration, it is necessary to extend it (MariaDBSpringService) with some character encoding description utils. So, to specify custom encoding (i.e. to change standard one) I've just added the following lines of code to MariaDBSpringService bean:

service.getConfiguration().addArg("--character-set-server=utf8mb4");
service.getConfiguration().addArg("--collation-server=utf8mb4_general_ci");

In general, the full bean declaration is below:

@Bean
public MariaDB4jSpringService mariaDB4jSpringService(){
    MariaDB4jSpringService service = new MariaDB4jSpringService();
    service.getConfiguration().addArg("--character-set-server=utf8mb4");
    service.getConfiguration().addArg("--collation-server=utf8mb4_general_ci");
    return service;
}

Hope, this will help somebody, who further can possibly face with such issue)) Thanks to everybody, who tried to help me!