简体   繁体   English

如何使用JDBC更改架构以连接到Grails中的MySQL?

[英]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.

相关问题 如何在Spring生态系统中使用JPA休眠连接到不同的mysql数据库模式? - How can I connect to different mysql database schemas using JPA hibernate in Spring ecosystem? 我如何将Android项目连接到mysql JDBC - How can i Connect Android project to mysql JDBC 如何使用SSL JDBC连接到MySQL服务器 - How to connect to MySQL server using SSL JDBC 如何使用 Electron 通过 JDBC 连接到 MySQL 数据库 - How to connect to a MySQL Database with JDBC using Electron 如何在Grails中连接到mysql数据库? - How do I connect to mysql database in Grails? 无法将grails应用程序连接到mySql数据库,无法加载JDBC驱动程序类'com.mysql.jdbc.Driver' - can't connect grails application to a mySql database, Cannot load JDBC driver class 'com.mysql.jdbc.Driver' 我可以在没有任何Web服务的情况下使用JDBC从android连接到mysql吗? - Can i connect to mysql from android without any webservice, And Using JDBC? 我如何使用终端连接到mysql? - How can i connect to mysql using terminal? 如何使用Java连接Mysql? - How can i connect Mysql by using Java? 使用 Entityframework Core,如何在不更改连接字符串的情况下动态更改我连接的 MySql 数据库? - Using Entityframework Core, how can I dynamically change the MySql database, I connect to, without changing the connection string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM