http://my.oschina.net/javagg/blog/3322
有一篇描述了如何在H2内存数据库中部署MixMode模式。在这个问题上我先试错了半天,这里总结一下遇到的几个坑
1)为了部署MixMode模式,embeded 那个节点必须启动一个TcpServer,否则其它节点无法通过:tcp://...访问此节点
如下代码
Server server = Server.createTcpServer(new String[] { "-tcpPort", "9101" });
server.start();
2)那篇文章中
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)");
是误导,如果启动的TcpServer用的9101,那么其它节点应该通过jdbc:h2:tcp://localhost:9101/.../test访问。
3)最好不要使用AUTO_SERVER=TRUE,因为如文档中所述,这个方式是不支持通过网络协议(tcp:// ssl://)访问embeded的
(Automatic Mixed Mode)This mode has three disadvantages: All processes need to have access to the database files. Then, if the first connection is
closed (the connection that started the server), open transactions of other connections will be rolled back. Also, explicit
client/server connections (using jdbc:h2:tcp:// or ssl://) are not supported.
4)虽然完全使用Server/client模式也可以完成业务需求,但是性能差太多了。我作的一个简单的insert 测试,embed比通过tcp走快大约60倍。所以如果可能主应用还是应该用embeded,让console通过Server/client来连。
以下是转帖的http://my.oschina.net/javagg/blog/3322
原文
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.

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.

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.

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模式)
分享到:
相关推荐
H2内存数据库h2部署操作手册.pdf
H2就不做很多介绍了。资源包内容列表是我进行H2预研是收集的H2资料,应该是最全面的的了: ...10、H2内存数据库h2部署操作手册.docx 11、H2内存数据库安装与维护.doc 12、H2数据库基础知识.docx 13、H2数据库使用.doc
H2Database是一款轻量级、开源的嵌入式和服务器模式的Java数据库,适用于测试、开发和嵌入式系统中的数据存储。它以其高性能、易用性和灵活性而受到开发者的欢迎。本文将深入探讨H2Database的高级特性,帮助你更好地...
H2数据库是一个开源的关系型数据库管理系统,由Java编写,能够嵌入到Java应用程序中运行,同时也提供网络版支持。H2遵循SQL标准,与多种Java持久层框架兼容,如Hibernate和EclipseLink,并且支持集群环境。在使用H2...
我们也可以合理推测,“H2P倡议”所涉及的源码可能是一个开源项目的核心代码库,它不仅包含了解决问题的代码,还融合了设计模式和最佳实践,为开发者提供了一种高效开发互联网应用的新方法。同时,相关的工具可能...
H2Database是一款轻量级、高性能的内存数据库,它支持多种数据库模式,包括单用户模式、多用户服务器模式,以及嵌入式模式。本教程旨在帮助用户了解并熟练掌握H2的使用。 ### 数据库配置 配置H2数据库涉及到设置...
根据提供的文件信息,我们可以从《ZOOM H2中文说明书》这一文档中提炼出与ZOOM H2相关的多个重要知识点。 ### ZOOM H2简介 ZOOM H2是一款由日本ZOOM公司生产的手持式立体声录音机。它以其小巧便携、操作简便以及...
本文详细介绍了如何将 H2 数据库从本地 JAR 包部署到 Kubernetes 集群中,并解决了在 Kubernetes 使用 Ingress 时可能遇到的 TCP 服务支持问题。通过理解 H2 数据库的配置和连接方式,以及 Kubernetes 的服务暴露...
H2提供了多种运行模式,包括内嵌模式(Embedded Mode)、服务器模式(Server Mode)以及Web模式(Web Console),使其在各种应用场景下都能灵活运用。 在内嵌模式下,H2数据库直接作为应用程序的一部分运行,无需...
h2 database 是一个开源免费的Java SQL数据库 数据库分为嵌入式 Embedding 和Server模式 要使用embedding mode 需要做以下事情: 1 添加h2 jar到classpath中 2 使用jdbc驱动:org h2 Driver 3 jdbc数据库URL:jdbc:...
启动h2database嵌入模式通常涉及将h2database的jar文件添加到项目的类路径中,然后通过Java代码调用特定的API来创建和管理数据库。例如,可以使用`org.h2.tools.Server`类启动内存数据库服务。 性能优化策略包括: ...
用户可以直接在类路径中包含H2数据库,从而简化部署流程。 2. **多模式支持**: H2数据库支持多种运行模式,包括内嵌模式、服务器模式、客户端/服务器模式以及分布式模式。这使得H2能适应各种应用场景,从小型的...
H2还提供兼容模式,可以兼容一些主 流的数据库,具有比较完备的数据库特性,如支client/server连接,能够支持标准的SQL语 句,支持存储过程等。因此采用H2作为开发期、测试期和演示的数据库非常方便,不太适合作为...
3. **独立服务器模式**:除了嵌入式模式,H2还提供了独立服务器模式,允许多用户通过网络访问数据库,这在需要多个客户端连接到同一数据库时非常有用。 4. **兼容性**:H2实现了SQL标准,因此对于熟悉SQL语法的...
《H2内存数据库h2部署操作手册.docx》是一个详细的部署指南,可能包括了如何在不同环境(如Windows、Linux)下部署H2,以及如何配置服务器模式以供多个客户端连接。 《H2数据库基础知识.docx》则可能再次强调了H2的...
- **嵌入式模式**(Embedded Mode):H2数据库以一个JAR文件的形式嵌入到应用程序中,可以直接通过Java代码连接。数据可以存储在磁盘上,关闭数据库后数据仍然存在。这是适合单应用环境的一个选择。 2. **iBatis**...
3. **多模式**:H2支持多种运行模式,如单用户模式、服务器模式、Web模式等,适应不同应用场景。 4. **跨平台**:由于H2是用Java编写的,因此可以在任何支持Java的平台上运行,包括Windows、Linux、Mac OS等。 5. ...
3. **多种模式**: H2支持多种数据库模式,包括单用户模式、多用户模式(TCP服务器)、内存模式(所有数据都存储在内存中)以及混合模式,这使得它能够适应不同的应用场景。 4. **兼容性**: H2数据库设计时充分考虑...
由于其轻量级和易于部署的特性,H2数据库常被用于快速原型开发、自动化测试和小型应用中。 此外,H2数据库的命令行界面(CLI)和Web界面(Web Console)也是其便捷性的一大体现。通过这些工具,用户可以直接在终端...
- 在H2中,可以创建多个模式,每个模式下可包含多个表,用于组织数据库结构。 - 模式可以看作是数据库的命名空间,提供了多租户支持。 5. **数据存储** - H2支持多种存储模式,如内存模式(数据仅存在于内存中)...