2014-10-29 56 views
0

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

硬编码方法

正如你所看到的,我用下面的各种方法试图和所有给我的错误以下。 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:[email protected] 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:[email protected] 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:[email protected] 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:[email protected] 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:[email protected] 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:[email protected] 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:[email protected] 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:[email protected] 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:[email protected] 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:[email protected] 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,它会抛出错误。但数据源的作品。

回答

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 
} 

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