seqName.trim().length() == 0) {
throw new SequenceException("[" + seqName + "] the sequenceName is null");
}
if (sequenceBo == null) {
throw new SequenceException("[" + seqName + "] the sequenceBo is null");
}
if (!sequenceBo.validate()){
throw new SequenceException("[" + seqName + "] the sequenceBo validate fail. BO:"+sequenceBo);
}
// 初始化该sequence
try {
initSequenceRecord(sequenceBo);
} catch (SequenceException e) {
throw e;
}
isInitialize = true;
}
/**
* <p> 获取下一个序列号 </p>
* @Title getNextVal
* @param
* @return long
* @author coderzl
*/
public long getNextVal(){
if(isInitialize == false){
throw new SequenceException("[" + seqName + "] the MysqlSequenceHolder not inited");
}
if(sequenceRange == null){
throw new SequenceException("[" + seqName + "] the sequenceRange is null");
}
long curValue = sequenceRange.getAndIncrement();
if(curValue == -1){
try{
lock.lock();
curValue = sequenceRange.getAndIncrement();
if(curValue != -1){
return curValue;
}
sequenceRange = retryRange();
curValue = sequenceRange.getAndIncrement();
}finally {
lock.unlock();
}
}
return curValue;
}
/**
* <p> 初始化当前这条记录 </p>
* @Title initSequenceRecord
* @Description
* @param sequenceBo
* @return void
* @author coderzl
*/
private void initSequenceRecord(MysqlSequenceBo sequenceBo){
//在限定次数内,乐观锁更新数据库记录
for(int i = 1; i < initRetryNum; i++){
//查询bo
MysqlSequenceBo curBo = sequenceDAO.getSequence(sequenceBo.getSeqName());
if(curBo == null){
throw new SequenceException("[" + seqName + "] the current sequenceBo is null");
}
if (!curBo.validate()){
throw new SequenceException("[" + seqName + "] the current sequenceBo validate fail");
}
//改变当前值
long newValue = curBo.getSeqValue()+curBo.getStep();
//检查当前值
if(!checkCurrentValue(newValue,curBo)){
newValue = resetCurrentValue(curBo);
}
int result = sequenceDAO.updSequence(sequenceBo.getSeqName(),curBo.getSeqValue(),newValue);
if(result > 0){
sequenceRange = new SequenceRange(curBo.getSeqValue(),newValue - 1);
curBo.setSeqValue(newValue);
this.sequenceBo = curBo;
return;
}else{
continue;
}
}
//限定次数内,更新失败,抛出异常
throw new SequenceException("[" + seqName + "] sequenceBo update error");
}
/**
* <p> 检查新值是否合法 新的当前值是否在最大最小值之间</p>
* @param curValue
* @param curBo
* @return boolean
* @author coderzl
*/
private boolean checkCurrentValue(long curValue,MysqlSequenceBo curBo){
if(curValue > curBo.getMinValue() && curValue <= curBo.getMaxValue()){
return true;
}
return false;
}
/**
* <p> 重置sequence当前值 :当前sequence达到最大值时,重新从最小值开始 </p>
* @Title resetCurrentValue
* @param curBo
* @return long
* @author coderzl
*/
private long resetCurrentValue(MysqlSequenceBo curBo){
return curBo.getMinValue();
}
/**
* <p> 缓存区间使用完毕时,重新读取数据库记录,缓存新序列段 </p>
* @Title retryRange
* @param SequenceRange
* @author coderzl
*/
private SequenceRange retryRange(){
for(int i = 1; i < getRetryNum; i++){
//查询bo
MysqlSequenceBo curBo = sequenceDAO.getSequence(sequenceBo.getSeqName());
if(curBo == null){
throw new SequenceException("[" + seqName + "] the current sequenceBo is null");
}
if (!curBo.validate()){
throw new SequenceException("[" + seqName + "] the current sequenceBo validate fail");
}
//改变当前值
long newValue = curBo.getSeqValue()+curBo.getStep();
//检查当前值
if(!checkCurrentValue(newValue,curBo)){
newValue = resetCurrentValue(curBo);
}
int result = sequenceDAO.updSequence(sequenceBo.getSeqName(),curBo.getSeqValue(),newValue);
if(result > 0){
sequenceRange = new SequenceRange(curBo.getSeqValue(),newValue - 1);
curBo.setSeqValue(newValue);
this.sequenceBo = curBo;
return sequenceRange;
}else{
continue;
}
}
throw new SequenceException("[" + seqName + "] sequenceBo update error");
}
}
总结
?当服务重启或异常的时候,会丢失当前服务所缓存且未用完的序列
?分布式场景,多个服务同时初始化,或者重新获取sequence时,乐观锁会保证彼此不冲突。A服务获取0-99,B服务会获取100-199,以此类推
?当该sequence获取较为频繁时,增大step值,能提升性能。但同时服务异常时,损失的序列也较多
?修改数据库里sequence的一些属性值,比如step,max等,再下一次从数据库获取时,会启用新的参数
?sequence只是提供了有限个序列号(最多max-min个),达到max后,会循环从头开始。
?由于sequence会循环,所以达到max后,再获取,就不会唯一。建议使用sequence来做业务流水号时,拼接时间。如:20170612235101+序列号
业务id拼接方法
@Service
public class JrnGeneratorService {
private static final String SEQ_NAME = "T_SEQ_TEST";
/** sequence服务 */
@Autowired
private MySqlSequence mySqlSequence;
public String generateJrn() {
try {
String sequence = mySqlSequence.getNextValue(SEQ_NAME);
sequence = leftPadding(sequence,8);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String nowdate = sDateFormat.format(calendar.getTime());
nowdate.substring(4, nowdate.length());
String jrn = nowdate + sequence + RandomUtil.getFixedLengthRandom(6);//10位时间+8位序列 + 6位随机数=24位流水号
return jrn;
} catch (Exception e) {
//TODO
}
}
private String leftPadding(String seq,int len){
String res ="";
String str ="";
if(seq.length()<len){
for(int i=0;i<len-seq.length();i++){
str +="0";
}
}
res =str+seq;
return res;
}
}
以上就是Mysql中的Sequence实现方法实例详解的详细内容,更多请关注php中文网其它相关文章!
学习教程快速掌握从入门到精通的SQL知识。
……