Skip to content

Commit ae0f310

Browse files
committed
CAY-2913 Suspected connection leak on SQLTemplateProcessor exceptions
porting to master
1 parent 470f960 commit ae0f310

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*****************************************************************
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
****************************************************************/
19+
20+
package org.apache.cayenne.access;
21+
22+
import org.apache.cayenne.CayenneRuntimeException;
23+
import org.apache.cayenne.datasource.UnmanagedPoolingDataSource;
24+
import org.apache.cayenne.di.Inject;
25+
import org.apache.cayenne.query.SQLAction;
26+
import org.apache.cayenne.query.SQLActionVisitor;
27+
import org.apache.cayenne.query.SQLSelect;
28+
import org.apache.cayenne.unit.di.runtime.CayenneProjects;
29+
import org.apache.cayenne.unit.di.runtime.RuntimeCase;
30+
import org.apache.cayenne.unit.di.runtime.RuntimeCaseDataSourceFactory;
31+
import org.apache.cayenne.unit.di.runtime.UseCayenneRuntime;
32+
import org.junit.Test;
33+
34+
import java.lang.reflect.Method;
35+
import java.util.Collections;
36+
37+
import static org.junit.Assert.assertEquals;
38+
import static org.junit.Assert.assertThrows;
39+
40+
@UseCayenneRuntime(CayenneProjects.TESTMAP_PROJECT)
41+
public class DataNodeQueryExceptionsIT extends RuntimeCase {
42+
43+
@Inject
44+
protected DataNode node;
45+
46+
@Inject
47+
protected RuntimeCaseDataSourceFactory dataSourceFactory;
48+
49+
@Test
50+
public void testQueryException() {
51+
SQLSelect<Object> throwingSS = new SQLSelect<Object>(Object.class, "SELECT 1") {
52+
@Override
53+
public SQLAction createSQLAction(SQLActionVisitor visitor) {
54+
assertEquals(1, activeConnections());
55+
throw new CayenneRuntimeException("Emulated exception");
56+
}
57+
};
58+
59+
assertEquals(0, activeConnections());
60+
assertThrows(CayenneRuntimeException.class, () -> node.performQueries(
61+
Collections.singletonList(throwingSS),
62+
new MockOperationObserver()));
63+
64+
assertEquals(0, activeConnections());
65+
}
66+
67+
@Test
68+
public void testQueryError() {
69+
SQLSelect<Object> throwingSS = new SQLSelect<Object>(Object.class, "SELECT 1") {
70+
@Override
71+
public SQLAction createSQLAction(SQLActionVisitor visitor) {
72+
assertEquals(1, activeConnections());
73+
throw new Error("Emulated error");
74+
}
75+
};
76+
77+
assertEquals(0, activeConnections());
78+
assertThrows(Error.class, () -> node.performQueries(
79+
Collections.singletonList(throwingSS),
80+
new MockOperationObserver()));
81+
82+
assertEquals(0, activeConnections());
83+
}
84+
85+
int activeConnections() {
86+
try {
87+
UnmanagedPoolingDataSource ds = dataSourceFactory.getSharedDataSource().unwrap(UnmanagedPoolingDataSource.class);
88+
89+
Method poolSize = UnmanagedPoolingDataSource.class.getDeclaredMethod("poolSize");
90+
poolSize.setAccessible(true);
91+
92+
Method availableSize = UnmanagedPoolingDataSource.class.getDeclaredMethod("availableSize");
93+
availableSize.setAccessible(true);
94+
95+
return (int) poolSize.invoke(ds) - (int) availableSize.invoke(ds);
96+
} catch (Exception e) {
97+
throw new RuntimeException(e);
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)