使用grails连接到多个oracle数据源的问题

时间:2014-10-29 19:47:24

标签: database oracle grails datasource

如果这是重复,请原谅我。我搜索并搜索过。但是我有一些东西在过去的几天里一直困扰着我。我已经研究过,并没有找到解决我问题的解决方案。我正在处理需要访问多个数据库的应用程序。如果我只使用一个数据库并将其注入到具有数据源对象的代码中,那么它对我使用的任何数据库都可以。但是,如果我尝试添加另一个数据源,那就是问题开始的时候。我已经尝试将配置添加到datasource.groovy并且我已经尝试过硬编码'连接。

硬编码方法

正如您所看到的,我尝试过使用下面的各种方法,所有人都给了我错误。 ojdbc6.jar正确加载,因为我已经通过故意拼写错误拼写驱动程序类名称并获得了类找不到错误。

import java.sql.*

def index(){
    ...
    def db = [url:'jdbc:oracle:oci8:@someotherdatabase.whatever.com', user:'other_user', password:'other_pw', driver:'oracle.jdbc.OracleDriver']
    def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
    //      Sql.loadDriver("oracle.jdbc.driver.OracleDriver")
    //      Sql.loadDriver("oracle.jdbc.OracleDriver")
    //      def sql2 = Sql.newInstance('jdbc:oracle:oci8:@someotherdatabase.whatever.com','other_user','other_pw','oracle.jdbc.driver.OracleDriver')
    //      def sql = Sql.newInstance("jdbc:oracle:oci8:@someotherdatabase.whatever.com", "other_user", "other_pw", "oracle.jdbc.OracleDriver")
    //      DriverManager.registerDriver(new oracle.jdbc.OracleDriver())
    //      Class.forName("oracle.jdbc.driver.OracleDriver")
    //      Connection conn = DriverManager.getConnection('jdbc:oracle:oci8:@someotherdatabase.whatever.com','other_user','other_pw')
    //      Sql sql = new Sql(conn)
    ...
}

错误:

Error |
2014-10-28 11:38:06,766 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - SQLException occurred when processing request: [GET] /ApplicationPortal/home
No suitable driver found for jdbc:oracle:oci8:@someotherdatabase.whatever.com. Stacktrace follows:
Message: No suitable driver found for jdbc:oracle:oci8:@someotherdatabase.whatever.com
   Line | Method
->> 602 | getConnection in java.sql.DriverManager
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   185 | getConnection in     ''
|   192 | index . . . . in com.myapp.HomeController$$EOu0LeDV
|   200 | doFilter      in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|    63 | doFilter . .  in grails.plugin.cache.web.filter.AbstractFilter
|   895 | runTask       in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run . . . . . in     ''
^   662 | run           in java.lang.Thread

Datasource.groovy配置方法

我在datasource.groovy尝试了不同的配置。目前这就是它的样子:

dataSource {
    dialect = org.hibernate.dialect.Oracle10gDialect
    driverClassName = 'oracle.jdbc.driver.OracleDriver'
    pooled = true
    properties {
        maxActive = -1
        minEvictableIdleTimeMillis=1800000
        timeBetweenEvictionRunsMillis=1800000
        numTestsPerEvictionRun=3
        testOnBorrow=true
        testWhileIdle=true
        testOnReturn=true
        validationQuery="SELECT 1 from dual"
    }
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
    showsql = true
}

environments {
    development {
        dataSource {
            url = "jdbc:oracle:oci:@database.whatever.com"
            username="data_user"
            password = "some_pw"
        }
        dataSource_task {
            url = "jdbc:oracle:oci8:@someotherdatabase.whatever.com"
            username="other_user"
            password = "other_pw"
        }
    }

}

我收到了错误。我不知道它为什么要尝试使用h2数据库。我需要它连接到oracle数据库。

Error |
2014-10-29 13:30:53,500 [localhost-startStop-1] ERROR pool.ConnectionPool  - Unable to create initial connections of pool.
Message: Driver:org.h2.Driver@42b1b290 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
   Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Error |
2014-10-29 13:30:53,547 [localhost-startStop-1] ERROR pool.ConnectionPool  - Unable to create initial connections of pool.
Message: Driver:org.h2.Driver@2d2c1472 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
   Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Error |
2014-10-29 13:30:53,621 [localhost-startStop-1] ERROR pool.ConnectionPool  - Unable to create initial connections of pool.
Message: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
   Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Error |
2014-10-29 13:30:53,634 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Error creating bean with name 'transactionManager_task': Cannot resolve reference to bean 'sessionFactory_task' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory_task': Cannot resolve reference to bean 'hibernateProperties_task' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties_task': Cannot resolve reference to bean 'dialectDetector_task' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector_task': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
Message: Error creating bean with name 'transactionManager_task': Cannot resolve reference to bean 'sessionFactory_task' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory_task': Cannot resolve reference to bean 'hibernateProperties_task' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties_task': Cannot resolve reference to bean 'dialectDetector_task' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector_task': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
   Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory_task': Cannot resolve reference to bean 'hibernateProperties_task' while setting bean property 'hibernateProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateProperties_task': Cannot resolve reference to bean 'dialectDetector_task' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector_task': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'hibernateProperties_task': Cannot resolve reference to bean 'dialectDetector_task' while setting bean property 'properties' with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dialectDetector_task': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'dialectDetector_task': Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Caused by MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Caused by SQLException: Driver:org.h2.Driver@17898b92 returned null for URL:jdbc:oracle:oci8:@someotherdatabase.whatever.com
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   895 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   918 | run      in     ''
^   662 | run . .  in java.lang.Thread
Error |
Forked Grails VM exited with error

设定: Grails 2.3.7 Oracle 11g

修改 数据源方法的问题不在于我使用了多少数据源。我和这个名字有关系。如果我只使用一个datasouce作为datasource_other,它将抛出错误。但数据源有效。

1 个答案:

答案 0 :(得分:0)

就数据源配置方法而言,我想到了一个。这是一个愚蠢的错误。我没有将driverClassName添加到dataSource_task。

dataSource_task {
    url = "jdbc:oracle:oci8:@someotherdatabase.whatever.com"
    username="other_user"
    password = "other_pw"
    driverClassName = 'oracle.jdbc.driver.OracleDriver'// <- it needed this
}

我仍然想知道为什么硬编码方法不起作用。