com.bixuebihui.sequence.SequenceUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of c-dbtools Show documentation
Show all versions of c-dbtools Show documentation
a fast small database connection pool and a active record flavor mini framework
package com.bixuebihui.sequence;
import com.bixuebihui.jdbc.IDbHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-2 15:21:30
* Note: Java实现的Sequence工具
*
* @author xingwx
* @version $Id: $Id
*/
public class SequenceUtils {
private static final SequenceUtils INSTANCE = new SequenceUtils();
private final Map keyMap = new Hashtable<>(20); // Sequence载体容器
private static final int POOL_SIZE = 10; // Sequence值缓存大小
private static final Logger LOG = LoggerFactory.getLogger(SequenceUtils.class);
/**
* 禁止外部实例化
*/
private SequenceUtils() {
}
/**
* 获取SequenceUtils的单例对象
*
* @return SequenceUtils的单例对象
*/
public static SequenceUtils getInstance() {
return INSTANCE;
}
/**
* 获取下一个Sequence键值
*
* @param keyName
* Sequence名称
* @param dbHelper 数据库连接
* @return 下一个Sequence键值
*/
public synchronized long getNextKeyValue(String keyName, IDbHelper dbHelper) {
try {
return getOrCreateKeyInfo(keyName, dbHelper).getNextKey();
} catch (SQLException e) {
LOG.error("",e);
}
return 0;
}
/**
* moveKeyValueToCurrent.
*
* @param keyName a {@link java.lang.String} object.
* @param max a {@link java.lang.Long} object.
* @param dbHelper a {@link IDbHelper} object.
*/
public synchronized void moveKeyValueToCurrent(String keyName, Long max, IDbHelper dbHelper) {
try {
getOrCreateKeyInfo(keyName, dbHelper).moveTo(max);
} catch (SQLException e) {
LOG.error("",e);
}
}
private KeyInfo getOrCreateKeyInfo(String keyName, IDbHelper dbHelper) throws SQLException {
KeyInfo keyInfo;
if (keyMap.containsKey(keyName)) {
keyInfo = keyMap.get(keyName);
} else {
keyInfo = new KeyInfo(keyName, POOL_SIZE, dbHelper);
keyMap.put(keyName, keyInfo);
}
return keyInfo;
}
}