Grails 1.3.5:如何配置Datasource.groovy以连接到MySQL或SQL Server [英] Grails 1.3.5: How to configure Datasource.groovy to either connect to MySQL or SQL Server

查看:119
本文介绍了Grails 1.3.5:如何配置Datasource.groovy以连接到MySQL或SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的grails应用程序可以针对MySQL或SQL Server运行。我想在application.properties文件中添加一个新属性,如
database.type = MySQL //或者它可以是SQLSERVER

我如何在application.properties文件中添加新属性在Datasource.groovy中获取此属性,以便如果它是MySQL,则连接到MySQL服务器,或者它是SQLSERVER我连接到SQL Server?

这是做到这一点的正确方法吗?我怎么能做到这一点?



编辑:在阅读和搜索选项后,我想通过以下方式解释。
我在/ grails-app / conf /文件夹中创建了config.properties文件。

  driverClassName = com.microsoft .sqlserver.jdbc.SQLServerDriver 
dataSource.url = jdbc:sqlserver:// localhost:1433; databaseName = testDB
dataSource.username = sa
dataSource.password = sa

同时更新了Config.groovy

  grails.config.locations = [classpath:config.properties] 

但是我得到以下错误

无法加载指定的配置位置classpath:config.properties:class path resource [config.properties]无法打开,因为它不存在



但是,如果使用

  grails.config.locations = [file:E:/工作区/ SpringSource2.3.3 / GRAILS_PRO / config.properties] 

应用程序启动并能够连接到数据库。我不想使用静态文件路径。使用classpath时出了什么问题?



对'run-app'和'war'模式都有同样的问题,即同一个文件不存在错误。



第二编辑:



在使用classpath并且无法使它工作的这么多挫败之后,我使用了环境属性。由于服务器将定义CATALINA_HOME,因此我使用以下内容来构建外部配置文件的路径。

  def CATALINA_HOME =CATALINA_HOME 
def CONFIG_FILE_NAME =db_config.properties
if(!grails.config.locations ||!(grails.config.locations instanceof List)){
grails.config.locations = []


if(System.getenv(CATALINA_HOME)){
def fullPath = System.getenv(CATALINA_HOME)+ File.separator +webapps+ File.separator + $ {appName}+ File.separator +WEB-INF+ File.separator +classes+ File.separator + CONFIG_FILE_NAME
grails.config.locations<< file:+ fullPath
} else {
println缺少配置!
}

上述解决方案是特定于Tomcat的。我真的很想看到classpath版本的工作!



谢谢。
问候,
Jay Chandran。

解决方案

把它放在正在运行的用户的主目录tomcat的实例或应该为您的应用程序创建一个.grails目录,将它放在那里


The grails application I am developing could run against MySQL or SQL Server. I want to add a new property in application.properties file say
database.type=MySQL // or it could be SQLSERVER

How do I get this property in Datasource.groovy so that if it is MySQL I connect to MySQL server or it is SQLSERVER I connect to SQL Server?

Is this the correct way to do it? How can I do it?

EDIT: After reading and searching for options I figured the following way explained. I have created config.properties file in /grails-app/conf/ folder.

driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
dataSource.url = jdbc:sqlserver://localhost:1433;databaseName=testDB
dataSource.username = sa
dataSource.password = sa

Also updated Config.groovy

grails.config.locations = ["classpath:config.properties"]

But I get the below error
Unable to load specified config location classpath:config.properties : class path resource [config.properties] cannot be opened because it does not exist

But if use

grails.config.locations = ["file:E:/workspace/SpringSource2.3.3/GRAILS_PRO/config.properties"]

The application starts up and is able to connect to the database. I don't want to use static file path. What is wrong when using classpath?

Have the same issue for both 'run-app' and 'war' mode i.e. same file does not exist error.

2nd EDIT:

After so much frustration of using classpath and not able to get it to work, I resorted to using environment property. Since server will have CATALINA_HOME defined, I used the below to build the path for external configuration file.

def CATALINA_HOME = "CATALINA_HOME"
def CONFIG_FILE_NAME = "db_config.properties"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
 grails.config.locations = []
}

if(System.getenv(CATALINA_HOME)) {
   def fullPath = System.getenv(CATALINA_HOME) + File.separator + "webapps" + File.separator + "${appName}" + File.separator + "WEB-INF" + File.separator + "classes" + File.separator + CONFIG_FILE_NAME
   grails.config.locations << "file:" + fullPath
} else {
     println "Missing configuration!"
 } 

The above solution is Tomcat specific. I really would like to see classpath version working!

Thank You. Regards, Jay Chandran.

解决方案

put it in the home directory of the user which is running the instance of tomcat or there should be a .grails directory created for your app , put it under there

这篇关于Grails 1.3.5:如何配置Datasource.groovy以连接到MySQL或SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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