https://kotlinlang.org logo
#exposed
Title
# exposed
j

Joel

07/17/2020, 9:29 PM
It's unclear to me how the default connection works. The docs say the latest connection is the default. Does this truly mean that the latest call of
Database.connect
is the default db? This is inconvenient when working with Spring Boot because bean creation order is undefined.
h

hichem fazai

07/17/2020, 11:07 PM
Did you make a try with exposed-spring-boot-starter (https://github.com/JetBrains/Exposed/tree/master/exposed-spring-boot-starter) ? You just need to put you connection parameters in application.properties
j

Joel

07/17/2020, 11:16 PM
Sure did. It’s the same problem that you can’t control the connection order of beans.
h

hichem fazai

07/17/2020, 11:33 PM
Are you calling
Database.connect
in your code?
j

Joel

07/17/2020, 11:47 PM
Yes. The spring starter calls it as well when it wires up the transaction manager. The problem is that the final call to
Database.connect
is considered the default connection if you call
transaction
without a db argument.
(which is stored in a
ThreadLocal
? unsure how that works with coroutines)
t

tapac

07/20/2020, 1:33 PM
@Joel, why do you need to call
Database.connect
? Do you work with different databases?
j

Joel

07/20/2020, 1:55 PM
@tapac yes indeed
We have a primary and a legacy that is being wired up for future migrations
All of the legacy queries specify the legacy database as a transaction argument, but we need to ensure that the default no-arg db is the primary
t

tapac

07/20/2020, 7:38 PM
I would advice either use primary db instance as an argument too or make function like
Copy code
fun <T> primaryTransaction(body : Transaction.()->T) = transaction(primaryDbInstance) { body() }
👍 1
j

Joel

07/20/2020, 9:38 PM
I have a number of methods that rely on the no-arg transaction. I'd prefer to not have to refactor all of them.
h

hichem fazai

07/21/2020, 6:46 AM
@Joel If I understood correctly, you see that correct behaviour is that transaction(leagacy-db-con) uses leagacy db connection and transaction without arguments uses connection defined in application.properties ?
j

Joel

07/23/2020, 3:22 PM
@hichem fazai that is what I am attempting to create
I've introduced the spring transaction manager which does a nice job at creating a context such that you don't need to specify transaction within models. However the spring transaction manager opens its own db connection, which also provides a bit of headache when trying to get these all in the correct order.
👍 1
A solid improvement would be to create a spring transaction manager with a db argument so that I can control ordering myself.
👍 1
3 Views