重新从数据库取出序号是在服务器重启时从数据库中取出最大的流水号,并初始化静态变量,数据库记录如: regid ...(其它列略) xh_index xh_year ls200908000001RBrb 1 2009 ls200908000002RBrb 2 2009在服务器启动后第一次取流水号是要从数据库中取,执行select top 1 xh_index from cx_reg where xh_year='2009' order by xh_index desc 把得到的最大流水号取出并赋给静态变量xh_index,以后再生成时就直接makeSequence()把xh_index自增1作为新的序列号,并组合成regid,并把这个序号同时保存到此条记录中! 现在的问题是在以后的操作中仍然有生成适于重复的xh_index至数据库记录中
public class Count{ int n; static Count instance; private Count(){ n = 0; } public static Count getInstance(){ if(instance == null){ instance = new Count(); } return instance; } public synchronized int getN(){ n++; return n; } }
只有在服务器刚启动时才从数据库中取最大值,其它时候都是从内存中的static变量中取到的呀!
regid ...(其它列略) xh_index xh_year
ls200908000001RBrb 1 2009
ls200908000002RBrb 2 2009在服务器启动后第一次取流水号是要从数据库中取,执行select top 1 xh_index from cx_reg where xh_year='2009' order by xh_index desc
把得到的最大流水号取出并赋给静态变量xh_index,以后再生成时就直接makeSequence()把xh_index自增1作为新的序列号,并组合成regid,并把这个序号同时保存到此条记录中!
现在的问题是在以后的操作中仍然有生成适于重复的xh_index至数据库记录中
需要有6位的序列号,而序列为是1,就在前面补5个0就是6位,10的话前面就补4个0,依此类推,再加上前缀后缀就是完整的流水号喽,而且序列是每年归一次0,
完整的流水号和序列在数据库记录中分别有保存
如果一个流水号为ls200908000001rb则对应的数据库记录中regid(完整流水号)保存的就是ls200908000001rb,xh_index(序列)字段保存的为1,xh_year(年份)保存的为2009,说清楚了吗?!
int n;
static Count instance;
private Count(){
n = 0;
}
public static Count getInstance(){
if(instance == null){
instance = new Count();
}
return instance;
}
public synchronized int getN(){
n++;
return n;
}
}