方法一:在conf/spring目录下添加一个resources.xml,配置一个dataSource数据源,覆盖DataSource.groovy中的数据源
- environments {
- production {
- grails.serverURL = "http://www.changeme.com"
- def dbConfLoc = System.getProperty('gis.config.location', null)
- if (!dbConfLoc) {
- dbConfLoc = "file:./gis.properties"
- }
- grails.config.locations = [ dbConfLoc ]
- def props = new Properties()
- println "Loading system properties from ${grails.config.locations[0]}"
- def f = new File(grails.config.locations[0])
- if (f.exists()) {
- props.load( f.newInputStream() )
- }
- }
- development {
- grails.serverURL = "http://localhost:8080/${appName}"
- }
- test {
- grails.serverURL = "http://localhost:8080/${appName}"
- }
- }
gis.properties文件内容如下(以oracle为例)
- dataSource.pooled=true
- dataSource.driverClassName=oracle.jdbc.OracleDriver
- dataSource.username=数据库名称
- dataSource.password=数据库密码
- dataSource.url=地址
[color=darkred]
- Java代码
- import org.springframework.core.io.support.PropertiesLoaderUtils
- import org.springframework.core.io.ClassPathResource
- def properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("datasource.properties"))
- dataSource {
- pooled = true
- driverClassName = properties.getProperty("driverClass")
- username = properties.getProperty("username")
- password = properties.getProperty("password")
- url = properties.getProperty("url")
- }
[/color]
即在DataSource.groovy中自己读取datasource.properties进行配置。
其实应该也可以在Config.groovy中添加语句
grails.config.locations = [ "classpath:datasource.properties"]
然后在DataSource.groovy使用grailsApplication.config对象读取配置,但由于此时 grailsApplication还没有初始化完毕,对外部的properties文件没有处理,暂时取不到 datasource.properties的配置
datasource.propterties 文件内容如下
- pooled=true
- driverClassName=oracle.jdbc.OracleDriver
- username=sde
- password=sde
- url=jdbc:oracle:thin:@192.168.1.206:1521:orcl
方法三:使用JNDI数据源。 DataSource.groovy
Java代码
dataSource {
jndiName="java:comp/env/jdbc/test"
pooled = true
logSql = false
}
dataSource { jndiName="java:comp/env/jdbc/test" pooled = true logSql = false }
服务器端配置,tomcat6为例
Xml代码
<GlobalNamingResources>
<Resource name="testDatabase" auth="Container" type="javax.sql.DataSource"
maxActive="20" maxIdle="30" maxWait="10000"
username="test" password="NdV" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.20.251:1521:test" />
</GlobalNamingResources>
...
<Context path="/test" docBase="D:\Worksapce\test" cookies="false">
<ResourceLink name="jdbc/test"
global="testDatabase"
type="javax.sql.DataSource"/>
</Context>
<GlobalNamingResources> <Resource name="testDatabase" auth="Container" type="javax.sql.DataSource" maxActive="20" maxIdle="30" maxWait="10000" username="test" password="NdV" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@192.168.20.251:1521:test" /> </GlobalNamingResources> ... <Context path="/test" docBase="D:\Worksapce\test" cookies="false"> <ResourceLink name="jdbc/test" global="testDatabase" type="javax.sql.DataSource"/> </Context>
为了使程序可以在grails控制台即jetty服务器下运行测试。需要在web-app/WEB-INF目录下添加jetty-env.xml,配置jetty下的jndi数据源
Xml代码
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource">
<Arg>jdbc/test</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">oracle.jdbc.driver.OracleDriver</Set>
<Set name="url">jdbc:oracle:thin:@192.168.20.251:1521:test</Set>
<Set name="username">test</Set>
<Set name="password">NdV</Set>
</New>
</Arg>
</New>
</Configure>