【问题标题】:How to use a multiple datasource with many laguage in grails 3.3如何在grails 3.3中使用具有许多语言的多个数据源
【发布时间】:2021-07-17 03:21:26
【问题描述】:

我尝试在多个数据库中连接 mi grails 3.3.9:在 posgrest 中的 1 个主数据源,在 sqlite 中备份的从属数据源,以及用于日志的第 3 个数据源 mongoDb。 问题是当我尝试将域类保存在 sqlite 数据源中时,无法检测到(单独或与其他)。

这是我的 application.yml:

 hibernate:
 cache:
    queries: false
    use_second_level_cache: false
    use_query_cache: false


dataSource:
        pooled: true
        jmxExport: true
        driverClassName: "org.postgresql.Driver"
        username: "postgres"
        password: "postgres"


IkebanaUser:
     pooled: true
     driverClassName: "org.sqlite.JDBC"
     username: ''
     password: ''

grails:
    mongo:
    host: "localhost"
    port: 27017
    username: "admin"
    password: "admin"
    databaseName: "IkebanaERP"


environments:
   development:
      dataSource:
          dbCreate: update
          url: jdbc:postgresql://localhost:5432/IkebanaERP

      dataSources:
          Ikebana:
             grails:
              mongodb:
                  connectionString: "mongodb://localhost:27017/IkebanaERP"


      IkebanaUser:
             url: jdbc:sqlite:/home/marcos/IkebanaUser
             dbCreate: update

test:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/IkebanaERP

production:
    dataSource:
        dbCreate: update
        url: jdbc:postgresql://localhost:5432/IkebanaERP
        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

这是我的域类

class Usuario {

  String nombre
  String  apellido

  static constraints = {
    nombre  nullable: true
    apellido unique: true
      }
static mapping = {
  datasource 'IkebanaUser'}
}

午餐这个错误:

  "Exception is org.grails.datastore.mapping.core.exceptions.ConfigurationException:   DataSource not found for name [IkebanaUser] in configuration. Please check your multiple data sources configuration and try again."

如果你放入域类:

  datasources(['DEFAULT', 'IkebanaUser']) 

仅用于保存在默认数据源(postgres 数据源)中 这段代码有什么问题?
谢谢

【问题讨论】:

  • 如果您配置了多个数据源(如datasources(['DEFAULT', 'IkebanaUser'])),列表中的第一个是默认值。例如,如果您调用Usuario.list(),它将查询默认数据源。如果您调用Usuario. IkebanaUser.list(),它将查询IkebanaUser 数据源。你能展示一下你用来发起查询的代码吗?
  • 我之前的评论中有一个无意的空格字符,我认为系统现在不会让我编辑它。 Usuario. IkebanaUser.list() 应该是 Usuario.IkebanaUser.list()
  • 我尝试保存一个对象 Usuarios ,查看控制器类 UsuarioController { def index() { def b = new Usuario(nombre: "Sorrento", apellido: "Sama") System.out.println( b.nombre) b.save() b.save(flush: true) def c = new Usuario(nombre: "Carolino",apellido: "Ruty") c.save() c.save(flush: true) if i尝试编码 def b = new Usuario(nombre: "Sorrento", apellido: "Sama") b.IkebanaUser.save() 这个代码没有运行 /* 看图

标签: grails


【解决方案1】:
IkebanaUser:
     pooled: true
     driverClassName: "org.sqlite.JDBC"
     username: ''
     password: ''

那应该是dataSources 的孩子,像这样:

dataSources:
    IkebanaUser:
         pooled: true
         driverClassName: "org.sqlite.JDBC"
         username: ''
         password: ''

【讨论】:

  • 项目中有很多可能出错的地方。我在您显示的代码中看到的所有错误都是上述配置问题。如果您可以向 github.com/jeffbrown/robertoriartemultipledatasources/blob/… 发送 PR 来证明问题,我很乐意帮助您解决这个问题。祝你好运。
  • 午餐这个错误:嵌套异常是org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment],非常感谢;
  • "this the class of code github.com/marcosRiarte/StackOVerFlowMultipleDataSource/tree/..." - 一个问题是您的事务管理方式没有完成。如果您可以将该代码放入与我上面链接的类似的可运行 Grails 应用程序中,我很乐意向您发送一份 PR,说明如何使其工作。我上面链接的仓库中的代码确实有效。
【解决方案2】:

问题很多

构建 graddle 需要

  compile group: 'com.zsoltfabok', name: 'sqlite-dialect', version: '1.0'

控制器方法需要像 jeff 所说的 @Transactional("IkebanaUser") 并保存对象 b.IkebanaUser.save(flush:true) 然后看看这个

 class UsuarioController {
   @Transactional("IkebanaUser")
    def index() {
       Usuario b = new Usuario(nombre:"Mañana", apellido: "ros" )
       System.out.println(b.apellido)
       b.IkebanaUser.save(flush:true)
   }

和 application.yml 看起来像 jeef 说的

dataSource:
   pooled: true
   jmxExport: true
   driverClassName: "org.postgresql.Driver"
   username: "postgres"
    password: "postgres"

dataSources:
    IkebanaUser:
           pooled: true
           driverClassName: "org.sqlite.JDBC"
           username: ''
          password: ''

environments:
    development:
        dataSource:
           dbCreate: update
           url: jdbc:postgresql://localhost:5432/IkebanaERP

    dataSources:
        IkebanaUser:
            url: jdbc:sqlite:/home/marcos/IkebanaUser
            dbCreate: update
test:
    dataSource:
        url: jdbc:sqlite:/home/marcos/IkebanaUser
        dbCreate: update
production:
    dataSource:
        url: jdbc:sqlite:/home/marcos/IkebanaUser
        dbCreate: update
        properties:
            jmxEnabled: true
   .......... 

【讨论】:

猜你喜欢
  • 2015-09-03
  • 2014-04-16
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多