内存数据库H2的Clustering模式数据恢复

H2 is written in Java, in-memory HSQL databases, can be embedded in Java applications or run in the client-server mode. 

 

H2 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, but maybe oneday in the future, the 'mixed clustering mode' will add this to the feature request list; it should be quite easy to implement.). It is possible to restore the cluster without stopping the server, however it is critical that no other application is changing the data in the first database while the second database is restored, so restoring the cluster is currently a manual process.

 

Suppose two server: 

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

server2: java -cp "h2.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.Server -tcp -tcpPort 9102 -baseDir server2

 

Createcluster1

 java -cp "h2.jar;%H2DRIVERS%;%CLASSPATH%" 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

 

Createcluster2

 java -cp "h2.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.CreateCluster -urlSource jdbc:h2:tcp://localhost:9102/./test -urlTarget jdbc:h2:tcp://localhost:9101/./test  -user sa -serverlist localhost:9101,localhost:9102

 

In this mode, Read-only queries(such as select) are only executed against the first cluster node - server1(9101), 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, or MERGE). However, they can be used in read-only statements and the result can then be used for modifying statements.

 

Data restore:

If server2 crash, insert/update/delete statements are only submitted to server1 without exception. we should delete server2's db file first, and then restart server2 mannually, and then redo Createcluster1 command, H2 will do data synchronization automatically (from server1 to server2), and after that, insert/update/delete statements will be executed against server1 and server2.

 

If server1 crash, we should delete server1's db file first, and then restart server1, then do another Createcluster2 command instead of Createcluster1 , otherwise the exception(target db's file should be empty) will be thrown, that's to say, H2's culstering data synchronization is baed on -urlSource and -urlTarget, and the target db file must be empty. But in this way, the select queries will be executed against server2, not server1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值