1

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,

Thomas Farvour
  • 1,103
  • 2
  • 11
  • 24

2 Answers2

0

In theory you could try setting additional datasource in config and configure domain classes to use multiple datasources and in runtime change connection settings for that additional in-memory datasource or do not change settings but clear it before using or clear on scheduled basis, or do not clear at all. For the record: haven't used this feature yet myself.

Ivar
  • 4,350
  • 2
  • 27
  • 29
  • That sounds plausible, but how would I go about changing the url of an existing dataSource? e.g. if I define it, how do I retrieve it just to change the url? – Thomas Farvour Sep 09 '13 at 16:49
  • So I think you could [externalise datasource config](http://stackoverflow.com/questions/970133/externalizing-grails-datasource-configuration) and modify file on runtime and [reload configuration after modification](http://stackoverflow.com/questions/13561127/how-can-update-external-config-files-without-rebuild-war-file-in-grails). – Ivar Sep 09 '13 at 19:11
  • 1
    So there is no way to create a dataSource on-the-fly, and then attach said datasource to a grails GORM domain class? – Thomas Farvour Sep 09 '13 at 20:23
  • @ThomasFarvour I have just stumbled upon this question. Have you managed to find a solution to this ? What was the approach you took to solve this ? – ionutab May 19 '15 at 11:12
-1

One quick way to work this around is by creating a new datasource in DataSource.groovy like this:

dataSource_example {
    dbCreate = ""
    dialect = "..."
    driverClassName = "..."
    url = "your_db_url"
    username = "your_username"
    password = "your_password"
    pooled = true
    properties {
        ....
    }
}

And then in your GrailsDomainClass:

public GrailsDomainClass {

    static mapping = {
        datasource "example"
        table name: "grails_domain_class", schema: "schema_one"
    }
}
Federico Leon
  • 129
  • 2
  • 10
  • How can you consider that as a "dynamic" datasource ? It's just a mapping to an additional datasource... – Bourdier Jonathan Jul 12 '17 at 09:32
  • Like asked in the question, "I'm at this point right now, but not sure how to configure a domain object to use this connection". That's exactly what I've answered, how you can configure a domain class to use a different datasource. You could be using a MySQL or Cassandra or whatever you like for the other domains and H2 in the domain yo need. Different question interpretations I see... – Federico Leon Oct 01 '17 at 08:00