Clustering / High Availability

This database supports a simple clustering / high availability mechanism. The architecture is: two database servers run on two different computers, and on both computers is a copy of the same database. If both servers run, each database operation is executed on both computers. If one server fails (power, hardware or network failure), the other server can still continue to work. From this point on, the operations will be executed only on one server until the other server is back up.

Clustering can only be used in the server mode (the embedded mode does not support clustering). The cluster can be re-created using the CreateCluster tool without stopping the remaining server. Applications that are still connected are automatically disconnected, however when appending;AUTO_RECONNECT=TRUE, they will recover from that.

To initialize the cluster, use the following steps:

  • Create a database
  • Use the CreateCluster tool to copy the database to another location and initialize the clustering. Afterwards, you have two databases containing the same data.
  • Start two servers (one for each copy of the database)
  • You are now ready to connect to the databases with the client application(s)

Using the CreateCluster Tool

To understand how clustering works, please try out the following example. In this example, the two databases reside on the same computer, but usually, the databases will be on different servers.

  • Create two directories: server1, server2. Each directory will simulate a directory on a computer.
  • Start a TCP server pointing to the first directory. You can do this using the command line:
    java org.h2.tools.Server
    -tcp -tcpPort 9101
    -baseDir server1
  • Start a second TCP server pointing to the second directory. This will simulate a server running on a second (redundant) computer. You can do this using the command line:
    java org.h2.tools.Server
    -tcp -tcpPort 9102
    -baseDir server2
  • Use the CreateCluster tool to initialize clustering. This will automatically create a new, empty database if it does not exist. Run the tool on the command line:
    java org.h2.tools.CreateCluster
    -urlSource jdbc:h2:tcp://localhost:9101/~/test
    -urlTarget jdbc:h2:tcp://localhost:9102/~/test
    -user sa
    -serverList localhost:9101,localhost:9102
  • You can now connect to the databases using an application or the H2 Console using the JDBC URL jdbc:h2:tcp://localhost:9101,localhost:9102/~/test
  • If you stop a server (by killing the process), you will notice that the other machine continues to work, and therefore the database is still accessible.
  • To restore the cluster, you first need to delete the database that failed, then restart the server that was stopped, and re-run the CreateCluster tool.

Detect Which Cluster Instances are Running

To find out which cluster nodes are currently running, execute the following SQL statement:

SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE NAME='CLUSTER'

If the result is '' (two single quotes), then the cluster mode is disabled. Otherwise, the list of servers is returned, enclosed in single quote. Example: 'server1:9191,server2:9191'.

It is also possible to get the list of servers by using Connection.getClientInfo().

The property list returned from getClientInfo() contains a numServers property that returns the number of servers that are in the connection list. To get the actual servers, getClientInfo() also has properties server0..serverX, where serverX is the number of servers minus 1.

Example: To get the 2nd server in the connection list one uses getClientInfo('server1')Note: TheserverX property only returns IP addresses and ports and not hostnames.

Clustering Algorithm and Limitations

Read-only queries are only executed against the first cluster node, but all other statements are executed against all nodes. There is currently no load balancing made to avoid problems with transactions. The following functions may yield different results on different cluster nodes and must be executed with care: RANDOM_UUID(), SECURE_RAND(), SESSION_ID(), MEMORY_FREE(), MEMORY_USED(), CSVREAD(), CSVWRITE(), RAND() [when not using a seed]. Those functions should not be used directly in modifying statements (for example INSERT, UPDATE, MERGE). However, they can be used in read-only statements and the result can then be used for modifying statements. Using auto-increment and identity columns is currently not supported. Instead, sequence values need to be manually requested and then used to insert data (using two statements).

When using the cluster modes, result sets are read fully in memory by the client, so that there is no problem if the server dies that executed the query. Result sets must fit in memory on the client side.

The SQL statement SET AUTOCOMMIT FALSE is not supported in the cluster mode. To disable autocommit, the method Connection.setAutoCommit(false) needs to be called.

It is possible that a transaction from one connection overtakes a transaction from a different connection. Depending on the operations, this might result in different results, for example when conditionally incrementing a value in a row.

