• SpringBoot:集成H2数据库并持久化,url中mem、file有什么区别?


    H2依赖

    <dependency>
        <groupId>com.h2databasegroupId>
        <artifactId>h2artifactId>
        <version>2.1.214version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    持久化配置

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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...]";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    去驱动里查看,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);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  • 相关阅读:
    Python OpenCV剪裁图片并修改对应的Labelme标注文件
    Markdown 数学公式详解
    DRF 自动生成接口文档
    通信真的是天坑专业吗?应届毕业生出来能做什么?
    Apache安装教程
    es的使用方法以及概念
    Camx-Dump Raw Frames
    【MyBatis框架】第二章 MyBatis入门
    【BLE】蓝牙技术的应用
    记一个src中危-图像大小与请求参数可修改
  • 原文地址:https://blog.csdn.net/weixin_43859729/article/details/128113982