首页 > 代码库 > java中生成流水号的一个例子(使用BerkeleyDB)
java中生成流水号的一个例子(使用BerkeleyDB)
package com.jiaoyiping.berkeleydb;import com.sleepycat.je.*;import com.sleepycat.utilint.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.UnsupportedEncodingException;/** * Created with IntelliJ IDEA. * User: 焦一平 * Date: 14-7-9 * Time: 下午1:27 * To change this template use File | Settings | File Templates. * 1.直接存key和value * 2.存储key和value组合成的对象(未使用) */public class SerialNumberGenerator { Logger logger = LoggerFactory.getLogger(SerialNumberGenerator.class); private static int MAX_VALUE = http://www.mamicode.com/9999; private static int MIN_VALUE = http://www.mamicode.com/1; /** * 对于任何的请求,总是先取出当前的值返回,然后再将数据库里的值加一 * @param key 业务主键 * @return 当前值 */ public String getNextValue(String key){ String currentValue = this.readCurrentValueFromDatabase(key); String result; if (currentValue =http://www.mamicode.com/= null || "".equals(currentValue)){ result = this.formatString(MIN_VALUE); this.instrtIntoDataBase(key,(MIN_VALUE+1)+""); BerkeleyDBUtil.getDatabase().getEnvironment().sync(); } else{ int intCurrentValue =http://www.mamicode.com/ Integer.parseInt(currentValue); result = this.formatString(intCurrentValue); //存储下一个值 String nextValue = http://www.mamicode.com/""; if (intCurrentValue =http://www.mamicode.com/= MAX_VALUE){ nextValue = MIN_VALUE+""; }else { nextValue = (intCurrentValue+1) +""; } this.instrtIntoDataBase(key,nextValue); BerkeleyDBUtil.getDatabase().getEnvironment().sync(); } return result; } public String readCurrentValueFromDatabase(String key){ DatabaseEntry theKey = new DatabaseEntry(StringUtils.toUTF8(key)); DatabaseEntry theValue = new DatabaseEntry(); TransactionConfig txnConfig = new TransactionConfig(); txnConfig.setSerializableIsolationVoid(true); Database database = BerkeleyDBUtil.getDatabase(); //Transaction txn = database.getEnvironment().beginTransaction(null,txnConfig); OperationStatus status = database.get(null,theKey,theValue,LockMode.DEFAULT); //对应的键不存在 if (status == OperationStatus.KEYEMPTY){ return null; } else if (status == OperationStatus.SUCCESS){ byte[] data =http://www.mamicode.com/ theValue.getData(); String result; try { result = new String(data,"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. result = ""; } return result; } return null; } public void instrtIntoDataBase(String key,String value){ DatabaseEntry theKey = new DatabaseEntry(StringUtils.toUTF8(key)); DatabaseEntry theValue = new DatabaseEntry(StringUtils.toUTF8(value)); TransactionConfig txnConfig = new TransactionConfig(); txnConfig.setSerializableIsolationVoid(true); Database database = BerkeleyDBUtil.getDatabase(); //Transaction txn = database.getEnvironment().beginTransaction(null,txnConfig); OperationStatus status = database.put(null,theKey,theValue); if (status == OperationStatus.SUCCESS){ logger.info("保存成功"); }// else if (status == OperationStatus.KEYEXIST){// logger.info("");// } } public String formatString(int input){ String result = ""; if(input > 1000){ result = input +""; }else{ int length = (input+"").length(); if (length == 1){ result = "000"+input; } else if (length == 2){ result = "00"+input; }else { result = "0"+input; } } return result; } public static void main(String[] args) { SerialNumberGenerator generator = new SerialNumberGenerator(); String result = generator.getNextValue("jiao"); System.out.println("======"+result+"======"); }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。