Skip to content

Commit

Permalink
fix class loader
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Nov 1, 2024
1 parent 64bcd69 commit 703e196
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@
Every database access using Exposed is started by obtaining a connection and creating a transaction.
To configure the database connection, use the <code>Database.connect()</code> function.
</p>
<p>
By default, Exposed using `ServiceLoader` to get `DatabaseConnectionAutoRegistration`.
It can be modified when calling `Database.connect` method by providing `connectionAutoRegistration` in parameter list.
</p>
<procedure id="config-db-connection-procedure">
<step>
<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Every database access using Exposed is started by obtaining a connection and cre
First of all, you have to tell Exposed how to connect to a database by using the `Database.connect` function.
It won't create a real database connection but will only provide a descriptor for future usage.

By default, Exposed using `ServiceLoader` to get `DatabaseConnectionAutoRegistration`.
It can be modified when calling `Database.connect` method by providing `connectionAutoRegistration` in parameter list.

A real connection will be instantiated later by calling the `transaction` lambda
(see [Transactions](Transactions.md) for more details).

Expand Down
39 changes: 26 additions & 13 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ class Database private constructor(
companion object {
internal val dialects = ConcurrentHashMap<String, () -> DatabaseDialect>()

private val connectionInstanceImpl: DatabaseConnectionAutoRegistration =
ServiceLoader.load(DatabaseConnectionAutoRegistration::class.java, Database::class.java.classLoader).firstOrNull()
private val connectionInstanceImpl: DatabaseConnectionAutoRegistration by lazy {
ServiceLoader.load(DatabaseConnectionAutoRegistration::class.java, Database::class.java.classLoader)
.firstOrNull()
?: error("Can't load implementation for ${DatabaseConnectionAutoRegistration::class.simpleName}")
}

private val driverMapping = mutableMapOf(
"jdbc:h2" to "org.h2.Driver",
Expand Down Expand Up @@ -162,12 +164,13 @@ class Database private constructor(
private fun doConnect(
explicitVendor: String?,
config: DatabaseConfig?,
connectionAutoRegistration: DatabaseConnectionAutoRegistration,
getNewConnection: () -> Connection,
setupConnection: (Connection) -> Unit = {},
manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) }
): Database {
return Database(explicitVendor, config ?: DatabaseConfig.invoke()) {
connectionInstanceImpl(getNewConnection().apply { setupConnection(this) })
connectionAutoRegistration(getNewConnection().apply { setupConnection(this) })
}.apply {
TransactionManager.registerManager(this, manager(this))
}
Expand All @@ -180,6 +183,7 @@ class Database private constructor(
* but instead provides the details necessary to do so whenever a connection is required by a transaction.
*
* @param datasource The [DataSource] object to be used as a means of getting a connection.
* @param connectionAutoRegistration The connection provider for database. If not provided, will use ServiceLoaded [connectionInstanceImpl]
* @param setupConnection Any setup that should be applied to each new connection.
* @param databaseConfig Configuration parameters for this [Database] instance.
* @param manager The [TransactionManager] responsible for new transactions that use this [Database] instance.
Expand All @@ -188,14 +192,16 @@ class Database private constructor(
datasource: DataSource,
setupConnection: (Connection) -> Unit = {},
databaseConfig: DatabaseConfig? = null,
connectionAutoRegistration: DatabaseConnectionAutoRegistration = connectionInstanceImpl,
manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) }
): Database {
return doConnect(
explicitVendor = null,
config = databaseConfig,
getNewConnection = { datasource.connection!! },
setupConnection = setupConnection,
manager = manager
manager = manager,
connectionAutoRegistration = connectionAutoRegistration
).apply {
connectsViaDataSource = true
}
Expand Down Expand Up @@ -225,14 +231,16 @@ class Database private constructor(
datasource: ConnectionPoolDataSource,
setupConnection: (Connection) -> Unit = {},
databaseConfig: DatabaseConfig? = null,
connectionAutoRegistration: DatabaseConnectionAutoRegistration = connectionInstanceImpl,
manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) }
): Database {
return doConnect(
explicitVendor = null,
config = databaseConfig,
getNewConnection = { datasource.pooledConnection.connection!! },
setupConnection = setupConnection,
manager = manager
manager = manager,
connectionAutoRegistration = connectionAutoRegistration
)
}

Expand All @@ -243,19 +251,22 @@ class Database private constructor(
* but instead provides the details necessary to do so whenever a connection is required by a transaction.
*
* @param getNewConnection A function that returns a new connection.
* @param connectionAutoRegistration The connection provider for database. If not provided, will use ServiceLoaded [connectionInstanceImpl]
* @param databaseConfig Configuration parameters for this [Database] instance.
* @param manager The [TransactionManager] responsible for new transactions that use this [Database] instance.
*/
fun connect(
getNewConnection: () -> Connection,
databaseConfig: DatabaseConfig? = null,
connectionAutoRegistration: DatabaseConnectionAutoRegistration = connectionInstanceImpl,
manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) }
): Database {
return doConnect(
explicitVendor = null,
config = databaseConfig,
getNewConnection = getNewConnection,
manager = manager
manager = manager,
connectionAutoRegistration = connectionAutoRegistration
)
}

Expand All @@ -266,6 +277,7 @@ class Database private constructor(
* but instead provides the details necessary to do so whenever a connection is required by a transaction.
*
* @param url The URL that represents the database when getting a connection.
* @param connectionAutoRegistration The connection provider for database. If not provided, will use ServiceLoaded [connectionInstanceImpl]
* @param driver The JDBC driver class. If not provided, the specified [url] will be used to find
* a match from the existing driver mappings.
* @param user The database user that owns the new connections.
Expand All @@ -274,25 +286,26 @@ class Database private constructor(
* @param databaseConfig Configuration parameters for this [Database] instance.
* @param manager The [TransactionManager] responsible for new transactions that use this [Database] instance.
*/
@Suppress("UnusedParameter", "LongParameterList")
fun connect(
url: String,
driver: String = getDriver(url),
user: String = "",
password: String = "",
setupConnection: (Connection) -> Unit = {},
databaseConfig: DatabaseConfig? = null,
connectionAutoRegistration: DatabaseConnectionAutoRegistration = connectionInstanceImpl,
manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) }
): Database {
Class.forName(driver).getDeclaredConstructor().newInstance()
val dialectName = getDialectName(url) ?: error("Can't resolve dialect for connection: $url")
return doConnect(
dialectName,
databaseConfig,
{
DriverManager.getConnection(url, user, password)
},
setupConnection,
manager
explicitVendor = dialectName,
config = databaseConfig,
getNewConnection = { DriverManager.getConnection(url, user, password) },
setupConnection = setupConnection,
manager = manager,
connectionAutoRegistration = connectionAutoRegistration,
)
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ org.gradle.configuration.cache=true
org.gradle.caching=true

group=org.jetbrains.exposed
version=0.56.0
version=0.57.0

0 comments on commit 703e196

Please sign in to comment.