h2database源码浅析:集群的更多相关文章

  1. Dubbo 源码分析 - 集群容错之 LoadBalance

    1.简介 LoadBalance 中文意思为负载均衡,它的职责是将网络请求,或者其他形式的负载"均摊"到不同的机器上.避免集群中部分服务器压力过大,而另一些服务器比较空闲的情况.通 ...

  2. Dubbo 源码分析 - 集群容错之 Cluster

    1.简介 为了避免单点故障,现在的应用至少会部署在两台服务器上.对于一些负载比较高的服务,会部署更多台服务器.这样,同一环境下的服务提供者数量会大于1.对于服务消费者来说,同一环境下出现了多个服务提供 ...

  3. Dubbo 源码分析 - 集群容错之 Router

    1. 简介 上一篇文章分析了集群容错的第一部分 -- 服务目录 Directory.服务目录在刷新 Invoker 列表的过程中,会通过 Router 进行服务路由.上一篇文章关于服务路由相关逻辑没有 ...

  4. Dubbo源码学习--集群负载均衡算法的实现

    相关文章: Dubbo源码学习文章目录 前言 Dubbo 的定位是分布式服务框架,为了避免单点压力过大,服务的提供者通常部署多台,如何从服务提供者集群中选取一个进行调用, 就依赖Dubbo的负载均衡策 ...

  5. Dubbo 源码分析 - 集群容错之 Directory

    1. 简介 前面文章分析了服务的导出与引用过程,从本篇文章开始,我将开始分析 Dubbo 集群容错方面的源码.这部分源码包含四个部分,分别是服务目录 Directory.服务路由 Router.集群 ...

  6. Dubbo源码(七) - 集群

    前言 本文基于Dubbo2.6.x版本,中文注释版源码已上传github:xiaoguyu/dubbo 集群(cluster)就是一组计算机,它们作为一个总体向用户提供一组网络资源.这些单个的计算机系 ...

  7. h2database源码浅析:SQL语句的执行

    最近想好好了解一下数据库的原理,下载了h2database的源码,准备好好看看.此过程的一些想法,暂且记下来,权当做读码笔记吧! 为了调试准备的测试用例: @Test public void test ...

  8. dubbo源码分析- 集群容错之Cluster(一)

    1.集群容错的配置项 failover - 失败自动切换,当出现失败,重试其他服务器(缺省),通常用于读操作,但重试会带来更长的延时. failfast - 快速失效,只发起一次调用,失败立即报错.通 ...

  9. h2database源码浅析:锁与MVCC

    Table Level Locking The database allows multiple concurrent connections to the same database. To mak ...

随机推荐

  1. HDU5781--ATM Mechine(概率dp)

    题意:Alice忘记了自己银行里存了多少钱,只记得在[0,k]之间.每次取钱如果余额足够就出钱,否则警告一次,警告超过w次就会把你抓起来,在不想被警察抓起来的前提下,Alice采取最优策略,求期望取钱 ...

  2. 第十一章、认识与学习 BASH 管线命令 (pipe)

    管线命令使用『 | 』界定符号 [root@www ~]# ls -al /etc | less 管线命令『 | 』仅能处理经由前面一个命令传来的正确信息,也就是 standard output 的信 ...

  3. [二]Json-lib的用法

    1.Json字符串 PrintWriter out=response.getWriter(); // String resultJson="{\"name\":\&quo ...

  4. .NET ORM框架(一)

    最近做项目自己整理了一个ORM框架,分享给大家看看,有很多不足望大家指出. 下面是使用方法 BLL 主要方法 逻辑层:子类继承父类, 直接用BASE调用 ManagerBLL 中的方法. public ...

  5. 爬取知乎百万信息之UrlTask

    这个模块的作用是从nexturl队列获取用户的关注列表的url,获取关注列表.服务器返回的Json的数据 封装一个对象的序列化和反序列化的类 public class SerializeHelper ...

  6. session 重写进入redis测试

    在实际业务中,当session存储过多 或者操作频繁,业务逐渐扩展的时候,文件存储已经无法满足session操作速度和需求,可以考虑用数据库或者nosql的redis来存储session,本文讲解如何 ...

  7. 单独批次性任务采用MySQL定时器解决需求

    有时候我们在开发的时候会遇到一些需求是在某个固定的时间段实现某些特殊功能,只做一次或者有规律的每分钟一次每小时一次,那么这个时候我们可以启用MySQL的定时器来帮忙解决该问题. 比如,有一个场景是要求 ...

  8. rank() over(partition)的使用

    有的时候会遇到这样的问题,我们需要查询一张表,而且要按照业务排序,比如我需要如下的结果: 地区   日期    费用  产品编号   用户编号 290 201202 258 1             ...

  9. PowerDesigner 15 概述

    PowerDesigner 15 概述 数据结构数据库powerbuildersybasemicrosoftuml   目录(?)[+]   一. PowerDesigner 介绍 PowerDesi ...

  10. 【转】Netty那点事(二)Netty中的buffer

    [原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch2-buffer.md 上一篇文章我们概要介绍了Netty的原 ...