我有一个Spring应用程序,其中托管了几个REST和SOAP WebServices。我的客户端的最新请求是执行一个存储过程,该存储过程接收多个参数和3个自定义数组。
它似乎工作得很好,但是在5次执行之后,我得到了以下错误:
超时:池空。无法在30秒内获取连接,没有可用的大小:5;忙:5;空闲:0;最后等待:30000。
我不明白为什么在使用这个特定特性时,连接不被发布。
我正在扩展Spring中的StoredProcedure
:
public class OracleArrayStoredProcedure extends StoredProcedure {
在此之后,我有一堆参数的最终字符串(超过50),然后我有了构造函数:
public OracleArrayStoredProcedure(DataSource ds) {
super(ds, PROC_NAME);
在构造函数中,我有参数,其中还包括数组:
declareParameter(new SqlParameter(PARAM1, OracleTypes.NUMBER));
// Arrays
declareParameter(new SqlInOutParameter(ARRAY1, OracleTypes.ARRAY, "ARRAY1"));
declareParameter(new SqlInOutParameter(ARRAY2, OracleTypes.ARRAY, "ARRAY2"));
declareParameter(new SqlInOutParameter(ARRAY3, OracleTypes.ARRAY, "ARRAY3"));
compile();
然后是执行,这就是我无法释放的连接的位置:
public ArrayResponse execute(ArrayRequest arrayRequest) {
ArrayResponse response = new ArrayResponse();
Map<String, Object> inputs = new HashMap<String, Object>();
try {
OracleConnection connection = getJdbcTemplate().getDataSource().getConnection()
.unwrap(OracleConnection.class);
// ARRAY1
ArrayDescriptor arrayFirstDescriptor = new ArrayDescriptor(ARRAY1, connection);
StructDescriptor recFirstDescriptor = new StructDescriptor("FIRSTREC", connection);
Object[] arrayFirstStruct = new Object[arrayRequest.getArray1().size()];
int i = 0;
for (Iterator<Array1> iterator = arrayRequest.getArray1().iterator(); iterator
.hasNext();) {
Model1 model1 = (Model1) iterator.next();
STRUCT s = new STRUCT(arrayFirstDescriptor, connection, new Object[] {
// Bunch of attributes
arrayStructArray[i++] = s;
}
ARRAY inStructArray = new ARRAY(arrayDescriptor, connection, array1Struct);
最后,我将数组和参数放在输入映射中,并执行它:
inputs.put(ARRAY1, inStructArray);
Map<String, Object> out = super.execute(inputs);
这种方法的问题是,我不能释放连接(即使我使用connection.close()),所以在5次执行之后,它就不再工作了。我做错了什么?
当我不需要使用STRUCT时,我就不需要使用
OracleConnection connection = getJdbcTemplate().getDataSource().getConnection()
.unwrap(OracleConnection.class);
所以一切都很正常。
发布于 2017-09-15 06:10:02
我能够通过实现AbstractSqlTypeValue()来修复它,它有一个传递DataSource连接的方法。这样,我就不用手动连接了。然后我简单地将structArray添加到输入映射中。
SqlTypeValue structArray = new AbstractSqlTypeValue() {
@Override
protected Object createTypeValue(Connection connection, int arg1, String arg2) throws SQLException {
Object[] modelArray = new Object[request.getArray().size()];
int i = 0;
for (Iterator<Model> iterator = request.getArray().iterator(); iterator.hasNext();) {
Model model = (Model) iterator.next();
Struct s = connection.createStruct("TEST_REC", new Object[] {
// All attributes go here
});
modelArray[i++] = s;
}
Array structArray = ((OracleConnection) connection).createOracleArray("TEST_STRUCT",
modelArray);
return structArray ;
}
};
https://stackoverflow.com/questions/46221422
复制