{"record":{"id":"35509863bb0d2347","repo":"apache/shardingsphere","slug":"unable-to-unwrap-runtime-database-data-source-to","errorCode":null,"errorMessage":"Unable to unwrap runtime database data source to `%s`.","messagePattern":"Unable to unwrap runtime database data source to `(.+?)`\\.","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"warning","filePath":"mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/database/metadata/jdbc/MCPJdbcDatabaseProfileLoader.java","lineNumber":157,"sourceCode":"            throw new SQLFeatureNotSupportedException();\n        }\n        \n        @Override\n        public int getLoginTimeout() throws SQLException {\n            throw new SQLFeatureNotSupportedException();\n        }\n        \n        @Override\n        public Logger getParentLogger() throws SQLFeatureNotSupportedException {\n            throw new SQLFeatureNotSupportedException();\n        }\n        \n        @Override\n        public <T> T unwrap(final Class<T> iface) throws SQLException {\n            if (iface.isInstance(this)) {\n                return iface.cast(this);\n            }\n            throw new SQLException(String.format(\"Unable to unwrap runtime database data source to `%s`.\", iface.getName()));\n        }\n        \n        @Override\n        public boolean isWrapperFor(final Class<?> iface) {\n            return iface.isInstance(this);\n        }\n    }\n}\n","sourceCodeStart":139,"sourceCodeEnd":166,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/mcp/support/src/main/java/org/apache/shardingsphere/mcp/support/database/metadata/jdbc/MCPJdbcDatabaseProfileLoader.java#L139-L166","documentation":"Thrown by the synthetic DataSource wrapper inside MCPJdbcDatabaseProfileLoader.DataSourceSupplier (a minimal delegating DataSource used to probe database metadata): unwrap(iface) only succeeds when the wrapper itself implements the requested interface, otherwise it throws SQLException with the target interface name. This follows the JDBC Wrapper contract strictly — the wrapper holds no reference to the real underlying DataSource to delegate unwrap to.","triggerScenarios":"Calling unwrap(X.class) on this profile-loading DataSource for any interface it does not itself implement (e.g. unwrap(PoolDataSource.class), unwrap(MysqlDataSource.class)). isWrapperFor(iface) returns true only for iface.isInstance(this), so it predicts exactly what unwrap will accept.","commonSituations":"Library code (connection pools, drivers, monitoring agents) calling unwrap to reach vendor-specific datasource APIs, or casting-based access paths that assume the original DataSource is reachable behind any DataSource instance.","solutions":["Call isWrapperFor(targetClass) first and skip the vendor-specific path when it returns false.","Use only JDBC-standard DataSource methods on this wrapper; it exists solely to supply connections for metadata profiling, not to expose vendor APIs.","Access the real vendor DataSource from wherever it was originally constructed, not through this loader's wrapper."],"exampleFix":"// before\nMysqlDataSource mysql = dataSource.unwrap(MysqlDataSource.class);\n// after\nif (dataSource.isWrapperFor(MysqlDataSource.class)) {\n    MysqlDataSource mysql = dataSource.unwrap(MysqlDataSource.class);\n} else {\n    // fall back to standard DataSource usage; vendor API not exposed by this wrapper\n}","handlingStrategy":"type-guard","validationCode":"if (!dataSource.isWrapperFor(PGDataSource.class)) { /* vendor-specific path unavailable; use standard DataSource API */ }","typeGuard":"boolean canUnwrap(DataSource ds, Class<?> iface) { return ds.isWrapperFor(iface); }","tryCatchPattern":"try {\n    T vendor = dataSource.unwrap(iface);\n} catch (final SQLException ex) {\n    // message names the unsupported interface; degrade to standard JDBC usage\n}","preventionTips":["Always pair unwrap with a prior isWrapperFor check","Never assume vendor DataSource APIs are reachable through framework-supplied wrappers","Fetch vendor handles at construction time, not through intermediaries"],"tags":["jdbc","datasource","wrapper","mcp"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}