24

I am configuring a database in Spring JPA and I want to know what the possible values are of spring.datasource.initialization-mode. I found this page with common properties but it doesn't give all possible values. I'd expect there to be some documentation on all possible values of all properties you can set.

I am using the property in the props section in my applicationContext.xml as properties for the entityManagerFactory

<util:properties id="props">
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</prop>
    <prop key="hibernate.hbm2ddl.auto">create</prop>
    <prop key="hibernate.ddl-auto">create</prop>
    <prop key="spring.jpa.show-sql">true</prop>
    <prop key="spring.jpa.generate.ddl">true</prop>
    <prop key="spring.jpa.hibernate.ddl-auto">create</prop>
    <prop key="spring.datasource.initialization-mode">always</prop>
    <prop key="spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation">true</prop>
</util:properties>
kometen
  • 6,536
  • 6
  • 41
  • 51
Christine
  • 5,617
  • 4
  • 38
  • 61

4 Answers4

23

When all else fails, you remember "use the source, Luke!". The values are given in the Javadoc of the enum DataSourceInitializationMode. Values are always, embedded and never.

Christine
  • 5,617
  • 4
  • 38
  • 61
  • 1
    *it doesn't give all possible vallues.*, then how did you concluded your answer ? do you have any official source ? or your own research ? – Ravi Dec 25 '18 at 12:16
  • 8
    Javadoc of an enum should give all possible values. Because that's how an enum works. – Christine Dec 25 '18 at 13:40
  • I'm trying to do this in spring boot. In my application.properties field I set spring.datasource.initialization-mode=embedded, but I got a warning that said "Cannot resolve configuration property 'spring.datasource.initialization-mode'" Does anyone know a way to specify this in spring-boot? – MiguelMunoz Jan 10 '21 at 03:07
13

Forgive me for butting in almost a year late. After having faced a similar problem as explained by Christine, I decided to take the clue and begin searching in the source. It would appear that the following is detailed in the link here https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/jdbc/DataSourceInitializationMode.html :

Enum Constant Summary Enum Constants

Enum Constant and Description

ALWAYS Always initialize the datasource.

EMBEDDED Only initialize an embedded datasource.

NEVER Do not initialize the datasource.

3

Simply put, there're 3 options: always, embedded and never.

Note that: the property spring.datasource.initialization-mode is outdated. You may want to consider spring.sql.init.mode instead for new projects.

source for the old one

/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.jdbc;

/**
 * Supported {@link javax.sql.DataSource} initialization modes.
 *
 * @author Vedran Pavic
 * @author Stephane Nicoll
 * @since 2.0.0
 * @see AbstractDataSourceInitializer
 */
public enum DataSourceInitializationMode {

    /**
     * Always initialize the datasource.
     */
    ALWAYS,

    /**
     * Only initialize an embedded datasource.
     */
    EMBEDDED,

    /**
     * Do not initialize the datasource.
     */
    NEVER

}

source for the new

/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.sql.init;

/**
 * Supported database initialization modes.
 *
 * @author Andy Wilkinson
 * @since 2.5.1
 * @see AbstractScriptDatabaseInitializer
 */
public enum DatabaseInitializationMode {

    /**
     * Always initialize the database.
     */
    ALWAYS,

    /**
     * Only initialize an embedded database.
     */
    EMBEDDED,

    /**
     * Never initialize the database.
     */
    NEVER

}
JJJohn
  • 915
  • 8
  • 26
0

Spring Behaviour varies w.r.t to the Spring Version From 2.7 Version of Spring

  1. Spring Creates Table using schema.sql and data.sql in classpath for Embedded i.e memory database spring.datasource.url=jdbc:h2:mem:somedbofyours

I have tried the following configuration

  1. Create file based H2

    spring.datasource.url=jdbc:h2:file:~/db/eesandb spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=none

Whatever be the above combinations, it didnt create or execute schema.sql in 2.7.4

  1. The Following Property spring.sql.init.mode determines creation of DDL,DML by referring schema.sql and data.sql in classpath

Example spring.sql.init.mode = always (or) spring.datasource.initialization-mode=always.

Visit Similar Post Why Spring Boot 2.0 application does not run schema.sql?