Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load implementation for DatabaseConnectionAutoRegistration #748

Closed
ghost opened this issue Dec 30, 2019 · 16 comments
Closed

Can't load implementation for DatabaseConnectionAutoRegistration #748

ghost opened this issue Dec 30, 2019 · 16 comments
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Dec 30, 2019

java.lang.ExceptionInInitializerError: null
	at one.xjcyan1de.creativeplus.CreativePlus.connectDatabase(CreativePlus.kt:70) ~[?:?]
	at one.xjcyan1de.creativeplus.CreativePlus.enable(CreativePlus.kt:43) ~[?:?]
	at com.github.xjcyan1de.cyanlibz.plugin.ExtendedBukkitPlugin.onEnable(ExtendedBukkitPlugin.kt:38) ~[?:?]
	at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:338) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:420) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:468) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:382) ~[patched_1.15.1.jar:git-Paper-27]
	at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:481) ~[patched_1.15.1.jar:git-Paper-27]
	at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:290) ~[patched_1.15.1.jar:git-Paper-27]
	at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:884) ~[patched_1.15.1.jar:git-Paper-27]
	at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.IllegalStateException: Can't load implementation for DatabaseConnectionAutoRegistration
	at org.jetbrains.exposed.sql.Database.<clinit>(Database.kt:64) ~[?:?]
	... 12 more

CreativePlus.connectDatabase:

    private fun connectDatabase() = Database.connect(
        HikariDataSource(HikariConfig().apply {
            jdbcUrl = Config.sqlUrl
            driverClassName = Config.sqlDriver
            username = Config.sqlUsername
            password = Config.sqlPassword
            maximumPoolSize = 4
            addDataSourceProperty("cachePrepStmts", "true")
            addDataSourceProperty("prepStmtCacheSize", "250")
            addDataSourceProperty("prepStmtCacheSqlLimit", "2048")
            addDataSourceProperty("characterEncoding", "utf8")
            addDataSourceProperty("useUnicode", "true")
            addDataSourceProperty("useSSL", "false")
            addDataSourceProperty("useJDBCCompliantTimezoneShift", "true")
            addDataSourceProperty("useLegacyDatetimeCode", "false")
            addDataSourceProperty("serverTimezone", TimeZone.getDefault().id)
        })
    )
@ghost
Copy link
Author

ghost commented Dec 30, 2019

System Info: Java 11 (Dynamic Code Evolution 64-Bit Server VM 11.0.5+5-201912121506) Host: Windows 10 10.0 (amd64)

@ghost
Copy link
Author

ghost commented Dec 30, 2019

kotlin-stdlib-1.3.61
exposed-core-0.20.2

@StepByStepler
Copy link

Encountered the same issue. The problem is that exposed classes here are loaded by other ClassLoader instance, but line 64 of org.jetbrains.exposed.sql.Database uses contextClassLoader under the hood: ServiceLoader.load(DatabaseConnectionAutoRegistration::class.java) invokes overload with Thread.currentThread().getContextClassLoader(). I suggest to manually specify the classloader to search (org.jetbrains.exposed.sql.Database::class.java.classLoader, for example). This way dynamically loaded jars (like Bukkit plugins) can also use Exposed.

@Tapac
Copy link
Contributor

Tapac commented Dec 30, 2019

You have to add exposed-jdbc into your dependency. It's a transport layer implementation (the only at the moment).

@ghost
Copy link
Author

ghost commented Dec 31, 2019

@Tapac its already added. but ServiceLoader didn't load DatabaseConnectionAutoRegistration as service, because Bukkit classloader. Check @StepByStepler comment

@ghost
Copy link
Author

ghost commented Dec 31, 2019

So if i use @StepByStepler suggestion its loads exposed-jdbc connection impl. but exposed still use context service loader.

        val bukkitClassLoader = Database::class.java.classLoader
        val bukkitServiceLoader = ServiceLoader.load(DatabaseConnectionAutoRegistration::class.java,bukkitClassLoader)
        println("[Bukkit] ClassLoader: $bukkitClassLoader ServiceLoader: ${bukkitServiceLoader.toList()}")

        val contextClassLoader = Thread.currentThread().contextClassLoader
        val contextServiceLoader = ServiceLoader.load(DatabaseConnectionAutoRegistration::class.java,contextClassLoader)
        println("[Context] ClassLoader: $contextClassLoader ServiceLoader: ${contextServiceLoader.toList()}")

        database = connectDatabase()
