【问题标题】:After specifying an alternate datasource, grails is throwing a sql exception for embedded h2指定备用数据源后,grails 为嵌入式 h2 抛出 sql 异常
【发布时间】:2017-02-06 21:40:13
【问题描述】:

我有一小组 grails 3.0.11 应用程序。我们有一个在应用程序之间共享的域模块。 当其中一个应用程序(app1、app2 等)启动时,它会连接到数据源并为域模块中的每个类创建一个表。套件中的所有应用程序都将尝试创建这些表。

我修改了 application.yml 以便它使用 MSSQL 实例而不是内部 h2 db,并且我将 dbCreate 设置为更新,以便架构将在应用程序关闭时保持不变。

我正在尝试拆分域,以便每个应用程序只管理其相关类的架构。即,app1 将在启动时处理classA、classB 和classC 的ddl,而app2 将处理classX、classY、classZ。 遵循this 指南,我定义了每个应用程序唯一的第二个数据源(即app1db、app2db 等),并且我已将mapping = { datasource 'appXdb'} 添加到指定相关应用程序的每个类中。

现在,当我启动应用程序时,我得到一个 sql 异常:

原因:java.sql.SQLException:驱动程序:jTDS 1.3.1 返回 null URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000

为什么我的应用程序在重新定义数据源以指向 mssql 实例并添加第二个也指向 mssql 的数据源后仍尝试访问 h2 db?

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 文件

标签: sql-server hibernate grails jdbc h2


【解决方案1】:

事实证明,我没有在 application.yml 中正确嵌套环境覆盖。我在主要部分添加了一个父元素 dataSources: about the actual ds defs,但在环境部分没有做同样的事情,这意味着我的 ds 正在加载没有 url,然后默认为 grails h2 db。

感谢@quindimildev!在尝试找出格式以遵循您的建议时,我意识到了我的疏忽。

【讨论】:

    【解决方案2】:

    在我的情况下,我必须清除所有旧的编译文件,其中包含旧配置。 为了确保我有一个全新的编译,我做了以下操作:

    1. grails 完全干净
    2. grails 刷新依赖项
    3. grails 编译
    4. 圣杯刺激战争

    之后返回 "null for URL:jdbc:h2:mem:grailsDB;MVCC=TRUE;LOCK_TIMEOUT=10000" 的错误消失了。

    【讨论】:

      猜你喜欢
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2021-01-07
      相关资源
      最近更新 更多