{"record":{"id":"c021491b1e02a1b4","repo":"testcontainers/testcontainers-java","slug":"error-while-executing-init-function","errorCode":null,"errorMessage":"Error while executing init function: {}::{}","messagePattern":"Error while executing init function: (.+?)::(.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java","lineNumber":258,"sourceCode":"     * @param connection    JDBC connection to apply init functions to.\n     * @throws SQLException on script or DB error\n     */\n    private void runInitFunctionIfRequired(final ConnectionUrl connectionUrl, Connection connection)\n        throws SQLException {\n        if (connectionUrl.getInitFunction().isPresent()) {\n            String className = connectionUrl.getInitFunction().get().getClassName();\n            String methodName = connectionUrl.getInitFunction().get().getMethodName();\n\n            try {\n                Class<?> initFunctionClazz = Class.forName(className);\n                Method method = initFunctionClazz.getMethod(methodName, Connection.class);\n\n                method.invoke(null, connection);\n            } catch (\n                ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e\n            ) {\n                LOGGER.error(\"Error while executing init function: {}::{}\", className, methodName, e);\n                throw new SQLException(\"Error while executing init function: \" + className + \"::\" + methodName, e);\n            }\n        }\n    }\n\n    @Override\n    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {\n        return delegate != null ? delegate.getPropertyInfo(url, info) : new DriverPropertyInfo[0];\n    }\n\n    @Override\n    public int getMajorVersion() {\n        return delegate != null ? delegate.getMajorVersion() : 1;\n    }\n\n    @Override\n    public int getMinorVersion() {\n        return delegate != null ? delegate.getMinorVersion() : 0;\n    }","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java#L240-L276","documentation":"runInitFunctionIfRequired invokes a static method (configured via TC_INITFUNCTION=class::method) with the connection and wraps ClassNotFoundException, NoSuchMethodException, IllegalAccessException and InvocationTargetException in this SQLException. The init function class or method could not be loaded/accessed, or the method itself threw.","triggerScenarios":"jdbc:tc URL with TC_INITFUNCTION=com.example.Init::setup where the class is not on the test classpath, the method is not public static taking a Connection, or the invoked method throws an exception.","commonSituations":"Method signature wrong (must accept java.sql.Connection); class not visible to the driver's classloader; nested exception inside the init method; typo in class or method name.","solutions":["Ensure the class is public, on the classpath, and the method is public static void name(java.sql.Connection)","Verify the TC_INITFUNCTION format exactly: fully.qualified.Class::methodName","Inspect getCause() (especially InvocationTargetException) for the real failure inside the method","Simplify the init function to isolate the throwing code"],"exampleFix":"// before\npublic class Init { public static void setup() { ... } }\n// after\npublic class Init { public static void setup(java.sql.Connection connection) { ... } }","handlingStrategy":"validation","validationCode":"Class<?> c = Class.forName(\"com.example.Init\");\njava.lang.reflect.Method m = c.getMethod(\"setup\", java.sql.Connection.class);\nint mods = m.getModifiers();\nif (!java.lang.reflect.Modifier.isStatic(mods) || !java.lang.reflect.Modifier.isPublic(mods))\n    throw new IllegalStateException(\"TC_INITFUNCTION must be public static void setup(Connection)\");","typeGuard":null,"tryCatchPattern":"try {\n    runTestWithContainer();\n} catch (SQLException e) {\n    Throwable cause = e.getCause();\n    if (cause instanceof java.lang.reflect.InvocationTargetException && cause.getCause() != null) {\n        throw new RuntimeException(\"Init function failed\", cause.getCause());\n    }\n    throw e;\n}","preventionTips":["Match the exact signature: public static void name(java.sql.Connection)","Use fully qualified class names in TC_INITFUNCTION","Keep init functions trivial or well unit-tested"],"tags":["jdbc","testcontainers","reflection","init-function"],"backgroundTag":"sql-query-failed","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}