[04:36:11 INFO]: [Bukkit] ClassLoader: org.bukkit.plugin.java.PluginClassLoader@3688059d ServiceLoader: [org.jetbrains.exposed.jdbc.ExposedConnectionImpl@dd5cd47]
[04:36:11 INFO]: [Context] ClassLoader: jdk.internal.loader.ClassLoaders$AppClassLoader@e73f9ac ServiceLoader: []
[04:36:11 ERROR]: Error occurred while enabling CreativePlus v1.0-SNAPSHOT (Is it up to date?)
java.lang.ExceptionInInitializerError: null
	at one.xjcyan1de.creativeplus.CreativePlus.connectDatabase(CreativePlus.kt:80) ~[?:?]
	at one.xjcyan1de.creativeplus.CreativePlus.enable(CreativePlus.kt:53) ~[?:?]
	at com.github.xjcyan1de.cyanlibz.plugin.ExtendedBukkitPlugin.onEnable(ExtendedBukkitPlugin.kt:43) ~[?:?]
	at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:338) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:420) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:468) ~[patched_1.15.1.jar:git-Paper-27]
	at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:382) ~[patched_1.15.1.jar:git-Paper-27]
	at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:481) ~[patched_1.15.1.jar:git-Paper-27]
	at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:290) ~[patched_1.15.1.jar:git-Paper-27]
	at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:884) ~[patched_1.15.1.jar:git-Paper-27]
	at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.IllegalStateException: Can't load implementation for DatabaseConnectionAutoRegistration
	at org.jetbrains.exposed.sql.Database.<clinit>(Database.kt:64) ~[?:?]
	... 12 more

@Tapac
Copy link
Contributor

Tapac commented Jan 3, 2020

Fixed in master. Will be released soon.

@du-it
Copy link

du-it commented Dec 17, 2020

It's still in version 0.28.1

@vladfrangu
Copy link

This issue also seems to occur with relocating via shadow jars... 👀 Any idea what can be done to solve this?

@dtbullock
Copy link

It's still in version 0.28.1

I'm on 0.28.1, and the advice to include exposed-jdbc worked for me.

@SecretX33
Copy link

This issue also seems to occur with relocating via shadow jars... 👀 Any idea what can be done to solve this?

I'm getting this issue after relocating this dependency via shadow jar too, and I have no idea how to fix that.

@WillFP
Copy link

WillFP commented Apr 6, 2022

Still having this issue?

@Tapac
Copy link
Contributor

Tapac commented Apr 9, 2022

@WillFP , did you add exposed-jdbc as a dependency? Do you use package relocation?

@maciejtulaza
Copy link

in my case changing runtimeOnly("org.jetbrains.exposed:exposed-jdbc:0.38.2") into implementation("org.jetbrains.exposed:exposed-jdbc:0.38.2") helped

@nathanfallet
Copy link

I spent some time on this issue because it was happening to me as well:
I was excluding META-INF/** from fat jar to avoid duplicates errors when creating the jar file, so I was excluding service loading metadata as well.
So if anyone still have this problem, check that META-INF/services/org.jetbrains.exposed.sql.DatabaseConnectionAutoRegistration is in your jar file (else, review your exclusion patterns or use duplicatesStrategy = DuplicatesStrategy.EXCLUDE to exclude duplicates)

@1fexd
Copy link

1fexd commented May 9, 2024

I spent some time on this issue because it was happening to me as well: I was excluding META-INF/** from fat jar to avoid duplicates errors when creating the jar file, so I was excluding service loading metadata as well. So if anyone still have this problem, check that META-INF/services/org.jetbrains.exposed.sql.DatabaseConnectionAutoRegistration is in your jar file (else, review your exclusion patterns or use duplicatesStrategy = DuplicatesStrategy.EXCLUDE to exclude duplicates)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants