alibaba/druid · error · SQLException

create driver instance error, driver className '

Error message

create driver instance error, driver className '

What it means

DruidDriver.createDriver(className) catches IllegalAccessException when calling rawDriverClass.newInstance() — the driver class loaded, but its no-arg constructor (or the class itself) is not accessible from Druid's classloader. The chained IllegalAccessException explains which access failed. This is the same createDriver path as the InstantiationException variant; both indicate the named class cannot be instantiated reflectively.

Source

Thrown at core/src/main/java/com/alibaba/druid/proxy/DruidDriver.java:265

        }
        config.setUrl(url);
        return config;
    }

    public static Driver createDriver(final String className) throws SQLException {
        Class<?> rawDriverClass = Utils.loadClass(className);

        if (rawDriverClass == null) {
            throw new SQLException("jdbc-driver's class not found. '" + className + "'");
        }

        Driver rawDriver;
        try {
            rawDriver = (Driver) rawDriverClass.newInstance();
        } catch (InstantiationException e) {
            throw new SQLException("create driver instance error, driver className '" + className + "'", e);
        } catch (IllegalAccessException e) {
            throw new SQLException("create driver instance error, driver className '" + className + "'", e);
        }

        return rawDriver;
    }

    @Override
    public int getMajorVersion() {
        return this.majorVersion;
    }

    @Override
    public int getMinorVersion() {
        return this.minorVersion;
    }

    @Override
    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
        DataSourceProxyImpl dataSource = getDataSource(url, info);

View on GitHub (pinned to fa8dc99126)

Solutions

  1. Use the standard, public, unmodified driver class shipped by the vendor so its constructor is accessible.
  2. If on JPMS, ensure open packages or add the required --add-opens JVM flag for the driver module.
  3. Inspect the chained IllegalAccessException to identify the inaccessible member, then widen access or switch to a DataSource-based config that constructs the driver directly.

Example fix

// before
// rawDriverClassName points at a relocated/shaded driver with package-private ctor
DriverManager.getConnection("jdbc:wrap-druid://..."); // throws 'create driver instance error'

// after
// use the canonical public driver class:
// rawDriverClassName = com.mysql.cj.jdbc.Driver
// or switch to DataSource config and instantiate the driver directly in code.
Defensive patterns

Strategy: validation

Validate before calling

Class<?> c = Utils.loadClass(className);
if (c != null) {
    int mod = c.getModifiers();
    if (!Modifier.isPublic(mod)) {
        throw new IllegalStateException(className + " class is not public");
    }
    try {
        java.lang.reflect.Constructor<?> ctor = c.getDeclaredConstructor();
        if (!Modifier.isPublic(ctor.getModifiers())) {
            throw new IllegalStateException(className + " no-arg constructor is not public");
        }
    } catch (NoSuchMethodException e) {
        throw new IllegalStateException(className + " has no no-arg constructor", e);
    }
}

Type guard

public static boolean publiclyInstantiableDriver(String className) {
    Class<?> c = Utils.loadClass(className);
    if (c == null || !Modifier.isPublic(c.getModifiers())) return false;
    try {
        return Modifier.isPublic(c.getDeclaredConstructor().getModifiers());
    } catch (NoSuchMethodException e) { return false; }
}

Try / catch

try {
    Driver d = DruidDriver.createDriver(className);
} catch (SQLException e) {
    if (e.getCause() instanceof IllegalAccessException) {
        throw new IllegalStateException("driver class/constructor not accessible: " + className
            + " (check JPMS --add-opens or use the stock driver class)", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: rawDriverClassName points at a class with a non-public constructor or a non-public class; or the classloader that loaded Druid cannot access the driver's constructor due to Java module/access restrictions (e.g. setAccessible refused on JDK 9+ without --add-opens).

Common situations: Driver shaded/relocated by a tool that changed visibility; module-path (JPMS) deployment restricting reflective access; custom driver subclass with a package-private constructor; security manager denying access.

Related errors


AI-assisted analysis of alibaba/druid@fa8dc99126 (2026-08-14). Data as JSON: /api/errors/6cff8c5930446869. Report an issue: GitHub.