ythy / blog

Give everything a shot
7 stars 0 forks source link

In memory database #181

Open ythy opened 5 years ago

ythy commented 5 years ago

What is an in memory database?

Typical databases involve a lot of setup.

Scenario 1 - Let’s consider a situation where you would want to do a quick DEV. Using a traditional database involves a lot of overhead.

Scenario 2 - Consider your unit tests You don’t want them to fail when some data/schema in the database changes You would want to be able to run them in parallel - multiple developers might be running the tests in parallel.

In these kind of scenarios, an in memory database provides an ideal solution. An in memory database is created when an application starts up and destroyed when the application is stopped.

Advantages

H2

H2 is one of the popular in memory databases. Spring Boot has very good integration for H2.

H2 is a relational database management system written in Java. It can be embedded in Java applications or run in the client-server mode.

H2 supports a sub set of the SQL standard. H2 also provides a web console to maintain the database.

ythy commented 5 years ago

必要的h2配置

# Enabling H2 Console
spring.h2.console.enabled=true

spring.datasource.url=jdbc:h2:mem:springs
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    runtime('com.h2database:h2')
}
ythy commented 5 years ago

H2数据库控制台地址: 注意 JDBC URLspring.datasource.url 的值 http://127.0.0.1:7777/h2-console/

ythy commented 5 years ago

Important: 1.jdbc:h2:mem is used to define In-memory databases.

  1. jdbc:h2:file is used to define disk-based databases.