Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2021 Collate
* Licensed 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
* http://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.openmetadata.service.governance.workflows;

import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.logging.Logger;
import javax.sql.DataSource;

/**
* A {@link DataSource} wrapper that intercepts every {@link Connection} it vends and ensures
* that all {@link Statement} variants created from that connection go through
* {@link IdempotentDdlStatement}. Only used in migration context so Flowable upgrade scripts
* skip already-existing DDL objects instead of failing.
*/
final class IdempotentDdlDataSource implements DataSource {

private final DataSource delegate;

IdempotentDdlDataSource(DataSource delegate) {
this.delegate = delegate;
}

@Override
public Connection getConnection() throws SQLException {
return wrapConnection(delegate.getConnection());
}
Comment thread
gitar-bot[bot] marked this conversation as resolved.

@Override
public Connection getConnection(String username, String password) throws SQLException {
return wrapConnection(delegate.getConnection(username, password));
}

private Connection wrapConnection(Connection real) {
return (Connection)
Proxy.newProxyInstance(
real.getClass().getClassLoader(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be tricky: we are manipulating via reflection

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohityadav766 The alternative is a 250-line concrete Connection implementation that delegates all 40+ methods just to override 3, which is more code with no functional difference, we can go by this approach as well, ill go with your take

new Class<?>[] {Connection.class},
(proxy, method, args) -> {
if ("createStatement".equals(method.getName())) {
Statement stmt = (Statement) invokeDelegate(method, real, args);
return new IdempotentDdlStatement(stmt, real);
}
return invokeDelegate(method, real, args);
});
Comment on lines +51 to +62
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IdempotentDdlDataSource is central to the idempotent-migration behavior (proxying Connection and wrapping all createStatement* results), but there are no tests validating that all createStatement overloads are wrapped and that exceptions from invokeDelegate are surfaced as the original SQLException (not UndeclaredThrowableException/InvocationTargetException). Adding a focused unit test (mock DataSource/Connection/Statement) would help prevent regressions in the proxy interception logic.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added IdempotentDdlDataSourceTest (5 tests): createStatement() no-arg, two-arg, and three-arg overloads all return IdempotentDdlStatement; SQLException from createStatement surfaces as SQLException directly (not UndeclaredThrowableException/InvocationTargetException); same for non-createStatement methods. All passing.

}

/**
* Invokes a method on the real connection, unwrapping any {@link InvocationTargetException}
* so callers receive the original exception type (e.g. {@link SQLException}).
*/
private static Object invokeDelegate(Method method, Object target, Object[] args)
throws Throwable {
try {
return method.invoke(target, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return delegate.getLogWriter();
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {
delegate.setLogWriter(out);
}

@Override
public void setLoginTimeout(int seconds) throws SQLException {
delegate.setLoginTimeout(seconds);
}

@Override
public int getLoginTimeout() throws SQLException {
return delegate.getLoginTimeout();
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return delegate.getParentLogger();
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return delegate.unwrap(iface);
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return delegate.isWrapperFor(iface);
}
}
Loading
Loading