I am working on a Grails application where I must access several datasources. The datasources are defined in the default database (ie. they are stored there and I must make
I've got two different datasources working with Grails 2.3.11. I'm using 1 datasource for my H2 database and another for an Oracle db. I had to use Hibernate 4 with Grails 2.3. In my BuildConfig.groovy I specified the dependency on hibernate 4:
runtime ":hibernate4:4.3.5.4"
In my DataSource.groovy file I used the following Hibernate caching settings:
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory'
singleSession = true // configure OSIV singleSession mode
}
(SIDE NOTE: without the cache settings, I was getting following CacheManager error, "Another unnamed CacheManager already exists in the same VM". There's an open bug report on this at https://jira.grails.org/browse/GPCACHEEHCACHE-13, but once I put the settings in place the error was gone.)
then I defined my datasources:
environments {
development {
dataSource_oracle {
pooled = true
dialect = org.hibernate.dialect.Oracle10gDialect
driverClassName = 'oracle.jdbc.OracleDriver'
username = 'user'
password = 'pass'
url = 'jdbc:oracle:thin:@(serverName):(port):(SID)'
dbCreate = 'validate'
}
dataSource {
dbCreate = "update"
url = "jdbc:h2:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
properties {
jmxEnabled = true
initialSize = 5
maxActive = 50
minIdle = 5
maxIdle = 25
maxWait = 10000
maxAge = 10 * 60000
timeBetweenEvictionRunsMillis = 5000
minEvictableIdleTimeMillis = 60000
validationQuery = "SELECT 1"
validationQueryTimeout = 3
validationInterval = 15000
testOnBorrow = true
testWhileIdle = true
testOnReturn = false
jdbcInterceptors = "ConnectionState"
defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED
}
}
}
}
By default, my Domain classes use the H2 db and I specify my Oracle datasource as:
class MyService {
def dataSource_oracle
static transactional = true
def getMethod() {
assert dataSource_oracle != null, "dataSource is null! Please check your configuration!"
def sql = Sql.newInstance(dataSource_oracle)
...
}
}
Above, I allow the dependency injection to provide the service with the oracle datasource, def dataSource_oracle. If I want to use the H2 datasource, I declare the datasource as def dataSource and allow the DI to inject my other datasource.
I could not get the two datasources to work as specified in the documentation at http://grails.github.io/grails-doc/2.3.11/guide/conf.html#multipleDatasources. By declaring the datasources as dataSource and dataSource_lookup then using it as:
class DataService {
static datasource = 'lookup'
void someMethod(...) {
…
}
}
but I was able to get it working with the solution described above.