2

I am developing a spring boot application:

using dependencies:

    dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.4.1.RELEASE")
    compile('org.springframework.boot:spring-boot-starter-data-jpa:1.4.1.RELEASE')
    compile('mysql:mysql-connector-java:5.1.39')
}

I want to run sql on init:

SET NAMES 'utf8mb4'

to support emoji read/write

I make these configs on application.properties

spring.datasource.tomcat.init-sql=SET NAMES 'utf8mb4'
spring.datasource.init-sql=SET NAMES 'utf8mb4'
spring.datasource.connection-init-sql=SET NAMES 'utf8mb4'

But i can't make it work, and there no log for this on application startup.

kvh
  • 2,118
  • 19
  • 29

3 Answers3

8

I think i found the answer, according to this answer:

the right properties will be:

spring.datasource.tomcat.initSQL=SET NAMES 'utf8mb4'

then this will exec on connection.

Community
  • 1
  • 1
kvh
  • 2,118
  • 19
  • 29
0

Please look at the related spring-boot doc

As stated, you need to create a file called schema.sql, or schema-mysql.sql if the script is mysql specific, and put your scripts there. Also, make sure that you set spring.jpa.hibernate.ddl-auto=none.

you can read the details in the doc.

Gokhan Oner
  • 3,237
  • 19
  • 25
0

First you can create configuration bean:

@Configuration
public class JdbcConfig {
    @Bean
    @Primary
    @ConfigurationProperties("db.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
}

And then add to your application.yaml:

db:
    datasource:
        driverClassName: oracle.jdbc.driver.OracleDriver
        jdbcUrl:
        username:
        password:
        initSQL: "YOUR INIT SQL HERE"
   
Valeriy
  • 1,365
  • 3
  • 18
  • 45