在Grails中使用多个数据源时使用错误的数据源进行验证? [英] Validation using wrong datasource when using multiple datasources in Grails?

查看:108
本文介绍了在Grails中使用多个数据源时使用错误的数据源进行验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Grails 2.2.1,当我们试图在一个绑定到多个数据源的域类上执行一个CRUD操作时,看到了一个问题。



是我们的静态映射和一些操作的结果:

在这种情况下,CAR表只存在于d2中。

  class Car {
static mapping = {
datasources(['d1','d2'])
}
}

Car.d2.listAll()//此代码工作并从d2返回数据
carObject.d2.save()//此代码失败,表或视图不存在

现在,如果我将表格添加到d1

  class Car {
static mapping = {
datasources(['d1','d2'])
}


Car.d2.listAll()//此代码工作并从d2返回数据
carObject.d2.save()//此代码现在可以工作并插入一行进入d2

的表格在你列出的第一个数据表中必须存在多个数据源,有没有人知道解决这个问题的方法?
$ b UPDATE 10/27

我曾尝试注入实际的数据源并使用它来执行CRUD操作作为解决方法。不幸的是,grails不允许你使用注入的数据源来指定你想要更改的地方。

链接:

不仅表格必须存在于列出的第一个数据源中,而且似乎也在检查数据。它不会允许我用d1中存在的键将记录插入到d2中。



UPDATE 2 10/27



经过研究,似乎验证是针对列出的第一个数据源运行的,那么实际的操作将在指定的dataSource上执行。



它现在工作,如果我通过验证false:

  carObject.d2.save(validate:false)

这个工作的唯一问题显然是我失去了验证。

解决方案

我试着为会话工厂bean创建新会话。在此之后做object.save()。看起来像是有效的。请参阅示例:

  def ctx = grailsApplication.mainContext 

ctx.getBeanNamesForType(SessionFactoryImplementor.class)。每个{

SessionFactoryImplementor sfi = ctx.getBean(it)
GrailsS​​essionContext gsc = new GrailsS​​essionContext(sfi)

if(!TransactionSynchronizationManager.getResource(gsc.sessionFactory) ){
def newSession = gsc.sessionFactory.openSession()
def sessionHolder = new SessionHolder(newSession)
TransactionSynchronizationManager.bindResource(gsc.sessionFactory,sessionHolder)
}
}


We are using Grails 2.2.1 and are seeing an issue when we try and perform a CRUD operation on a domain class that is tied to multiple datasources.

Here is what our static mapping looks like and the results of some operations:
In this scenario the CAR table only exists in d2.

class Car {
   static mapping = {
      datasources(['d1','d2'])
   }
}

Car.d2.listAll() //This code works and return data from d2
carObject.d2.save() //This code fails with a Table or View does not exist

Now if I add the table to d1

class Car {
   static mapping = { 
      datasources(['d1','d2'])
   }
}

Car.d2.listAll() //This code works and return data from d2
carObject.d2.save() //This code now works and inserts a row into table in d2

So it seems when you have multiple datasources the table has to exist in the first one you have listed, does anyone know a way around this?

UPDATE 10/27

I have tried injecting the actual datasource and using it to perform the CRUD operations as a workaround. Unfortunately grails does not allow you to use an injected datasource to specify where you want your changes to go.

Link: How to use injected dataSource in Grails to perform operations?

Issue seems to be worst than originally anticipated. Not only does the table have to exist in the first datasource listed, but it seems to be checking the data as well. It will not allow me to insert a record into d2 with a key that exists in d1.

UPDATE 2 10/27

After researching it seems that the validation is being run against the first dataSource listed, then the actual operation is being performed on the dataSource specified.

It now works if i pass in validate false:

carObject.d2.save(validate:false) 

The only problem with this work around obviously is that I lose the validation.

解决方案

I've tried to create new session for session factory bean. And after this do object.save(). Looks like it works. See example:

def ctx = grailsApplication.mainContext

        ctx.getBeanNamesForType(SessionFactoryImplementor.class).each {

            SessionFactoryImplementor sfi = ctx.getBean(it)
            GrailsSessionContext gsc = new GrailsSessionContext(sfi)

            if(!TransactionSynchronizationManager.getResource(gsc.sessionFactory)) {
                def newSession = gsc.sessionFactory.openSession()
                def sessionHolder = new SessionHolder(newSession)
                TransactionSynchronizationManager.bindResource(gsc.sessionFactory, sessionHolder)
            }
        }

这篇关于在Grails中使用多个数据源时使用错误的数据源进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