原理:此方式MyCAT將sequence配置到文件中,當使用到sequence中的配置後,MyCAT會更下classpath中的sequence_conf.properties文件中
sequence當前的值。
配置方式:
在sequence_conf.properties文件中做如下配置:
#default global sequence
GLOBAL.HISIDS=
#最小值
GLOBAL.MINID=10001
#最大值
GLOBAL.MAXID=20000
#當前值
GLOBAL.CURID=10000
<!-- sequnceHandlerType=0表示:使用本地文件的方式配置mycat的全局序列號,對應sequence_conf.properties文件 sequnceHandlerType=1表示:使用數據庫表的方式配置mycat的全局序列號 --> <property name="sequnceHandlerType">0</property>
測試:
<!-- sequnceHandlerType=0表示:使用本地文件的方式配置mycat的全局序列號,對應sequence_conf.properties文件 sequnceHandlerType=1表示:使用數據庫表的方式配置mycat的全局序列號 --> <property name="sequnceHandlerType">1</property>
數據庫配置:
1) 創建MYCAT_SEQUENCE表
– 創建存放sequence的表
DROP TABLE IF EXISTS MYCAT_SEQUENCE;
– name sequence名稱
– current_value 當前value
– increment 增長步長! 可理解為mycat在數據庫中一次讀取多少個sequence. 當這些用完後, 下次再從數據庫中讀取.
/*創建存放sequence的表*/ CREATE TABLE MYCAT_SEQUENCE ( NAME VARCHAR (50), current_value INT NOT NULL, increment INT NOT NULL DEFAULT 100, PRIMARY KEY (NAME) ) ENGINE = INNODB;
INSERT INTO MYCAT_SEQUENCE ( NAME, current_value, increment ) VALUES ('GLOBAL', 100000, 100);
-- ---------------------------- -- 函數結構: `mycat_seq_currval` -- 獲取當前sequence的值(返回當前值,增量) -- ---------------------------- DROP FUNCTION IF EXISTS mycat_seq_currval; CREATE FUNCTION `mycat_seq_currval`(seq_name VARCHAR(50)) RETURNS varchar(64) CHARSET latin1 DETERMINISTIC BEGIN DECLARE retval VARCHAR(64); SET retval="-1,0"; SELECT concat(CAST(current_value AS CHAR),",",CAST(increment AS CHAR) ) INTO retval FROM MYCAT_SEQUENCE WHERE name = seq_name; RETURN retval ; END
-- ---------------------------- -- 函數結構: `mycat_seq_setval` -- 設置sequence的值 -- ---------------------------- DROP FUNCTION IF EXISTS mycat_seq_setval; CREATE FUNCTION `mycat_seq_setval`(seq_name VARCHAR(50), value INTEGER) RETURNS varchar(64) CHARSET latin1 DETERMINISTIC BEGIN DECLARE retval VARCHAR(64); DECLARE inc INT; SET inc = 0; SELECT increment INTO inc FROM MYCAT_SEQUENCE WHERE name = seq_name; UPDATE MYCAT_SEQUENCE SET current_value = value WHERE name = seq_name; SELECT concat(CAST(value as CHAR),",",CAST(inc as CHAR)) INTO retval; RETURN retval; END
-- ---------------------------- -- 函數結構: `mycat_seq_nextval` -- 獲取下一個sequence的值 -- ---------------------------- DROP FUNCTION IF EXISTS mycat_seq_nextval; CREATE FUNCTION `mycat_seq_nextval`(seq_name VARCHAR(50)) RETURNS varchar(64) CHARSET latin1 DETERMINISTIC BEGIN DECLARE retval VARCHAR(64); DECLARE val BIGINT; DECLARE inc INT; DECLARE seq_lock INT; set val = -1; set inc = 0; SET seq_lock = -1; SELECT GET_LOCK(seq_name, 15) into seq_lock; if seq_lock = 1 then SELECT current_value + increment, increment INTO val, inc FROM MYCAT_SEQUENCE WHERE name = seq_name for update; if val != -1 then UPDATE MYCAT_SEQUENCE SET current_value = val WHERE name = seq_name; end if; SELECT RELEASE_LOCK(seq_name) into seq_lock; end if; SELECT concat(CAST((val - inc + 1) as CHAR),",",CAST(inc as CHAR)) INTO retval; RETURN retval; END
注意:MYCAT_SEQUENCE表和以上的3個function,需要放在同一個節點上。function請直接在具體節點的數據庫上執行
測試:
insert into travelrecord(id,name)values(next value for MYCATSEQ_GLOBAL,'hexmith');
insert into travelrecord(id,name)values(next value for MYCATSEQ_GLOBAL,'Mycat');
在前面的測試中,我添加到MYCAT_SEQUENCE表中的數據設置的步長是100,此處吻合。