This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push:
new c79fa2fe Internal refactoring
c79fa2fe is described below
commit c79fa2feca4f1fa7bca68170febda3431ef4a7ba
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 21 13:22:49 2026 -0500
Internal refactoring
---
.../commons/dbcp2/managed/ManagedConnection.java | 28 ++++++++++++----------
.../dbcp2/managed/TestConnectionWithNarayana.java | 2 +-
2 files changed, 17 insertions(+), 13 deletions(-)
diff --git
a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
index 284586e2..b0e67445 100644
--- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
+++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedConnection.java
@@ -102,6 +102,18 @@ public class ManagedConnection<C extends Connection>
extends DelegatingConnectio
updateTransactionStatus();
}
+ /**
+ * Checks if the connection is enlisted in a transaction and throws a
SQLException if it is.
+ *
+ * @param operation The operation being performed.
+ * @throws SQLException Thrown when the connection is enlisted in a
transaction.
+ */
+ private void checkTransactionContext(final String operation) throws
SQLException {
+ if (transactionContext != null) {
+ throw new SQLException("Cannot " + operation + " while enlisted in
a transaction");
+ }
+ }
+
@Override
public void close() throws SQLException {
if (!isClosedInternal()) {
@@ -129,9 +141,7 @@ public class ManagedConnection<C extends Connection>
extends DelegatingConnectio
@Override
public void commit() throws SQLException {
- if (transactionContext != null) {
- throw new SQLException("Commit cannot be set while enrolled in a
transaction");
- }
+ checkTransactionContext("commit");
super.commit();
}
@@ -185,25 +195,19 @@ public class ManagedConnection<C extends Connection>
extends DelegatingConnectio
@Override
public void rollback() throws SQLException {
- if (transactionContext != null) {
- throw new SQLException("Commit cannot be set while enrolled in a
transaction");
- }
+ checkTransactionContext("rollback");
super.rollback();
}
@Override
public void setAutoCommit(final boolean autoCommit) throws SQLException {
- if (transactionContext != null) {
- throw new SQLException("Auto-commit cannot be set while enrolled
in a transaction");
- }
+ checkTransactionContext("set auto-commit");
super.setAutoCommit(autoCommit);
}
@Override
public void setReadOnly(final boolean readOnly) throws SQLException {
- if (transactionContext != null) {
- throw new SQLException("Read-only cannot be set while enrolled in
a transaction");
- }
+ checkTransactionContext("set read-only");
super.setReadOnly(readOnly);
}
diff --git
a/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
b/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
index 3cc35425..97ed0f46 100644
---
a/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
+++
b/src/test/java/org/apache/commons/dbcp2/managed/TestConnectionWithNarayana.java
@@ -116,7 +116,7 @@ public class TestConnectionWithNarayana {
// Let the reaper do it's thing
Thread.sleep(1000);
final SQLException e = assertThrows(SQLException.class,
conn::commit);
- assertEquals("Commit cannot be set while enrolled in a
transaction", e.getMessage(), "Should not work after timeout");
+ assertEquals("Cannot commit while enlisted in a transaction",
e.getMessage(), "Should not work after timeout");
mds.getTransactionManager().rollback();
}
assertEquals(0, mds.getNumActive());