码迷,mamicode.com
首页 > 数据库 > 详细

java中生成流水号的一个例子(使用BerkeleyDB)

时间:2014-07-11 19:29:26      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   使用   数据   

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),布布扣,bubuko.com

java中生成流水号的一个例子(使用BerkeleyDB)

标签:style   blog   java   color   使用   数据   

原文地址:http://www.cnblogs.com/jiaoyiping/p/3833551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!