SpringBoot:集成H2数据库并持久化,url中mem、file有什么区别?
admin
2024-03-02 22:27:28
0

H2依赖

com.h2databaseh22.1.214

持久化配置

SpringBoot版本是2.3.12.RELEASE,下面有些配置看SpringBoot版本的,比如spring.datasource.schema是这个版本以下的,spring.sql.init.data是更高版本的

./dbfile 是把持久文件生成到程序启动路径下,你也可以自定义其他路径

# 数据源配置
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.schema=classpath:db/h2-schema.sql
spring.datasource.url=jdbc:h2:file:./dbfile
spring.datasource.username=root
spring.datasource.password=root
# 下面是控制台的配置
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

H2 url的含义

先看以下两个常量,代表url开头与后半段,后半段的类型有mem内存模式、file本地文件模式、tcp|ssl远程模式,含义如下(参考http://www.h2database.com/html/features.html#embedded_databases)

TopicURL Format and Examples
Embedded (local) connectionjdbc:h2:[file:][] jdbc:h2:~/test jdbc:h2:file:/data/sample jdbc:h2:file:C:/data/sample (Windows only)
In-memory (private)jdbc:h2:mem:
In-memory (named)jdbc:h2:mem: jdbc:h2:mem:test_mem
Server mode (remote connections) using TCP/IPjdbc:h2:tcp://[:]/[] jdbc:h2:tcp://localhost/~/test jdbc:h2:tcp://dbserv:8084/~/sample jdbc:h2:tcp://localhost/mem:test
Server mode (remote connections) using TLSjdbc:h2:ssl://[:]/[] jdbc:h2:ssl://localhost:8085/~/sample;

org.h2.engine.Constants

	/*** The database URL prefix of this database.*/public static final String START_URL = "jdbc:h2:";/*** The database URL format in simplified Backus-Naur form.*/public static final String URL_FORMAT = START_URL +"{ {.|mem:}[name] | [file:]fileName | " +"{tcp|ssl}:[//]server[:port][,server2[:port]]/name }[;key=value...]";

去驱动里查看,ConnectionInfo里把url截取,然后判断类型,当是file时,把持久化标识persistent标位了true,后续数据库启动和运行都会用到这个标识判断是否持久化

org.h2.engine.ConnectionInfo

	/*** Create a connection info object.** @param u the database URL (must start with jdbc:h2:)* @param info the connection properties or {@code null}* @param user the user name or {@code null}* @param password*            the password as {@code String} or {@code char[]}, or*            {@code null}*/public ConnectionInfo(String u, Properties info, String user, Object password) {u = remapURL(u);originalURL = url = u;if (!u.startsWith(Constants.START_URL)) {throw getFormatException();}if (info != null) {readProperties(info);}if (user != null) {prop.put("USER", user);}if (password != null) {prop.put("PASSWORD", password);}readSettingsFromURL();Object timeZoneName = prop.remove("TIME ZONE");if (timeZoneName != null) {timeZone = TimeZoneProvider.ofId(timeZoneName.toString());}setUserName(removeProperty("USER", ""));name = url.substring(Constants.START_URL.length());parseName();convertPasswords();String recoverTest = removeProperty("RECOVER_TEST", null);if (recoverTest != null) {FilePathRec.register();try {Utils.callStaticMethod("org.h2.store.RecoverTester.init", recoverTest);} catch (Exception e) {throw DbException.convert(e);}name = "rec:" + name;}}	private void parseName() {if (".".equals(name)) {name = "mem:";}if (name.startsWith("tcp:")) {remote = true;name = name.substring("tcp:".length());} else if (name.startsWith("ssl:")) {remote = true;ssl = true;name = name.substring("ssl:".length());} else if (name.startsWith("mem:")) {persistent = false;if ("mem:".equals(name)) {unnamed = true;}} else if (name.startsWith("file:")) {name = name.substring("file:".length());persistent = true;} else {persistent = true;}if (persistent && !remote) {name = IOUtils.nameSeparatorsToNative(name);}}

相关内容

热门资讯

哈萨克斯坦5月CPI环比上涨0... 6月2日消息,哈萨克斯坦5月CPI环比上涨0.9%。
Strategy在5月26日至... 6月2日消息,Strategy在5月26日至6月1日期间购买了705个比特币,总花费7510万美元,...
时序数据第一股:元光科技招股首... 6月2日消息,元光科技(02605.HK) 今天首日招股,据市场消息,今天开簿国际配售已获得超额认购...
长城汽车:5月汽车销量10.2... 6月2日消息,长城汽车(601633.SH)公告称,2025年5月公司销量为102,231台,同比增...
黄金股集体走强,潼关黄金涨超1... 6月2日消息,截至发稿,潼关黄金(00340.HK)涨10.42%、赤峰黄金(06693.HK)涨3...
如祺出行2025收入翻倍,利润... 3月31日,如祺出行(9680.HK)交出了2025年全年业绩报告。在全国多地频发网约车运力饱和预警...
港股收评:恒生指数收跌0.57... 6月2日消息,香港恒生指数收跌0.57%,报23157.97点,恒生科技指数收跌0.70%,报513...
“商业航天第一股”之战打响:中... 商业航天第一股争夺战正式打响3月31日,中科宇航技术股份有限公司(下称“中科宇航”)的科创板IPO申...
特斯拉在丹麦5月销量同比下降3... 6月2日消息,特斯拉在丹麦5月销量同比下降30.5%。