日期:2014-05-20 浏览次数:21013 次
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
/**
*
* <b>Note</b>: Java实现的Sequence工具
*/
public class SequenceUtil {
/**
* 单例模式
*/
private static SequenceUtil instance = new SequenceUtil();
/**
* 存放序列的MAP
*/
private Map<String, KeyInfo> keyMap = new HashMap<String, KeyInfo>(20);
private static final int POOL_SIZE = 1;
/**
* 防止外部实例化
*/
private SequenceUtil() {
}
/**
* 单例模式,获取单例
*
* @return SequenceUtils的单例对象
*/
public static SequenceUtil getInstance() {
return instance;
}
/**
* 获取下一个sequence值
*
* @param keyName
* Sequence名称
* @return 下一个Sequence键值
*/
public synchronized int getNextKeyValue(String keyName) {
KeyInfo keyInfo = null;
Integer keyObject = null;
try {
if (keyMap.containsKey(keyName)) {
keyInfo = keyMap.get(keyName);
} else {
keyInfo = new KeyInfo(keyName, POOL_SIZE);
keyMap.put(keyName, keyInfo);
}
keyObject = keyInfo.getNextKey();
} catch (SQLException e) {
e.printStackTrace();