加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用Spring jdbcTemplate时的ConnectionPooling配置

发布时间:2020-12-15 01:22:34 所属栏目:大数据 来源:网络整理
导读:我正在开发一个需要在大型机上调用DB2函数以获得ID的应用程序. 在我的spring应用程序上下文中,我定义了jdbc模板,从而可以在zOS上查询DB2: bean id="jdbcTemplateDB2" class="org.springframework.jdbc.core.JdbcTemplate" property name="dataSource" ref="

我正在开发一个需要在大型机上调用DB2函数以获得ID的应用程序.

在我的spring应用程序上下文中,我定义了jdbc模板,从而可以在zOS上查询DB2:

    <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文件中,我看到具有连接池的数据源类-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

完整的stackTrace如下

    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

最佳答案
您读过这个:http://forum.springsource.org/showthread.php?66142-How-to-use-javax-sql-datasource-with-a-ConnectionPoolDataSource

它说com.ibm.db2.jcc.DB2DataSource如果已正确使用,则已经是一个连接池.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读