Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot in memory database H2 doesn't load data from file on initialization

I have an issue with loading data into an in-memory database on application initialization. I have created schema.sql and data.sql files containing table structure and initial data.

schema.sql :

CREATE TABLE users (
  id          INT PRIMARY KEY,
  username    VARCHAR(64) NOT NULL,
  password    VARCHAR(64) 
);

and data.sql :

INSERT INTO users (id, username, password) VALUES
  (1, 'usr1', 'bigSecret'),
  (2, 'usr2', 'topSecret');

I am using JpaRepository for working with data layer:

public interface UserRepository extends JpaRepository<User, Long> {
}

And I also configure application.properties

spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

But when I call

List<User> users = userRepository.findAll();

User entity

@Entity
@Table(name = "users")
public class User {

  @Id
  @GeneratedValue
  private Long id;
  private String username;
  private String password;

  public User() {  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}

I get an empty list, but I should get two pre-populated users from my in-memory H2 database. What's wrong with in memory database? Thanks.

like image 502
tyomka Avatar asked Jan 24 '17 23:01

tyomka


People also ask

How do I access H2 in-memory database?

Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.

How do I transfer data to my H2 database?

Syntax. Following is the basic syntax of INSERT INTO statement. INSERT INTO tableName { [ ( columnName [,...] ) ] { VALUES { ( { DEFAULT | expression } [,...] ) }

Is H2 in-memory database suitable for development and testing environment?

It can be embedded in Java applications or run in the client-server mode. Mainly, H2 database can be configured to run as inmemory database, which means that data will not persist on the disk. Because of embedded database it is not used for production development, but mostly used for development and testing.


1 Answers

You can always try to run those scripts per specification of h2, where you should add an INIT script in your connection url (being one of the options):

jdbc:h2:mem:test;INIT=RUNSCRIPT FROM '~/schema.sql'\;RUNSCRIPT FROM '~/data.sql'"

This functionality is enabled via the INIT property. Note that multiple commands may be passed to INIT, but the semicolon delimiter must be escaped, as in the example below.

Update

Be aware that having these options in your application.properties:

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=true
spring.datasource.initialize=true

may cause some clashing during startup. So you should always aim for one or the other, but never both at the same time. For simple cases just these alone are sufficient to auto build tables and reload after shutdown & startup

like image 92
Maciej Kowalski Avatar answered Oct 12 '22 05:10

Maciej Kowalski