views:

724

answers:

3

Hi!

I have a groovy application which is using an Oracle DB as DataSource.

In DataSource.groovy I've set:

dataSource {
pooled = true
driverClassName = "oracle.jdbc.driver.OracleDriver"
username = "scott"
password = "tiger
//loggingSql = true
}

For some performance reasons at some points I am accesing the DB using sql in the following way:

def sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger", "oracle.jdbc.driver.OracleDriver")

That is, username and password are hardwired twice in the application. My question is if it possible to address in my application to the attributes username and password already set in the DataSource.groovy.

Thanks in advance,

Luis

A: 

Can't you just do the following? (assuming that dataSource is a variable in scope)

def sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE", dataSource.username, dataSource.password, dataSource.driverClassName)
chillitom
dataSource is not a variable in the scope of this page.
Luixv
+1  A: 

You may create Sql class by datasource, for example

def sql = new Sql(myDataSource)

where myDataSource - object of class DataSource (you can get your DS declared in DataSource.groovy)

Alexey Sviridov
Thanks for your answer. Could you please tell me how to define "myDataSource". In fact a DataSource.groovy I have already defined a dataSource. My question is how to access to this variable. Thanks in advance! Luis
Luixv
I'm don't try myself. try def myDataSource = ConfigurationHolder.config.dataSource, give me back if it ok, them i'm correct the answer
Alexey Sviridov
Unfortunately your proposal throws an exception:org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method <init>() to invoke from this list: public groovy.sql.Sql#<init>(javax.sql.DataSource) public groovy.sql.Sql#<init>(groovy.sql.Sql) public groovy.sql.Sql#<init>(java.sql.Connection)Anyway don't worry. I got a solution based in your proposal. Thanks!
Luixv
+1  A: 

The solution is to add some imports

import javax.sql.DataSource
import groovy.sql.Sql
import org.codehaus.groovy.grails.commons.ConfigurationHolder

and the following code:

def _url      = ConfigurationHolder.config.dataSource.url
def _username = ConfigurationHolder.config.dataSource.username
def _password = ConfigurationHolder.config.dataSource.password
def _driver   = ConfigurationHolder.config.dataSource.driverClassName
def sql = Sql.newInstance(_url, _username, _password, _driver)

def query = "<your SQL query>"
sql.eachRow(query){
    println "ID: " + it.id // Whatever you need
}
Luixv
Sorry for my annoing :) but what about def sql = new Sql(ConfigurationHolder.config.dataSource as DataSource)If it works it's better then newInstance because you get connection through DataSource class which is usually pooled and better configured.
Alexey Sviridov
def sql = new Sql(ConfigurationHolder.config.dataSource as DataSource) don't work. We get a MAP with all dataSource informations, arguments not matching with new Sql.Solution add by Luixv works, thanks !
Fabien Barbier