Spring embeddeb db表已存在错误

cac*_*ert 7 spring-boot

我正在尝试使用嵌入式数据库运行spring boot应用程序.在bean初始化期间(由于某种原因?)我的表创建脚本被调用两次,第二次调用失败,并且"table already exists"错误.下面是我的代码,可能是什么问题.

@Configuration
public class AppConfig {

private static final Logger LOG = LoggerFactory.getLogger(AppConfig.class);

private static EmbeddedDatabase dataSource;

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new DbPlaceholderConfigurer(dataSource());
}


@Bean
public static EmbeddedDatabase dataSource() {
    if(dataSource == null) {
        EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder();
        databaseBuilder.addScript("classpath:schema.sql");
        //databaseBuilder.setName("test");
        databaseBuilder.setType(EmbeddedDatabaseType.H2);
        EmbeddedDatabase build = databaseBuilder.build();
        initPopulate(build);
        dataSource = build;
    }
    return dataSource;
}

private static void initPopulate(EmbeddedDatabase embeddedDatabase) {
    try {
        Connection connection = embeddedDatabase.getConnection();
        PreparedStatement prepareStatement;
        prepareStatement = connection.prepareStatement("INSERT INTO Settings VALUES (?,?,?)");
        prepareStatement.setInt(1, 1);
        prepareStatement.setString(2, "testKey");
        prepareStatement.setString(3, "testVal");
        prepareStatement.executeUpdate();
        connection.close();
    } catch (SQLException e) {
        LOG.error("Error ", e);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

错误日志如下:

Caused by: org.h2.jdbc.JdbcSQLException: Table "SETTINGS" already exists; SQL   statement:
CREATE TABLE Settings( id INT PRIMARY KEY, testKey VARCHAR(100), testValue VARCHAR(100) ) [42101-192]
Run Code Online (Sandbox Code Playgroud)

注意:我可以通过设置以下属性来成功启动我的应用程序,但我真的好奇为什么spring调用表创建脚本两次.

spring.datasource.continue-on-error=true
Run Code Online (Sandbox Code Playgroud)

注2:表创建脚本(schema.sql)如下所示:

create table contacts (
 id identity,
 firstname varChar(30) not null,
 lastName varChar(30) not null,
 phoneNumber varChar(20),
 emailAddress varChar(50)
);
Run Code Online (Sandbox Code Playgroud)

kyl*_*eus 9

我发现,当我有我的H2这是发生schema.sql文件data.sql文件在我的src/main /资源目录,在我还引用DatabaseConfig:

@Bean
public DataSource embeddedDataSource() {
    return new EmbeddedDatabaseBuilder()
        .setType(EmbeddedDatabaseType.H2)
        .addScript("classpath:schema.sql")
        .addScript("classpath:data.sql")
        .build();
}
Run Code Online (Sandbox Code Playgroud)

我在DatabaseConfig类中初始化内存中的数据库,然后Spring Boot尝试根据其配置规则加载相同的数据.

为了摆脱错误的删除schema.sql文件data.sql数据源() .

@Bean
public DataSource embeddedDataSource() {
    return new EmbeddedDatabaseBuilder()
        .setType(EmbeddedDatabaseType.H2).build();
}
Run Code Online (Sandbox Code Playgroud)


Ant*_*hin 2

您可以替换数据库初始化代码。Spring Boot开箱即用。以下属性应该可以让您有所了解:

spring.datasource.initialize=true # Populate the database using 'data.sql'.
spring.datasource.separator=; # Statement separator in SQL initialization scripts.
spring.datasource.sql-script-encoding= # SQL scripts encoding.
Run Code Online (Sandbox Code Playgroud)

Spring Boot 请参阅文档中的其他属性。