[英]How can I change schemas using JDBC to connect to MySQL in Grails?
I'm using a spring bean to interact with SQL in a Grails app. 我正在使用Spring bean在Grails应用程序中与SQL交互。 My
resources.groovy
file looks like this: 我的
resources.groovy
文件如下所示:
beans = {
groovySql(groovy.sql.Sql, ref('dataSource')) {}
}
My DataSource.groovy
file has a data source set up (and named dataSource
). 我的
DataSource.groovy
文件设置了一个数据源(并命名为dataSource
)。
The JDBC connection string that I had been using looked something like this: 我一直在使用的JDBC连接字符串如下所示:
jdbc:mysql://my.domain.com/Schema_Number_One
And when I had it like that, everything worked fine. 当我有那样的东西时, 一切都很好。 However, I now need to use more than just the one schema on that server.
但是,我现在需要使用的不仅仅是该服务器上的一个模式。 So I changed my connection string to simply this:
所以我将连接字符串更改为:
jdbc:mysql://my.domain.com/
I figured this would be fine, and that I could just issue a query like USE Schema_Number_Two
to switch. 我认为这会很好,并且我可以发出诸如
USE Schema_Number_Two
类的查询来进行切换。 But something's gone awry. 但是有些地方出了问题。
When I try to run this code: 当我尝试运行此代码时:
def query = "USE Schema_Number_Two"
groovySql.execute(query)
groovySql.commit()
I get the following error in the console: 我在控制台中收到以下错误:
Connection is read-only. 连接是只读的。 Queries leading to data modification are not allowed.
不允许进行导致数据修改的查询。
So... apparently it sees the USE
statement as a write query? 所以...显然,它将
USE
语句视为写查询? My next thought was to try to trick it into thinking it was a read-only statement by doing this: 我的下一个想法是,通过执行以下操作试图诱使它认为它是只读语句:
def query = "USE Schema_Number_Two; SELECT 1"
groovySql.execute(query)
groovySql.commit()
And while that query does execute without error, it doesn't stick. 并且,尽管该查询确实执行没有错误,但它不会停留。 The very next query returned some error message indicating no database had been specified.
下一个查询返回了一些错误消息,指示未指定数据库。
I've also tried adding @Transactional(readOnly=false)
just before the method I'm calling here in my controller, but that doesn't seem to have any impact. 我也尝试在控制器中我在此处调用的方法之前添加
@Transactional(readOnly=false)
,但这似乎没有任何影响。 And I can confirm that I'm using the Grails Transactional annotation. 我可以确认我正在使用Grails事务注释。
As far as I could tell, there was no way to accomplish what I was trying to do here. 据我所知,没有办法完成我在这里试图做的事情。 What I ended up having to do was simply to define separate data sources.
我最终要做的就是简单地定义单独的数据源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.