Is there an elegant way to, at runtime, configure a GrailsDomainClass
to use a DataSource or Connection at runtime? Ideally, this can simply use a in-memory H2 instance.
Here is my use case example. I am writing a grails service that needs to use the HQL syntax with domain objects to peform some complex database calculations. I would like it so that it can run concurrently with each thread using an isolated in-memory H2 instance.
What would be the best way to accomplish this? I know Dynamic DataSource routing might be one place to start, but how is this implemented elegantly in Grails? I need to avoid setting the instances in configuration files as the in-memory H2 instances need to be created on-the-fly.
I'm at this point right now, but not sure how to configure a domain object to use this connection.
def ds = new JdbcDataSource()
ds.setURL("jdbc:h2:mem:dw_1;MVCC=TRUE;LOCK_TIMEOUT=10000;INIT=CREATE SCHEMA IF NOT EXISTS dw_1")
ds.setUser("sa")
ds.setPassword("sa")
def conn = ds.getConnection()
Kind Regards,