指定备用数据源后,grails 为嵌入式 h2 抛出 sql 异常
After specifying an alternate datasource, grails is throwing a sql exception for embedded h2
我有一小部分 grails 3.0.11 应用程序。我们有一个在应用程序之间共享的域模块。
当其中一个应用程序(app1、app2 等)启动时,它连接到数据源并为域模块中的每个 class 创建一个 table。套件中的所有应用程序都将尝试创建这些 table。
我修改了 application.yml 以便它使用 MSSQL 实例而不是内部 h2 数据库,并且我将 dbCreate 设置为更新以便模式将在应用程序关闭时持续存在。
我正在尝试拆分域,以便每个应用程序只管理其相关 classes 的架构。即,app1 将在启动时处理 classA、classB 和 classC 的 ddl,而 app2 将处理 classX、classY、classZ。
按照 this 指南,我定义了每个应用程序唯一的第二个数据源(即 app1db、app2db 等),并且我向每个 class 添加了 mapping = { datasource 'appXdb'}
以指定相关应用程序。
现在,当我启动应用程序时,出现 sql 异常:
Caused by: java.sql.SQLException: Driver:jTDS 1.3.1 returned null for
URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000
为什么我的应用程序在重新定义数据源以指向 mssql 实例并添加第二个也指向 mssql 的数据源后仍在尝试访问 h2 数据库?
application.yml:
---
server:
port: 3434
contextPath: '/app1'
---
grails:
profile: web
codegen:
defaultPackage: cars.app
info:
app:
name: '@info.app.name@'
version: '@info.app.version@'
grailsVersion: '@info.app.grailsVersion@'
spring:
groovy:
template:
check-template-location: false
---
grails:
mime:
disable:
accept:
header:
userAgents:
- Gecko
- WebKit
- Presto
- Trident
types:
all: '*/*'
atom: application/atom+xml
css: text/css
csv: text/csv
form: application/x-www-form-urlencoded
html:
- text/html
- application/xhtml+xml
js: text/javascript
json:
- application/json
- text/json
multipartForm: multipart/form-data
pdf: application/pdf
rss: application/rss+xml
text: text/plain
hal:
- application/hal+json
- application/hal+xml
xml:
- text/xml
- application/xml
urlmapping:
cache:
maxsize: 1000
controllers:
defaultScope: singleton
converters:
encoding: UTF-8
views:
default:
codec: html
gsp:
encoding: UTF-8
htmlcodec: xml
codecs:
expression: html
scriptlets: html
taglib: none
staticparts: none
---
hibernate:
cache:
queries: false
use_second_level_cache: true
use_query_cache: false
region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
endpoints:
jmx:
unique-names: true
shutdown:
enabled: true
dataSources:
dataSource:
pooled: true
jmxExport: true
driverClassName: net.sourceforge.jtds.jdbc.Driver
username: grails
password: password
app1DataSource:
pooled: true
jmxExport: true
driverClassName: net.sourceforge.jtds.jdbc.Driver
username: grails
password: password
environments:
development:
dataSource:
dbCreate: update
url: jdbc:jtds:sqlserver://127.0.0.1;databaseName=cars_demo
appDataSource:
dbCreate: update
url: jdbc:jtds:sqlserver://1127.0.0.1;databaseName=cars_demo
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
production:
dataSource:
dbCreate: update
url: jdbc:h2:mem:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
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
映射元素:static mapping = {datasource 'app1DataSource'}
edit1:添加了 application.yml 和映射元素。
事实证明,我没有在 application.yml 中正确嵌套我的环境覆盖。我在主要部分添加了一个父元素数据源:关于实际的 ds defs 但在环境部分没有做同样的事情,这意味着我的 ds 正在加载 w/o a url 然后默认为 grails h2分贝
谢谢@quindimildev!我在尝试按照您的建议确定格式时意识到自己的疏忽。
在我的例子中,我必须清除所有旧的编译文件,这些文件中有旧的配置。
为了确保我有一个新的编译,我做了以下事情:
- grails 干净利落
- grails 刷新依赖项
- grails 编译
- grails 产品 war
之后返回的错误 "null for URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000" 消失了。
我有一小部分 grails 3.0.11 应用程序。我们有一个在应用程序之间共享的域模块。 当其中一个应用程序(app1、app2 等)启动时,它连接到数据源并为域模块中的每个 class 创建一个 table。套件中的所有应用程序都将尝试创建这些 table。
我修改了 application.yml 以便它使用 MSSQL 实例而不是内部 h2 数据库,并且我将 dbCreate 设置为更新以便模式将在应用程序关闭时持续存在。
我正在尝试拆分域,以便每个应用程序只管理其相关 classes 的架构。即,app1 将在启动时处理 classA、classB 和 classC 的 ddl,而 app2 将处理 classX、classY、classZ。
按照 this 指南,我定义了每个应用程序唯一的第二个数据源(即 app1db、app2db 等),并且我向每个 class 添加了 mapping = { datasource 'appXdb'}
以指定相关应用程序。
现在,当我启动应用程序时,出现 sql 异常:
Caused by: java.sql.SQLException: Driver:jTDS 1.3.1 returned null for URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000
为什么我的应用程序在重新定义数据源以指向 mssql 实例并添加第二个也指向 mssql 的数据源后仍在尝试访问 h2 数据库?
application.yml:
---
server:
port: 3434
contextPath: '/app1'
---
grails:
profile: web
codegen:
defaultPackage: cars.app
info:
app:
name: '@info.app.name@'
version: '@info.app.version@'
grailsVersion: '@info.app.grailsVersion@'
spring:
groovy:
template:
check-template-location: false
---
grails:
mime:
disable:
accept:
header:
userAgents:
- Gecko
- WebKit
- Presto
- Trident
types:
all: '*/*'
atom: application/atom+xml
css: text/css
csv: text/csv
form: application/x-www-form-urlencoded
html:
- text/html
- application/xhtml+xml
js: text/javascript
json:
- application/json
- text/json
multipartForm: multipart/form-data
pdf: application/pdf
rss: application/rss+xml
text: text/plain
hal:
- application/hal+json
- application/hal+xml
xml:
- text/xml
- application/xml
urlmapping:
cache:
maxsize: 1000
controllers:
defaultScope: singleton
converters:
encoding: UTF-8
views:
default:
codec: html
gsp:
encoding: UTF-8
htmlcodec: xml
codecs:
expression: html
scriptlets: html
taglib: none
staticparts: none
---
hibernate:
cache:
queries: false
use_second_level_cache: true
use_query_cache: false
region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'
endpoints:
jmx:
unique-names: true
shutdown:
enabled: true
dataSources:
dataSource:
pooled: true
jmxExport: true
driverClassName: net.sourceforge.jtds.jdbc.Driver
username: grails
password: password
app1DataSource:
pooled: true
jmxExport: true
driverClassName: net.sourceforge.jtds.jdbc.Driver
username: grails
password: password
environments:
development:
dataSource:
dbCreate: update
url: jdbc:jtds:sqlserver://127.0.0.1;databaseName=cars_demo
appDataSource:
dbCreate: update
url: jdbc:jtds:sqlserver://1127.0.0.1;databaseName=cars_demo
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
production:
dataSource:
dbCreate: update
url: jdbc:h2:mem:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
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
映射元素:static mapping = {datasource 'app1DataSource'}
edit1:添加了 application.yml 和映射元素。
事实证明,我没有在 application.yml 中正确嵌套我的环境覆盖。我在主要部分添加了一个父元素数据源:关于实际的 ds defs 但在环境部分没有做同样的事情,这意味着我的 ds 正在加载 w/o a url 然后默认为 grails h2分贝
谢谢@quindimildev!我在尝试按照您的建议确定格式时意识到自己的疏忽。
在我的例子中,我必须清除所有旧的编译文件,这些文件中有旧的配置。 为了确保我有一个新的编译,我做了以下事情:
- grails 干净利落
- grails 刷新依赖项
- grails 编译
- grails 产品 war
之后返回的错误 "null for URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000" 消失了。