2013-01-17 48 views
3

我正在開發一個應用程序,需要在大型機上調用DB2函數來獲取一個id。使用Spring時的ConnectionPooling配置jdbcTemplate

在我的Spring應用程序上下文,我定義我的JDBC模板查詢DB2在zOS正是如此:

<bean id="jdbcTemplateDB2" 
     class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="dataSourceDB2"/> 
    </bean> 

我再定義數據源如下:

<bean id="dataSourceDB2" 
    class="com.ibm.db2.jcc.DB2DataSource"> 
     <property name="serverName" value="hostname" /> 
     <property name="portNumber" value="portno"/> 
     <property name="databaseName" value="dbname"/> 
     <property name="driverType" value="4"/> 
     <property name="user" value="userid"/> 
     <property name="password" value="password"/> 
</bean> 

上述作品。 但是,查看ibm的db2jcc.jar文件,我看到一個類爲datasource WITH連接池 - com.ibm.db2.jcc.DB2ConnectionPoolDataSource。於是,我就用那個,而不是上面的一個 - 像這樣:

<bean id="dataSourceDB2" 
    class="com.ibm.db2.jcc.DB2ConnectionPoolDataSource"> 
     <property name="serverName" value="hostname" /> 
     <property name="portNumber" value="portno"/> 
     <property name="databaseName" value="dbname"/> 
     <property name="driverType" value="4"/> 
     <property name="user" value="userid"/> 
     <property name="password" value="password"/> 
</bean> 

然而,使用DB2ConnectionPoolDataSource給我下面的錯誤。

Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found 

完整的堆棧跟蹤低於

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classUniqueIdDaoImpl_v2': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplateDB2' defined in class path resource [META-INF/spring/applicationContext-db2.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.ibm.db2.jcc.DB2ConnectionPoolDataSource' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found 
     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609) 
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469) 
     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
     at com.slma.euclid.core.dao.MainDB2.main(MainDB2.java:18) 
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplateDB2' defined in class path resource [META-INF/spring/applicationContext-db2.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.ibm.db2.jcc.DB2ConnectionPoolDataSource' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818) 
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735) 
     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:439) 
     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:417) 
     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559) 
     at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150) 
     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:304) 
     ... 13 more 
    Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.ibm.db2.jcc.DB2ConnectionPoolDataSource' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found 
     at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485) 
     at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516) 
     at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118) 
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) 
     ... 27 more 
    Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found 
     at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241) 
     at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470) 
     ... 33 more 

我在做什麼錯?在實例化Spring JdbcTemplate時使用連接池數據源的正確方法是什麼?

任何幫助表示讚賞。

-SGB

回答

3

你讀過這一個:http://forum.springsource.org/showthread.php?66142-How-to-use-javax-sql-datasource-with-a-ConnectionPoolDataSource

它說,如果正確使用com.ibm.db2.jcc.DB2DataSource已經是一個連接池。

+0

查看類文件,它看起來不像com.ibm.db2.jcc.DB2DataSource實現連接池並且似乎不推薦使用。所以希望有另一種解決方案。我確實看到您發佈的鏈接 - 謝謝 - 但在閱讀完該主題後,我仍然不確定如何繼續:)。在該頁面的最後,原始海報提到他們最終創建了自己的池化機制。我目前使用com.ibm.db2.jcc.DB2DataSource。現在,我正在開放此線程(未回覆),希望有更好的解決方案的人可以參與進來。 – SGB

+0

找到IBM的此鏈接:http://publib.boulder.ibm.com/infocenter/db2luw/v9/ index.jsp?topic =/com.ibm.db2.udb.doc/ad/rjvdsprp.htm這似乎表明DB2DataSource是連接池。接受你的答案。 – SGB

相關問題