This is an automated email from the ASF dual-hosted git repository.

aadamchik pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new ae0f310fe CAY-2913 Suspected connection leak on SQLTemplateProcessor 
exceptions
ae0f310fe is described below

commit ae0f310fed33844ef1ddb91ebb79a6dff049d345
Author: Andrus Adamchik <[email protected]>
AuthorDate: Sat Feb 21 10:43:44 2026 -0500

    CAY-2913 Suspected connection leak on SQLTemplateProcessor exceptions
    
    porting to master
---
 .../cayenne/access/DataNodeQueryExceptionsIT.java  | 100 +++++++++++++++++++++
 1 file changed, 100 insertions(+)

diff --git 
a/cayenne/src/test/java/org/apache/cayenne/access/DataNodeQueryExceptionsIT.java
 
b/cayenne/src/test/java/org/apache/cayenne/access/DataNodeQueryExceptionsIT.java
new file mode 100644
index 000000000..55aa589f4
--- /dev/null
+++ 
b/cayenne/src/test/java/org/apache/cayenne/access/DataNodeQueryExceptionsIT.java
@@ -0,0 +1,100 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    https://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+
+package org.apache.cayenne.access;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.datasource.UnmanagedPoolingDataSource;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.query.SQLAction;
+import org.apache.cayenne.query.SQLActionVisitor;
+import org.apache.cayenne.query.SQLSelect;
+import org.apache.cayenne.unit.di.runtime.CayenneProjects;
+import org.apache.cayenne.unit.di.runtime.RuntimeCase;
+import org.apache.cayenne.unit.di.runtime.RuntimeCaseDataSourceFactory;
+import org.apache.cayenne.unit.di.runtime.UseCayenneRuntime;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+@UseCayenneRuntime(CayenneProjects.TESTMAP_PROJECT)
+public class DataNodeQueryExceptionsIT extends RuntimeCase {
+
+    @Inject
+    protected DataNode node;
+
+    @Inject
+    protected RuntimeCaseDataSourceFactory dataSourceFactory;
+
+    @Test
+    public void testQueryException() {
+        SQLSelect<Object> throwingSS = new SQLSelect<Object>(Object.class, 
"SELECT 1") {
+            @Override
+            public SQLAction createSQLAction(SQLActionVisitor visitor) {
+                assertEquals(1, activeConnections());
+                throw new CayenneRuntimeException("Emulated exception");
+            }
+        };
+
+        assertEquals(0, activeConnections());
+        assertThrows(CayenneRuntimeException.class, () -> node.performQueries(
+                Collections.singletonList(throwingSS),
+                new MockOperationObserver()));
+
+        assertEquals(0, activeConnections());
+    }
+
+    @Test
+    public void testQueryError() {
+        SQLSelect<Object> throwingSS = new SQLSelect<Object>(Object.class, 
"SELECT 1") {
+            @Override
+            public SQLAction createSQLAction(SQLActionVisitor visitor) {
+                assertEquals(1, activeConnections());
+                throw new Error("Emulated error");
+            }
+        };
+
+        assertEquals(0, activeConnections());
+        assertThrows(Error.class, () -> node.performQueries(
+                Collections.singletonList(throwingSS),
+                new MockOperationObserver()));
+
+        assertEquals(0, activeConnections());
+    }
+
+    int activeConnections() {
+        try {
+            UnmanagedPoolingDataSource ds = 
dataSourceFactory.getSharedDataSource().unwrap(UnmanagedPoolingDataSource.class);
+
+            Method poolSize = 
UnmanagedPoolingDataSource.class.getDeclaredMethod("poolSize");
+            poolSize.setAccessible(true);
+
+            Method availableSize = 
UnmanagedPoolingDataSource.class.getDeclaredMethod("availableSize");
+            availableSize.setAccessible(true);
+
+            return (int) poolSize.invoke(ds) - (int) availableSize.invoke(ds);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

Reply via email to