我在我的所有应用程序中都要调用以下类
@Component
public class GenericStoredProcedure
{
private final DataSource dataSource;
@Autowired
GenericStoredProcedure(DataSource dataSource){
this.dataSource = dataSource;
}
public Map executeSimpleProcedure(String procedureName, Map<String, Long> inParams, Map<String, RowMapperContainer> outParams)
{
InnerStoredProcedure innerStoredProcedure= new InnerStoredProcedure(dataSource, procedureName,inParams, outParams);
return innerStoredProcedure.executeProcedure(inParams);
}
/**************************************START OF INNER CLASS*************************************************/
/***********************************************************************************************************/
/**
* INNER CLASS 'InnerStoredProcedure' IS ONLY FOR THE USE OF OUTER CLASS GenericStoredProcedure
* @author mijaz
*
*/
private class InnerStoredProcedure extends StoredProcedure
{
public InnerStoredProcedure(DataSource ds, String SQL, Map<String, Long> inParams, Map<String, RowMapperContainer> outParams)
{
setDataSource(ds);
// setFunction(true);
setSql(SQL);
configerParameters(inParams, outParams);
compile();
}
/**
* Configure the input and output parameters for the stored procedure
* @param inParams
* @param outputParamers
*/
public void configerParameters(Map<String, Long> inParams, Map<String, RowMapperContainer>outputParamers)
{
if (inParams != null && inParams.size() > 0)
{
// entrySet()
Iterator<String> inParamsIterator = inParams.keySet().iterator();
while (inParamsIterator.hasNext())
{
String key = inParamsIterator.next();
declareParameter(new SqlParameter(key, Types.INTEGER));
}
}
Iterator <String> keySetIterator = outputParamers.keySet().iterator();
while (keySetIterator.hasNext())
{
String key = keySetIterator.next();
RowMapperContainer rowMapperContainer = (RowMapperContainer) outputParamers.get(key);
int sqlType = rowMapperContainer.getType();
if (sqlType == OracleTypes.CURSOR)
{
declareParameter(new SqlOutParameter(key,OracleTypes.CURSOR, rowMapperContainer.getRowMapper()));
}
else if (sqlType == OracleTypes.VARCHAR)
{
declareParameter(new SqlOutParameter(key,OracleTypes.VARCHAR));
}
else if (sqlType == OracleTypes.STRUCT)
{
declareParameter(new SqlOutParameter(key,OracleTypes.STRUCT,rowMapperContainer.getTypeName(), rowMapperContainer.getSqlReturnType()));
}
.....
}
}
public Map executeProcedure(Map inputs) {
return execute(inputs);
}
}
/**************************************END OF INNER CLASS 'InnerStoredProcedure'**************************/
/*********************************************************************************************************/
在所有DAO中,
@Component
public class ProductsDAO{
@Autowired
GenericStoredProcedure sp
public List<Item> methodXXX(int id){
.....
List items = sp.executeSimpleProcedure(procedure, in, mapper );
}
我知道GenericStoredProcedure是单例,但是在方法executeSimpleProcedure中创建了一个InnerStoredProcedure实例,这使得GenericStoredProcedure线程安全,对吗?
在Web应用程序中,创建InnerStoredProcedure实例是否最佳?