Grails 1.3.5: How to configure Datasource.groovy to either connect to MySQL or SQL Server
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 SQLSERVERHow 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 existBut 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 classpa开发者_Go百科th 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
精彩评论