import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
import com.huawei.widget.commons.dao.DBException;
import com.huawei.widget.commons.dao.spi.SequenceGenerator;
import com.huawei.widget.commons.exception.ErrorCode;
/**
* 基于spring框架,以bean形式配置实例,用于获取oracle上已存在sequence的递增值。
*
* @author g00106664
* @version C02 2009-5-14
* @since OpenEye WIDGET_SRV V100R001C02
*/
public class OracleSequenceGeneratorSpringImpl implements SequenceGenerator
{
/**
* 自增器缓存容器
*/
private Map<String, OracleSequenceMaxValueIncrementer> incrementerMap = null;
/**
* 默认构造函数
*/
public OracleSequenceGeneratorSpringImpl()
{
}
/*
* (非 Javadoc)
*
* @see com.huawei.widget.commons.dao.spi.SequenceGenerator#init(javax.sql.DataSource,
* java.util.List)
*/
public void init(DataSource dataSource, List<String> sequenceNames)
{
incrementerMap = Collections
.synchronizedMap(new HashMap<String, OracleSequenceMaxValueIncrementer>());
if (sequenceNames != null)
{
for (String sequenceName : sequenceNames)
{
incrementerMap.put(sequenceName,
new OracleSequenceMaxValueIncrementer(dataSource,
sequenceName));
}
}
}
/*
* (非 Javadoc)
*
* @see com.huawei.widget.commons.dao.spi.SequenceGenerator#nextIntValue(java.lang.String)
*/
public int nextIntValue(String sequenceName) throws DBException
{
return getIncrementer(sequenceName).nextIntValue();
}
/*
* (非 Javadoc)
*
* @see com.huawei.widget.commons.dao.spi.SequenceGenerator#nextLongValue(java.lang.String)
*/
public long nextLongValue(String sequenceName) throws DBException
{
return getIncrementer(sequenceName).nextLongValue();
}
/*
* (非 Javadoc)
*
* @see com.huawei.widget.commons.dao.spi.SequenceGenerator#nextStringValue(java.lang.String)
*/
public String nextStringValue(String sequenceName) throws DBException
{
return getIncrementer(sequenceName).nextStringValue();
}
private OracleSequenceMaxValueIncrementer getIncrementer(String sequenceName)
throws DBException
{
OracleSequenceMaxValueIncrementer incrementer = incrementerMap
.get(sequenceName);
if (null == incrementer)
{
throw new DBException(ErrorCode.COMMON_GET_SEQUENCE_UNIKEY_FAIL,
"The incrementer of sequence:" + sequenceName
+ " is not initialized.");
}
return incrementer;
}
}