It's unclear to me how the default connection work...
# exposed
j
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
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
Sure did. It’s the same problem that you can’t control the connection order of beans.
h
Are you calling
Database.connect
in your code?
j
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
@Joel, why do you need to call
Database.connect
? Do you work with different databases?
j
@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
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
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
@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
@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