Spring-boot
2016-04-18 09:50:21 6 举报
AI智能生成
Spring-boot data source configuration
作者其他创作
大纲/内容
Embedded Database support
auto-configure support
H2
HSQL
Derby
Simply include a build dependency
Example
org.springframework.boot
spring-boot-starter-data-jpa
org.hsqldb
hsqldb
runtime
Connection URLs
Not required
If you do config, ensure db's automatic shutdown is disabled
H2: DB_CLOSE_ON_EXIT=FALSE
In jHipster
jdbc:h2:file:./target/h2db/db/my-app;DB_CLOSE_DELAY=-1
HSQLDB: shutdown=true is NOT used
DBs
H2
In-Memory
One connection per db
jdbc:h2:mem:
Opening two connections within the same JVM means opening two dbs.
Multiple connections per db
jdbc:h2:mem:db-name
Accessing the same database only works within the same VM and class loader
Access from another process
jdbc:h2:tcp://localhost/mem:db-name
Closing
By default, closing the last connection to a db closes the db.
adding ';DB_CLOSE_DELAY=-1' to the URL keeps the db alive as long as the JVM is alive.
File
URL: 'jdbc:h2:[file:][]'
'file:' is optional
Closing
adding ';DB_CLOSE_DELAY=-1' to the URL keeps the db alive as long as the JVM is alive.
adding ';DB_CLOSE_ON_EXIT=FALSE' to disable db closing on JVM exiting.
This is RECOMMENDED in spring-boot tutorials
Compatibility
compatibility modes
DB2, Derby, HSQLDB, MSSQL, MySQL, Oracle
Integration test
an empty maven project
HSQLDB (server mode)
java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:sandboxDb --dbname.0 sandboxDb
java -cp .;../lib/hsqldb.jar org.hsqldb.util.DatabaseManager -user sa -url jdbc:hsqldb:database/sandboxDb
integrate with liquibase-maven-plugin
#liquibase.properties
driver: org.hsqldb.jdbc.JDBCDriver
url: jdbc:hsqldb:hsql://localhost/sandboxDb
username: SA
password:
org.hsqldb:hsqldb:2.2.8
org.liquibase:liquibase-core:2.0.5
org.liquibase:liquibase-maven-plugin:2.0.5
liquibase.properties
db.changelog.xml
database schema/data deployment
spring orm / hibernate / transactional annotation
test with in-memory db
Database
dependencies
spring-boot-starter-web
spring-boot-starter-validation
hibernate-validator
tomcat-embed-el

收藏
0 条评论
下一页