0

I am using below spring boot config:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

When my spring app comes up, I see the following:

H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:304a69fe-27f6-4271-a5c3-015f06910885'

However if i set the below in property file, i do see testdb being connected:

spring.datasource.url=jdbc:h2:mem:testdb

Can someone please let me know why do i need to explicitly set the url in property file? I had created another spring boot app recently with the exact same config but with spring boot version 2.2.4.RELEASE where h2 connected by default to testdb without setting it in property file.

Thanks!

user1318369
  • 715
  • 5
  • 15
  • 34

4 Answers4

0

This change ensures that each test in a test suite that shares an application context gets a unique embedded database, to prevent inconsistent embedded database state between tests.

You can revert to the previous behavior by following setting:

spring.datasource.generate-unique-name=false
0

Found out that with the latest versions of Spring Boot (2.3+), the H2 database name is randomly generated each time you restart the server. Similar post: springboot 2.3.0 while connecting to h2 database

user1318369
  • 715
  • 5
  • 15
  • 34
  • I am sorry but I think you didnt read my answer properly. I too mentioned the same. **If you are using any of this annotations - DataJdbcTest, DataJpaTest and JdbcTest in your test, then Spring through AutoConfigureTestDatabase will call TestDatabaseAutoConfiguration, which in turn, by default, will configure an in-memory embedded database instance with an auto generated unique name.** – Abhinaba Chakraborty Jul 20 '20 at 13:52
  • @AbhinabaChakraborty, I already knew the solution before posting this(add spring.datasource.url=jdbc:h2:mem:testdb in property file). However my question was why I would have to explicitly do it for one version of spring and not for another. As I mentioned, for the version 2.2.4.RELEASE, this wasn't necessary. As for the annotations you mentioned, I am not using any of those in either of the projects with the different spring versions. – user1318369 Jul 21 '20 at 17:03
0

I have done all the things still it is giving me Database "C:/Users/abhis/test" not found, either pre-create it or allow remote database creation (not recommended in secure environments)

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 05 '23 at 11:15
-1

Update:

As you are using h2 console, you probably have a property called

spring.h2.console.enabled=true

If so then Spring's H2ConsoleAutoConfiguration class gets enabled and it does the auto-configuration as given below. (Check here )


If you are using any of this annotations - @DataJdbcTest, @DataJpaTest and @JdbcTest in your test, then Spring through @AutoConfigureTestDatabase will call TestDatabaseAutoConfiguration, which in turn, by default, will configure an in-memory embedded database instance with an auto generated unique name.

If you want to solve the problem for single test case, please use:

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

If you want this to apply for all test cases, then please have this property in application.yaml

spring:
  test:
    database:
      replace: none
Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
  • I am not using any of the mentioned annotations, also this is not for any tests, I am connecting to h2 db from the main application – user1318369 Jul 06 '20 at 13:42
  • @user1318369 Do you have the property spring.h2.console.enabled defined? as i mentioned in my updated answer – Abhinaba Chakraborty Jul 06 '20 at 13:54
  • yes, I do have that property set. As I mentioned, in both the spring boot applications I have that set(with the verisons 2.2.4.RELEASE and 2.3.1.RELEASE), however for the latter I had to explicitly specifiy the spring.datasource.url, or else it assigns a random url which i can see in the application logs. So wondering why this different behavior? – user1318369 Jul 07 '20 at 14:16