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);}}

相关内容

热门资讯

今日科普作弊器!久久互娱作弊器... 今日科普作弊器!久久互娱作弊器AI插件,久久互娱多功能原来真的有作弊器插件久久互娱是不是有人用挂微扑...
揭秘!常胜棋牌透视功能脚本,常... 揭秘!常胜棋牌透视功能脚本,常胜棋牌都是是有透视脚本(有辅助挂辅教程)1、在常胜棋牌插件功能辅助器技...
科技揭秘透视挂!丫丫衡阳字牌3... 科技揭秘透视挂!丫丫衡阳字牌3透视挂AI软件,丫丫衡阳字牌3多功能总是是有透视挂软件1、许多玩家不知...
玩家必备教程透视挂!葫芦岛爱玩... 玩家必备教程透视挂!葫芦岛爱玩麻将透视挂AI攻略,葫芦岛爱玩麻将多功能确实有透视挂攻略1、下载好葫芦...
玩家实测!微乐陕西三带透视功能... 玩家实测!微乐陕西三带透视功能插件,微乐陕西三带一贯有透视插件(详细教程辅助)微乐陕西三带能透视中分...
重大通报外挂!闲逸斗地主外挂A... 重大通报外挂!闲逸斗地主外挂AI方法,闲逸斗地主多功能一贯是有外挂方法闲逸斗地主能透视中分为三种模型...
一分钟了解!棋牌游辅助功能攻略... 一分钟了解!棋牌游辅助功能攻略,棋牌游本来真的是有辅助攻略(有辅助挂技巧)1、进入游戏-大厅左侧-新...
科技新动态辅助!老地方辅助AI... 科技新动态辅助!老地方辅助AI脚本,老地方多功能总是真的有辅助脚本1、每一步都需要思考,不同水平的挑...
如何分辨真伪!蜜瓜大厅辅助器功... 如何分辨真伪!蜜瓜大厅辅助器功能方法,蜜瓜大厅切实真的是有辅助器方法(有挂辅方法)在进入蜜瓜大厅软件...
一分钟秒懂开挂!姜堰23张开挂... 一分钟秒懂开挂!姜堰23张开挂AI教程,姜堰23张多功能切实存在有开挂教程1、姜堰23张公共底牌简单...