版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28562059/article/details/52624688
grails 链接多个数据库的时候:
注意的地方:
1、格式要对齐
2、dialect必须写正确
3、采用多个数据源,可以在最顶层写,写可以写在environments里,同时也可以在两个层面上都定义
4、注意连接数据库的url必须写正确(否则会报空指针异常)
在application.yml文件中
dataSources:
dataSource:
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: sa
password: 123456
lookup:
dialect: org.hibernate.dialect.PostgreSQLDialect(可以不写)
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: root
password: 123456
environments:
development:
dataSources:
dataSource:
dbCreate: create-drop
url: jdbc:postgresql://127.0.0.1:5432/test
lookup:
dbCreate: create-drop
url: jdbc:postgresql://127.0.0.1:5432/lookup
test:
dataSources:
dataSource:
dbCreate: update
url: jdbc:postgresql://127.0.0.1:5432/test
production:
dataSources:
dataSource:
dbCreate: update
url: jdbc:postgresql://127.0.0.1:5432/test
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED
对于domain类里具体使用哪个数据库,可以通过使用 static mapping 来指定
static mapping = { //如果不指定,则默认为DEFAULT
datasource 'lookup' //使用lookup数据源
// datasources (['lookup', 'DEFAULT']) //使用两个数据源
// datasource 'ALL' //使用所有的数据源
}