【发布时间】:2017-09-14 14:11:30
【问题描述】:
我有一个 Spring Boot 应用程序,我在其中托管了多个 REST 和 SOAP WebServices。我客户的最新请求是执行一个存储过程,该过程接收多个参数和 3 个自定义数组。
它似乎工作正常,但在执行 5 次后,我收到以下错误:
超时:池为空。无法在 30 秒内获取连接,无可用 [size: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);
所以一切正常。
【问题讨论】:
-
它们没有被释放,因为它们不受 Spring 控制。您正在自己打开/请求新的连接,因此应该自己关闭它们。基本上,如果你开始做
getJdbcTemplate().getDataSource().getConnection()之类的事情,那么请从你的键盘上退后并重新考虑。而是使用 Spring 中的ConnectionCallback。 -
这是我能找到的唯一可行的例子。你能指出我正确的方向吗?我想把它做好
-
我已经为您指明了正确的方向,请使用
ConnectionCallback。
标签: java oracle spring-boot struct java-stored-procedures