diff --git a/documentation-website/Writerside/topics/Getting-Started-with-Exposed.topic b/documentation-website/Writerside/topics/Getting-Started-with-Exposed.topic index 914118a1e9..1f7904428b 100644 --- a/documentation-website/Writerside/topics/Getting-Started-with-Exposed.topic +++ b/documentation-website/Writerside/topics/Getting-Started-with-Exposed.topic @@ -250,6 +250,12 @@ but it does not immediately establish a connection with the database. The actual connection to the database will be established later when a database operation is performed. + + By default, Exposed uses a ServiceLoader to get an implementation of the DatabaseConnectionAutoRegistration + interface that represents a connection accessed by the Database instance. + This can be modified when calling the Database.connect method by providing an argument to connectionAutoRegistration + in the parameter list. +

With this, you've added Exposed to your Kotlin project and configured a database connection. You're now ready to define your data model and engage with the database using Exposed's DSL API. diff --git a/documentation-website/Writerside/topics/Working-with-Database.md b/documentation-website/Writerside/topics/Working-with-Database.md index 445505534c..4b8231055e 100644 --- a/documentation-website/Writerside/topics/Working-with-Database.md +++ b/documentation-website/Writerside/topics/Working-with-Database.md @@ -24,13 +24,20 @@ val db = Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver") Executing this code more than once per database will create leaks in your application, hence it is recommended to store it for later use: object DbSettings { - val db by lazy { - Database.connect(/* setup connection */) - } + val db by lazy { + Database.connect(/* setup connection */) + } } + + By default, Exposed uses a ServiceLoader to get an implementation of the DatabaseConnectionAutoRegistration + interface that represents a connection accessed by the Database instance. + This can be modified when calling the Database.connect method by providing an argument to connectionAutoRegistration + in the parameter list. + + ### H2 In order to use H2, you need to add the H2 driver dependency: diff --git a/exposed-core/api/exposed-core.api b/exposed-core/api/exposed-core.api index 4b06fe891f..ad453dce25 100644 --- a/exposed-core/api/exposed-core.api +++ b/exposed-core/api/exposed-core.api @@ -674,14 +674,14 @@ public final class org/jetbrains/exposed/sql/Database { } public final class org/jetbrains/exposed/sql/Database$Companion { - public final fun connect (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; - public final fun connect (Ljavax/sql/DataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; - public final fun connect (Lkotlin/jvm/functions/Function0;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; - public static synthetic fun connect$default (Lorg/jetbrains/exposed/sql/Database$Companion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; - public static synthetic fun connect$default (Lorg/jetbrains/exposed/sql/Database$Companion;Ljavax/sql/DataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; - public static synthetic fun connect$default (Lorg/jetbrains/exposed/sql/Database$Companion;Lkotlin/jvm/functions/Function0;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; - public final fun connectPool (Ljavax/sql/ConnectionPoolDataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; - public static synthetic fun connectPool$default (Lorg/jetbrains/exposed/sql/Database$Companion;Ljavax/sql/ConnectionPoolDataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; + public final fun connect (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; + public final fun connect (Ljavax/sql/DataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; + public final fun connect (Lkotlin/jvm/functions/Function0;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; + public static synthetic fun connect$default (Lorg/jetbrains/exposed/sql/Database$Companion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; + public static synthetic fun connect$default (Lorg/jetbrains/exposed/sql/Database$Companion;Ljavax/sql/DataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; + public static synthetic fun connect$default (Lorg/jetbrains/exposed/sql/Database$Companion;Lkotlin/jvm/functions/Function0;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; + public final fun connectPool (Ljavax/sql/ConnectionPoolDataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;)Lorg/jetbrains/exposed/sql/Database; + public static synthetic fun connectPool$default (Lorg/jetbrains/exposed/sql/Database$Companion;Ljavax/sql/ConnectionPoolDataSource;Lkotlin/jvm/functions/Function1;Lorg/jetbrains/exposed/sql/DatabaseConfig;Lorg/jetbrains/exposed/sql/DatabaseConnectionAutoRegistration;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/jetbrains/exposed/sql/Database; public final fun getDefaultIsolationLevel (Lorg/jetbrains/exposed/sql/Database;)I public final fun getDialectName (Ljava/lang/String;)Ljava/lang/String; public final fun registerDialect (Ljava/lang/String;Lkotlin/jvm/functions/Function0;)V diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt index d55c7dae28..20e3400f03 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Database.kt @@ -112,9 +112,11 @@ class Database private constructor( companion object { internal val dialects = ConcurrentHashMap 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", @@ -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)) } @@ -180,6 +183,8 @@ 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, + * a service loader will be used to locate and load a provider for [DatabaseConnectionAutoRegistration]. * @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. @@ -188,6 +193,7 @@ class Database private constructor( datasource: DataSource, setupConnection: (Connection) -> Unit = {}, databaseConfig: DatabaseConfig? = null, + connectionAutoRegistration: DatabaseConnectionAutoRegistration = connectionInstanceImpl, manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) } ): Database { return doConnect( @@ -195,7 +201,8 @@ class Database private constructor( config = databaseConfig, getNewConnection = { datasource.connection!! }, setupConnection = setupConnection, - manager = manager + manager = manager, + connectionAutoRegistration = connectionAutoRegistration ).apply { connectsViaDataSource = true } @@ -225,6 +232,7 @@ class Database private constructor( datasource: ConnectionPoolDataSource, setupConnection: (Connection) -> Unit = {}, databaseConfig: DatabaseConfig? = null, + connectionAutoRegistration: DatabaseConnectionAutoRegistration = connectionInstanceImpl, manager: (Database) -> TransactionManager = { ThreadLocalTransactionManager(it) } ): Database { return doConnect( @@ -232,7 +240,8 @@ class Database private constructor( config = databaseConfig, getNewConnection = { datasource.pooledConnection.connection!! }, setupConnection = setupConnection, - manager = manager + manager = manager, + connectionAutoRegistration = connectionAutoRegistration ) } @@ -243,19 +252,23 @@ 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, + * a service loader will be used to locate and load a provider for [DatabaseConnectionAutoRegistration]. * @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 ) } @@ -266,6 +279,8 @@ 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, + * a service loader will be used to locate and load a provider for [DatabaseConnectionAutoRegistration]. * @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. @@ -274,6 +289,7 @@ 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), @@ -281,18 +297,18 @@ class Database private constructor( 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, ) }