Home  >  Article  >  Java  >  Why does \"Table Not Found\" Error Occur with H2 In-Memory Database and How to Fix It?

Why does \"Table Not Found\" Error Occur with H2 In-Memory Database and How to Fix It?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 03:21:03880browse

 Why does

H2 In-Memory Database: Resolving "Table Not Found" Error

When using an H2 in-memory database with the URL "jdbc:h2:mem:test", users may encounter the error "Table 'PERSON' not found" while attempting to select data from a previously created table. This error occurs because, by default, H2 closes the database connection after table creation using Hypernate's hbm2ddl feature.

To resolve this issue and preserve the database content, modify the connection URL to the following format:

<code class="text">jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</code>

This parameter instructs H2 to keep the in-memory database open as long as the Java virtual machine (JVM) is running. By adding a semicolon (;) instead of a colon (:) between the URL and the parameter, you can specify additional parameters.

As the documentation states, "closing the last connection to a database closes the database. For an in-memory database, this means the content is lost." By setting the DB_CLOSE_DELAY=-1 parameter, you ensure that the database remains open until the JVM terminates, preserving the data even after the connection is closed.

The above is the detailed content of Why does \"Table Not Found\" Error Occur with H2 In-Memory Database and How to Fix It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn