Last active
September 8, 2020 10:44
-
-
Save biniama/ed9e397b374211d7933b4f5c34e20f48 to your computer and use it in GitHub Desktop.
Multiple datasource in Grails Services with SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dataSource { | |
pooled = true | |
jmxExport = true | |
driverClassName = "com.mysql.jdbc.Driver" | |
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" | |
} | |
hibernate { | |
cache.use_second_level_cache = true | |
cache.use_query_cache = false | |
// cache.region.factory_class = 'org.hibernate.cache.SingletonEhCacheRegionFactory' // Hibernate 3 | |
cache.region.factory_class = 'org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory' // Hibernate 4 | |
singleSession = true // configure OSIV singleSession mode | |
flush.mode = 'manual' // OSIV session flush mode outside of transactional context | |
} | |
// environment specific settings | |
environments { | |
development { | |
dataSource { | |
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', '' | |
url = "jdbc:mysql://localhost:3306/database1" | |
username = "root" | |
password = "password" | |
} | |
dataSource_second { | |
driverClassName = "com.mysql.jdbc.Driver" | |
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect" | |
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', '' | |
url = "jdbc:mysql://localhost:3306/database2" | |
username = "root" | |
password = "password" | |
} | |
} | |
test { | |
dataSource { | |
//Used by local test run (grails test-app) | |
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', '' | |
url = "jdbc:mysql://test-server.com:3306/test_ci" | |
username = "root" | |
password = "password" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.biniama | |
class TestController { | |
TestService testService | |
def index() { | |
Integer result = testService.getData() | |
render "Returned value is ${result}" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.biniama | |
import grails.transaction.Transactional | |
import groovy.sql.Sql | |
@Transactional | |
class TestService { | |
private Sql sql | |
void setDataSource_second(def dataSource) { | |
sql = new Sql(dataSource) | |
} | |
Integer getData() { | |
def q = "SELECT id FROM job LIMIT 1" | |
return sql.rows(q) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.biniama | |
import grails.transaction.Transactional | |
import groovy.sql.Sql | |
import javax.annotation.PostConstruct | |
@Transactional | |
class TestService { | |
def dataSource_second | |
Sql sql | |
@PostConstruct | |
def initSql() { | |
sql = new Sql(dataSource_second) | |
} | |
def getData() { | |
def q = "SELECT id FROM job LIMIT 1" | |
return sql.rows(q) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment