Akaitab Ghatak
Akaitab Ghatak

Reputation: 1

cannot connect with datasource in grails project

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

Answers (2)

LandyCodes
LandyCodes

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

injecteer
injecteer

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:

  1. put your class under /grails-app/servicesso it will get thedataSource` injected automatically (recommended)
  2. declare the dataSource as an argument of showname2() method and pass it explicitely upon each call.
  3. add a wiring mechanizm to your class. I'm not an expert in Spring, but I think it is an annotation and some configuration tweaks.

Upvotes: 2

Related Questions