1

I am trying to initialize data in in-memory database H2 when application starts up.

I have a big sql file. Everything is read fine until it reaches delimiter statement:

delimiter $
CREATE TRIGGER ins_sum BEFORE INSERT ON account
   FOR EACH ROW BEGIN INSERT INTO mytable(name) values (NEW.name);
END$
delimiter $
//ANOTHER TRIGGER

This is the error I see:

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "delimiter[*] $.... expected "DELETE, DROP, DECLARE, DEALLOCATE, {"; SQL statement:...

I have set MODE=MYSQL thru properties file, using DataSource programatically, setting mode in the data.sql file itself SET MODE MYSQL;

yml file:

spring.datasource.url
=jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
spring.datasource.username: myusername
spring.datasource.password: mypassword
driver-class-name: org.h2.Driver

Programatically:

@Bean
public DataSource dataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        EmbeddedDatabase db = builder
            .setType(EmbeddedDatabaseType.H2)
            .setName("mydb;DATABASE_TO_UPPER=false;MODE=MYSQL")
            .addScript("data.sql")
            .build();
        return db;
}

I have also tried to remove delimiter completely, but then it fails on the next line.

Faraz
  • 6,025
  • 5
  • 31
  • 88
  • @RaymondNijland I looked at it and next I will try to implement it that way. But there has to be a way to fix it without implementing Trigger interface. Thanks for sharing that link. – Faraz Oct 29 '18 at 14:31
  • "But there has to be a way to fix it without implementing Trigger interface." Analyze the H2 code it's opensource – Raymond Nijland Oct 29 '18 at 14:39
  • Thanks @RaymondNijland. That solved my problem. – Faraz Oct 29 '18 at 15:13
  • @Faraz How did you solve it? – Umer Khalid Dec 03 '21 at 15:32
  • @UmerKhalid I did this such a long time ago that I forgot. But I do remember solving it programatically. I may have implemented an interface or something to solve this. – Faraz Dec 03 '21 at 16:08

0 Answers0