问题
If this is a duplicate, please forgive me. I have searched and searched. But I have something that's been bugging me for the past couple of days. I have researched and haven't found a solution that solves my issues. I'm working on an application that will need to access multiple databases. If I do just one database and inject it into the code with a datasource object, it does just fine with whatever database I use. But if I try to add another datasource, that's when the problems start. I have tried adding the configuration to datasource.groovy and I've tried 'hardcoding' the connection.
Hardcode method
As you can see, I've tried using the various methods below and ALL have given me the error following. The ojdbc6.jar loads correctly, because I have verified by deliberately misspelling the driver class name and got the class not found error.
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:
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 configuration method
I have tried different configurations in datasource.groovy. Currently this is what it looks like:
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"
}
}
}
And I'm getting the error. I don't know why it's trying to use the h2 database. I need it to connect to the oracle database.
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
Setup: Grails 2.3.7 Oracle 11g
EDIT: The problem with the datasource method is not with how many datasources I use. I has to do with the name. If I just use one datasouce as datasource_other, it will throw the error. But datasource works.
回答1:
As far as the datasource configuration method is concerned, I figured that one. It was a boneheaded mistake. I didn't add the driverClassName to the 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
}
I'd still like to know why the hardcode method doesn't work tho.
来源:https://stackoverflow.com/questions/26639300/issues-connecting-to-more-than-one-oracle-datasource-using-grails