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 = 9999;
    private static int MIN_VALUE = 1;

    /**
     * 对于任何的请求,总是先取出当前的值返回,然后再将数据库里的值加一
     * @param key 业务主键
     * @return 当前值
     */
    public String getNextValue(String key){
        String currentValue = this.readCurrentValueFromDatabase(key);
        String result;
        if (currentValue == null || "".equals(currentValue)){
            result = this.formatString(MIN_VALUE);
            this.instrtIntoDataBase(key,(MIN_VALUE+1)+"");
            BerkeleyDBUtil.getDatabase().getEnvironment().sync();

        }
        else{
            int intCurrentValue = Integer.parseInt(currentValue);
            result = this.formatString(intCurrentValue);

            //存储下一个值
            String nextValue = "";
            if (intCurrentValue == 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 = 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+"======");
    }
}

 

java中生成流水号的一个例子(使用BerkeleyDB),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。