`

嵌入式 内存 数据库H2 Mixed Mode布署

阅读更多
  •  
    • -help or -? (print the list of options)
    • -web (start the Web Server and H2 Console)
    • -browser (start a browser and open a page to connect to the Web Server)
    • -tcp (start the TCP Server)
    • -tcpShutdown {url} (shutdown the running TCP Server, URL example: tcp://localhost:9094)
    • -pg (start the PG Server)
    • -ftp (start the FTP Server)
    • -trace (print additional trace information; for all servers)
    • -baseDir {directory} (sets the base directory for H2 databases; for all servers)
    • -ifExists (only existing databases may be opened; for all servers)
    • -webPort {port} (the port of Web Server, default: 8082)
    • -webSSL (HTTPS is to be be used)
    • -webAllowOthers (enable remote connections)
    • -tcpPort {port} (the port of TCP Server, default: 9092)
    • -tcpSSL (SSL is to be used)
    • -tcpAllowOthers (enable remote connections)
    • -tcpPassword {password} (the password for shutting down a TCP Server)
    • -tcpShutdownForce (don't wait for other connections to close)
    • -pgPort {port} (the port of PG Server, default: 5435)
    • -pgAllowOthers (enable remote connections)
    • -ftpPort {port}
    • -ftpDir {directory}
    • -ftpRead {readUserName}
    • -ftpWrite {writeUserName}
    • -ftpWritePassword {password}
      Shutdown a TCP server. If force is set to false, the server will not allow new connections, but not kill existing connections, instead it will stop if the last connection is closed. If force is set to true, existing connections are killed. After calling the method with force=false, it is not possible to call it again with force=true because new connections are not allowed. Example:
    Server.shutdownTcpServer("tcp://localhost:9094", password, true);
    
The command line interface for this tool. The options must be split into strings like this: "-baseDir", "/temp/data",... By default, -tcp, -web, -browser and -pg are started. If there is a problem starting a service, the program terminates with an exit code of 1. Options are case sensitive. The following options are supported: For each Server, additional options are available:

  •  
    •  
      •  
          Create a new web server, but does not start it yet. Example:
        Server server = Server.createWebServer(
        new String[] { "-trace" }).start();
        
    • Create a new TCP server, but does not start it yet. Example:
    Server server = Server.createTcpServer(
    new String[] { "-tcpAllowOthers" }).start();
    
Create a new PG server, but does not start it yet. Example:
Server server =
Server.createPgServer(new String[]{
"-pgAllowOthers"}).start();
Create a new ftp server, but does not start it yet. Example:
Server server = Server.createFtpServer(
new String[] { "-trace" }).start();

如果不指定H2的数据库文件输出到"C:\Documents and Settings\<userName>",如果你想让数据库文件输出到你指定的文件夹就设置URLs= jdbc:h2 :F:/h2/test。就是在URLs里面设置输出文件夹。

===========================================

Connection Modes

The following connection modes are supported:

  • Embedded mode (local connections using JDBC)
  • Remote mode (remote connections using JDBC or ODBC over TCP/IP)
  • Mixed mode (local and remote connections at the same time)

Embedded Mode

In embedded mode, an application opens a database from within the same JVM using JDBC. This is the fastest and easiest connection mode. The disadvantage is that a database may only be open in one virtual machine (and class loader) at any time. As in all modes, both persistent and in-memory databases are supported. There is no limit on the number of database open concurrently, or on the number of open connections.

The database is embedded in the application

Remote Mode

When using the remote mode (sometimes called server mode or client/server mode), an application opens a database remotely using the JDBC or ODBC API. A server needs to be started within the same or another virtual machine (or on another computer). Many applications can connect to the same database at the same time. The remote mode is slower than the embedded mode, because all data is transferred over TCP/IP. As in all modes, both persistent and in-memory databases are supported. There is no limit on the number of database open concurrently, or on the number of open connections.

The database is running in a server; the application connects to the server

Mixed Mode

The mixed mode is a combination of the embedded and the remote mode. The main application connects to a database in embedded mode, but also starts a server so that other applications (running in different virtual machines) can concurrently access the same data. The embedded connections are as fast as if the database is used in just the embedded mode, while the remote connections are a bit slower.

The database and the server is running inside the application; another application connects remotely

 

Mixed Mode Deploy

Method 1: Start server in J2EE application

Java Code (MixedMode.java):

package org.h2.samples;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.tools.Server;

/**
 * This sample program opens the same database once in embedded mode,
 * and once in the server mode. The embedded mode is faster, but only
 * the server mode supports remote connections.
 */
public class MixedMode {

    /**
     * This method is called when executing this sample application from the
     * command line.
     *
     * @param args the command line parameters
     */
    public static void main(String[] args) throws Exception {

        // start the server, allows to access the database remotely
        Server server = Server.createTcpServer(new String[] { "-tcpPort", "9101" });
        server.start();


        System.out.println("You can access the database remotely now, using the URL:");
        System.out.println("jdbc:h2:tcp://localhost:9081/~/test (user: sa, password: sa)");

        // now use the database in your application in embedded mode


        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:file:E:/Products/Opensource/h2/lee_test/test", "sa", "");

        //Connection conn = DriverManager.getConnection("jdbc:h2:file:E:/Products/Opensource/h2/lee_test/test;AUTO_SERVER=TRUE", "sa", ""); 

        // some simple 'business usage'
        Statement stat = conn.createStatement();
        stat.execute("DROP TABLE TIMER IF EXISTS");                                   
        stat.execute("CREATE TABLE TIMER(ID INT PRIMARY KEY, TIME VARCHAR)");


        System.out.println("Execute this a few times: SELECT TIME FROM TIMER");
        System.out.println("To stop this application (and the server), run: DROP TABLE TIMER");


        try {
            while (true) {
                // runs forever, except if you drop the table remotely
                stat.execute("MERGE INTO TIMER VALUES(1, NOW())");
                Thread.sleep(1000);
            }
        } catch (SQLException e) {
            System.out.println("Error: " + e.toString());
        }


        conn.close();

 

        // stop the server
        server.stop();
    }
}

 

主程序Run后,是以Embeded模式连接,此时利用H2 Tools中的Broswer UI,进行连接(模拟另一Client),可以有以下几种连接方式:

1. jdbc:h2:tcp://localhost:9101/file:E:/Products/Opensource/h2/lee_test/test

2. jdbc:h2:tcp://localhost:9101/file:E:/Products/Opensource/h2/lee_test/test;AUTO_SERVER=TRUE

 

如果将程序中的黄底部分的语句换成绿底部分的语句,可以有以下几种连接方式:

 

1. jdbc:h2:tcp://localhost:9101/file:E:/Products/Opensource/h2/lee_test/test

2. jdbc:h2:tcp://localhost:9101/file:E:/Products/Opensource/h2/lee_test/test;AUTO_SERVER=TRUE

3. jdbc:h2:file:E:/Products/Opensource/h2/lee_test/test;AUTO_SERVER=TRUE (这种方式自动转换为Server模式,谁先连上谁是Embed模式)

 

 

 

Method 2: Start server via command line:

Command line:

@java -cp "h2.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.Server -tcp -tcpPort 9101

@if errorlevel 1 pause

 

Java Code (MixedMode.java):

package org.h2.samples;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.tools.Server;

/**
 * This sample program opens the same database once in embedded mode,
 * and once in the server mode. The embedded mode is faster, but only
 * the server mode supports remote connections.
 */
public class MixedMode {

    /**
     * This method is called when executing this sample application from the
     * command line.
     *
     * @param args the command line parameters
     */
    public static void main(String[] args) throws Exception {

        // start the server, allows to access the database remotely
        Server server = Server.createTcpServer(new String[] { "-tcpPort", "9101" });
        server.start();


        System.out.println("You can access the database remotely now, using the URL:");
        System.out.println("jdbc:h2:tcp://localhost:9081/~/test (user: sa, password: sa)");

        // now use the database in your application in embedded mode


        Class.forName("org.h2.Driver");
        //Connection conn = DriverManager.getConnection("jdbc:h2:file:E:/Products/Opensource/h2/lee_test/test", "sa", ""); //以Command Line方式启Server,不能使用这种方式了

        Connection conn = DriverManager.getConnection("jdbc:h2:file:E:/Products/Opensource/h2/lee_test/test;AUTO_SERVER=TRUE", "sa", "");

        // some simple 'business usage'
        Statement stat = conn.createStatement();
        stat.execute("DROP TABLE TIMER IF EXISTS");                                   
        stat.execute("CREATE TABLE TIMER(ID INT PRIMARY KEY, TIME VARCHAR)");


        System.out.println("Execute this a few times: SELECT TIME FROM TIMER");
        System.out.println("To stop this application (and the server), run: DROP TABLE TIMER");


        try {
            while (true) {
                // runs forever, except if you drop the table remotely
                stat.execute("MERGE INTO TIMER VALUES(1, NOW())");
                Thread.sleep(1000);
            }
        } catch (SQLException e) {
            System.out.println("Error: " + e.toString());
        }


        conn.close();

 

        // stop the server
        server.stop();
    }
}

 

主程序Run后,是以Embeded模式连接,此时利用H2 Tools中的Broswer UI,进行连接(模拟另一Client),可以有以下几种连接方式:

1. jdbc:h2:file:E:/Products/Opensource/h2/lee_test/test;AUTO_SERVER=TRUE (这种方式自动转换为Server模式,谁先连上谁是Embed模式)

分享到:
评论

相关推荐

    使用JAVA内存数据库h2database性能优化

    【使用JAVA内存数据库h2database性能优化】 在开发应用程序时,我们经常遇到性能瓶颈,特别是当涉及到大量的IO操作时。数据库访问是这类问题的主要来源,特别是在处理高并发、实时计算和海量数据监控的情况下。例如...

    Cache Server V1.2.0 嵌入式(实时)内存数据库(linux 32)

    Cache Server嵌入式内存数据库是业内运行速度最快,功能强大的嵌入式(实时)内存数据库系统。产品定位于内存数据库系统和提供高端高性能系统的开发、处理平台。 Cache Server内存数据库是将所有数据加载到物理内存...

    嵌入式移动数据库研究

    嵌入式移动数据库是当前信息技术领域的一个重要分支,它结合了嵌入式系统、移动计算和数据库技术的优势,为各种移动设备提供了数据管理和处理的能力。嵌入式移动数据库(Embedded Mobile Database, EMDB)是在嵌入式...

    基于嵌入式内存数据库引擎的研究与设计

    【嵌入式内存数据库引擎】是一种特殊的数据库管理系统,它的核心设计是将数据库的主拷贝存储在内存中,而非依赖于传统的磁盘存储。这种设计显著提升了数据存取和处理速度,尤其适用于需要快速响应和高效率操作的系统...

    嵌入式内存数据库的研究与设计

    嵌入式内存数据库是一种专为嵌入式设备设计的数据库系统,它将数据完全或主要存储在内存中,以提供高速访问和响应能力。近年来,随着硬件技术的进步,内存容量大幅增加,使得嵌入式内存数据库的应用变得越来越普遍。...

    LokiJS一个JavaScript的嵌入式内存数据库

    LokiJS是一个专为JavaScript设计的轻量级、高效的嵌入式内存数据库。它主要用于客户端应用,特别是Web浏览器或Node.js环境中的数据管理。LokiJS的设计目标是提供快速、灵活的数据存储解决方案,无需依赖外部数据库...

    嵌入式系统/ARM技术中的嵌入式内存数据库的研究与设计

    【嵌入式内存数据库】是近年来随着硬件技术发展而逐渐受到关注的一个领域,尤其是在嵌入式系统/ARM技术中。由于内存容量的扩大,将数据存储在内存中以实现更快的访问速度和更高的效率成为可能。嵌入式设备在日常生活...

    内存数据库h2database(h2-2008-11-07)

    内存数据库H2 Database是Java开发的一个轻量级、高性能、开源的关系型数据库系统。它可以在内存中运行,也可以存储在磁盘上,并且支持多种模式,包括单用户模式、服务器模式以及分布式集群模式。H2 Database的设计...

    h2嵌入式数据库例子 springboot+h2+mybatisplus+swagger使用例子

    H2是一个开源的嵌入式数据库引擎,采用java语言编写,不受平台的限制,同时H2提供了一 个十分方便的web控制台用于操作和管理数据库内容。H2还提供兼容模式,可以兼容一些主 流的数据库,具有比较完备的数据库特性...

    嵌入式数据库H2开始服务

    嵌入式数据库H2是Java开发的一款轻量级、高性能的关系型数据库,广泛应用于桌面应用程序和服务器环境。它以其小巧的体积、快速的性能和易于使用的特点,在IT领域内受到许多开发者的青睐。H2数据库支持多种模式,包括...

    内存数据库H2

    内存数据库H2是一种轻量级、高性能的开源数据库系统,主要设计用于在内存中存储和处理数据,以提供快速的数据访问速度。它被广泛应用于测试、开发和嵌入式环境,尤其是在需要快速响应时间的应用中。H2数据库支持多种...

    内存数据库(h2多种数据库)

    内存数据库,如H2,是一种将数据存储在内存中的数据库管理系统。相较于传统的磁盘存储数据库,内存数据库在处理速度上有着显著优势,因为它们避免了磁盘I/O操作的延迟。H2是一款开源、轻量级、高性能的数据库,常...

    JAVA在SQLite嵌入式数据库中的应用.rar

    SQLite 作为一个开源的嵌入式数据库产品,具有系统开销小,检索效率高的特性,适用于手机、PDA、机顶盒设备等电器,并且作为嵌入式数据库在可下载的消费类应用程序中运行的很好。这篇文章介绍嵌入式数据库产品SQLite...

    嵌入式内存数据库引擎的设计

    嵌入式内存数据库引擎的设计是当前信息技术领域的一个关键议题,特别是在3G平台和高数据处理需求的背景下。嵌入式内存数据库引擎不同于传统的磁盘数据库,它的核心特点是数据库完全驻留在内存中,减少了磁盘I/O操作...

    H2内存数据库资料及DEMO

    H2内存数据库是一种轻量级、高性能的关系型数据库,它主要设计用于嵌入式系统,也可作为服务器模式运行。H2数据库的特点在于其快速、小巧且完全免费,它支持多种数据库模式,包括单用户模式、多用户模式以及内存模式...

    嵌入式数据库H2

    嵌入式数据库H2。不受平台的限制,适合作为嵌入式数据库试用。

    基于linux和Qt的嵌入式超市数据库管理程序

    在本项目中,我们探讨的是一个基于Linux操作系统和Qt框架的嵌入式超市数据库管理系统。这个系统设计的主要目标是提供一种高效、可靠的解决方案,用于跟踪超市的商品进货、出货和库存情况。以下将详细阐述该项目涉及...

    嵌入式移动数据库研究.doc

    (2)高效的事务处理嵌入式移动数据库往往面临资源有限的环境,如内存、CPU性能限制等。因此,数据库管理系统需要优化事务处理,例如采用轻量级事务模型,减少事务的开销,同时确保数据的完整性。 (3)数据安全性...

Global site tag (gtag.js) - Google Analytics