This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 3febcc01e011943c047934fdc872837319f11a70 Author: opencode <[email protected]> AuthorDate: Thu Nov 5 13:55:16 2026 +0100 Reject setJmxEnabled after the pool is created DataSourceProxy.setJmxEnabled stored the flag in PoolProperties after the pool had been created, but the pool MBean is created once in ConnectionPool.init and its own setter refuses the write. The two surfaces therefore disagreed, and the data source gave the impression that JMX had been enabled or disabled while nothing was published or removed. Reject the write once a pool exists, with the same localized message as the pool MBean, and keep accepting it as pre-start configuration. Add a test for the accepted pre-start write, the post-start rejection, and the unchanged configuration after the refusal. --- .../apache/tomcat/jdbc/pool/DataSourceProxy.java | 22 +++++++++++++++++++++- .../tomcat/jdbc/pool/util/LocalStrings.properties | 1 + .../tomcat/jdbc/pool/jmx/TestJmxRegistration.java | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java index 9d4875c1b2..7fe709bbde 100644 --- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java +++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java @@ -791,8 +791,20 @@ public class DataSourceProxy implements PoolConfiguration { } + /** + * Sets whether JMX is enabled, before the pool is created. Whether the pool registers an MBean is decided when + * the pool is created, so a write after that point is rejected rather than stored without effect, matching the + * pool MBean's own setter. + * + * @param enabled whether JMX is enabled + * + * @throws UnsupportedOperationException if the pool has already been created + */ @Override - public void setJmxEnabled(boolean enabled) { + public synchronized void setJmxEnabled(boolean enabled) { + if (pool != null) { + throw new UnsupportedOperationException(MessageFactory.get("dataSourceProxy.jmxEnabledAfterStart")); + } this.getPoolProperties().setJmxEnabled(enabled); } @@ -1452,6 +1464,14 @@ public class DataSourceProxy implements PoolConfiguration { } + /** + * Replaces the live database properties with the given set. The write is not masked, and it replaces the whole + * collection: a read-modify-write of the masked {@link #getDbProperties()} view hands back a set with the password + * removed and takes that credential away from the live configuration. Use {@link #setPassword(String)} and + * {@link #setUsername(String)} to change the credentials, as {@link #getDbProperties()} describes. + * + * @param dbProperties the replacement database properties + */ @Override public void setDbProperties(Properties dbProperties) { getPoolProperties().setDbProperties(dbProperties); diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/util/LocalStrings.properties b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/util/LocalStrings.properties index 326816cf97..01d861e64b 100644 --- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/util/LocalStrings.properties +++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/util/LocalStrings.properties @@ -122,6 +122,7 @@ dataSourceProxy.connectionNotXA=Connection from pool does not implement javax.sq dataSourceProxy.dataSourceClosed=DataSource has been closed. dataSourceProxy.errorClosingPool=Error during connection pool closure. dataSourceProxy.errorCreatingPool=Error during connection pool creation. +dataSourceProxy.jmxEnabledAfterStart=jmxEnabled must be set before the pool is started dataSourceProxy.notAWrapper=Not a wrapper of {0} dataSourceProxy.poolConfigurationNull=PoolConfiguration cannot be null. dataSourceProxy.unableToParseConnectionProperties=Unable to parse connection properties. diff --git a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/jmx/TestJmxRegistration.java b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/jmx/TestJmxRegistration.java index 3e34e68467..bc81fc1b0e 100644 --- a/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/jmx/TestJmxRegistration.java +++ b/modules/jdbc-pool/src/test/java/org/apache/tomcat/jdbc/pool/jmx/TestJmxRegistration.java @@ -896,4 +896,22 @@ class TestJmxRegistration extends JdbcPoolBaseTest { } } } + + @Test + void setJmxEnabledIsOnlyAcceptedBeforeThePoolIsCreated() throws Exception { + DataSource ds = newDataSource(); + // Before the pool exists the property is ordinary configuration. + ds.setJmxEnabled(false); + Assertions.assertFalse(ds.isJmxEnabled(), "the pre-start write must be stored"); + + ds.setJmxEnabled(true); + ds.createPool(); + + // After creation the MBean decision has been made; a write through the + // data source must be refused like the pool MBean's own setter. + Assertions.assertThrows(UnsupportedOperationException.class, + () -> ds.setJmxEnabled(false), + "a post-start write would otherwise be stored without creating or removing anything"); + Assertions.assertTrue(ds.isJmxEnabled(), "the refused write must not change the configuration"); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
