Reputation: 1
I have created a Grails 3.3.8
project. The datasource (Oracle) configuration is successfully done in the application.yml
file. But when I am using the datasource in my groovy class I am facing the following error:
Caused by GroovyRuntimeException: Ambiguous method overloading for method groovy.sql.Sql#<init>.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[interface java.sql.Connection]
[interface javax.sql.DataSource]
My code is :
package DATABASE_CONF
import groovy.sql.Sql
/**
*
* @author CESC
*/
public class dbconn {
def dataSource
public String showname2() {
def sql = new Sql(dataSource)
def namestr = ""
sql.eachRow('select * from TEST_TB') {
tp ->
namestr = namestr + "<br>" + tp.NAME
}
sql.close()
return namestr
}
}
Upvotes: 0
Views: 809
Reputation: 1
I was getting this exact same error message. What I found to work was manually injecting the dataSource bean like this:
class SomeClass {
def dataSource = grails.util.Holders.applicationContext.getBean("dataSource")
def myMethod {
Sql sql = new Sql(dataSource)
//Enter a SQL query
}
}
Upvotes: 0
Reputation: 20699
Your class can not resolve the dataSource
property, hence a null and ambigous method overloading exception.
The reason for that might be that your class resides in src
folder and does NOT take part in bean (auto-)wiring. There are several options to deal with the issue:
so it will get the
dataSource` injected automatically (recommended)dataSource
as an argument of showname2()
method and pass it explicitely upon each call.Upvotes: 2