h2database源码浅析:集群
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源码浅析:集群的更多相关文章
- Dubbo 源码分析 - 集群容错之 LoadBalance
1.简介 LoadBalance 中文意思为负载均衡,它的职责是将网络请求,或者其他形式的负载"均摊"到不同的机器上.避免集群中部分服务器压力过大,而另一些服务器比较空闲的情况.通 ...
- Dubbo 源码分析 - 集群容错之 Cluster
1.简介 为了避免单点故障,现在的应用至少会部署在两台服务器上.对于一些负载比较高的服务,会部署更多台服务器.这样,同一环境下的服务提供者数量会大于1.对于服务消费者来说,同一环境下出现了多个服务提供 ...
- Dubbo 源码分析 - 集群容错之 Router
1. 简介 上一篇文章分析了集群容错的第一部分 -- 服务目录 Directory.服务目录在刷新 Invoker 列表的过程中,会通过 Router 进行服务路由.上一篇文章关于服务路由相关逻辑没有 ...
- Dubbo源码学习--集群负载均衡算法的实现
相关文章: Dubbo源码学习文章目录 前言 Dubbo 的定位是分布式服务框架,为了避免单点压力过大,服务的提供者通常部署多台,如何从服务提供者集群中选取一个进行调用, 就依赖Dubbo的负载均衡策 ...
- Dubbo 源码分析 - 集群容错之 Directory
1. 简介 前面文章分析了服务的导出与引用过程,从本篇文章开始,我将开始分析 Dubbo 集群容错方面的源码.这部分源码包含四个部分,分别是服务目录 Directory.服务路由 Router.集群 ...
- Dubbo源码(七) - 集群
前言 本文基于Dubbo2.6.x版本,中文注释版源码已上传github:xiaoguyu/dubbo 集群(cluster)就是一组计算机,它们作为一个总体向用户提供一组网络资源.这些单个的计算机系 ...
- h2database源码浅析:SQL语句的执行
最近想好好了解一下数据库的原理,下载了h2database的源码,准备好好看看.此过程的一些想法,暂且记下来,权当做读码笔记吧! 为了调试准备的测试用例: @Test public void test ...
- dubbo源码分析- 集群容错之Cluster(一)
1.集群容错的配置项 failover - 失败自动切换,当出现失败,重试其他服务器(缺省),通常用于读操作,但重试会带来更长的延时. failfast - 快速失效,只发起一次调用,失败立即报错.通 ...
- h2database源码浅析:锁与MVCC
Table Level Locking The database allows multiple concurrent connections to the same database. To mak ...
随机推荐
- 机器学习-----线性回归浅谈(Linear Regression)
Linear Regreesion 在现实生活中普遍存在着变量之间的关系,有确定的和非确定的.确定关系指的是变量之间可以使用函数关系式表示,还有一种是属于非确定的(相关),比如人的身 ...
- C#- 将秒数转化成任意时间格式
将秒数转化成任意时间格式,可以使用C#的一个函数TimeSpan,看示例: TimeSpan ts = new TimeSpan(0, 0, 3661); richTextBox2.Text = ts ...
- smarty3--registerPlugin()函数报错问题
smarty版本:smarty3.1.30 registerPlugin错误信息: Notice: Trying to get property of non-object in E:\Joomla\ ...
- 手把手教你入门mac idea
一.前沿 去年入职后, 公司有很多人使用的是idea , 而不是eclipse. 之前就想转向idea, 但一直没转过来~~原因是团队的人提倡用开源eclipse.现在下定决心转向idea. 虽然, ...
- PostgreSQL的prepare 和 execute 动作背后
我给PostgreSQL的源代码加入了调试信息以后,会有如下表现: 我执行Prepare: postgres=# prepare s(; PREPARE postgres=# 背后的反应: ** In ...
- C#WinForm应用程序实现自动填充网页上的用户名和密码并点击登录按钮【转载】
使用WebBrowser控件,在documentComplete事件处理器里写 HtmlElement name = webBrowser1.Document.GetElementById(" ...
- ZOJ 3829 Known Notation 贪心
Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...
- rk3288的SDK修复cm3218光敏驱动bug
瑞芯的Android 4.4的SDK中kernel的补丁例如以下: diff --git a/arch/arm/boot/dts/rk3288-tb_8846.dts b/arch/arm/boot/ ...
- WaterWave
WaterWave.rar
- Qualcomm Web Site For Android Development
https://www.codeaurora.org/xwiki/bin/QAEP/release https://support.cdmatech.com/login/ https://chipco ...