简体   繁体   中英

Scala/Slick JDBC encoding configuration with typesafe config

I am building a Scala application using Slick where I load my MySQLDriverbackend with val db = Database.forConfig("mysql.dev") and where my "application.conf" file looks like

mysql = {
dev = {
    dataSourceClass = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
    properties = {
      databaseName = "test"  
      user = "user"
      password = "password"
      serverName = "localhost"
      portNumber="3306"
    }
  }
}

I am successfully saving data to my database which uses a default utf-8 encoding. However when trying to save the character 泗 I ended in my database with the dreaded ? instead of input character.

I think I failed to declare that my connection needs to use UTF-8, however I couldn't find the option in the documentation of forConfig .

Do you know how I can fix that? Thanks.

For MySQL, adding parameters useUnicode=true&characterEncoding=UTF-8 in your connection URL should solve the issue ie.

jdbc:mysql://<host>:<port>/<database>?useUnicode=true&characterEncoding=UTF-8

You could use this link to configure url.

An alternative to Sudhir's answer that keeps the same file structure is to set properties to:

properties = {
      databaseName = "test"  
      user = "user"
      password = "password"
      serverName = "localhost"
      portNumber="3306"
      characterEncoding="utf8"
      useUnicode=true
    }

utf8mp4 emoji issue

for spring-boot

Insert emoji does not work with spring-boot and MariaDB

spring.datasource.hikari.connection-init-sql=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

http://slick.lightbend.com/doc/3.2.0/config.html

Because slick supply HikariCP pool,I think hikari pool may solve this issue then try

mysql_orginal = {
  connectionPool = "HikariCP"
  driver = "com.mysql.jdbc.Driver"
  connectionInitSql ="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"
  url = "jdbc:mysql://127.0.0.1/table?useUnicode=true&characterEncoding=UTF-8"
  keepAliveConnection = true
  user = "root"
  password = "123456"
  numThreads = 4
  poolName="mysql_orginal_pool"
}

val ORIGIN_DB = Database.forConfig("mysql_orginal")

libraryDependencies += "com.typesafe.slick" %% "slick-hikaricp" % "3.2.0"

it works

demo: https://github.com/cclient/ScalaMysqlSync

ref:

http://slick.lightbend.com/doc/3.2.0/database.html#databaseconfig

https://github.com/slick/slick/blob/master/slick-hikaricp/src/main/scala/slick/jdbc/hikaricp/HikariCPJdbcDataSource.scala

https://github.com/slick/slick/blob/4674b977d9eeaf825d57808d48dbbfc37e47858c/doc/src/config.md#monitoring-indexmonitoring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM