当我在Apache Karaf 3.0.4中获取BasicDataSource时出现ClassCastException

时间:2015-11-16 04:12:02

标签: java datasource karaf

我正在使用Apache Karaf 3.0.4,我的问题是当我尝试从数据源获取属性时遇到错误(我有一个数据源列表),我正在使用此代码< / p>

    BundleContext bundleContext = FrameworkUtil.getBundle(MyController.class).getBundleContext(); 
    ServiceReference[] serviceReferences =bundleContext.getAllServiceReferences("javax.sql.DataSource", null); 

     //and in this part 

     for (ServiceReference serviceReference : serviceReferences) { 
            Object jndi = serviceReference.getProperty("osgi.jndi.service.name"); 
            if (jndi != null) { 
                serviceReferenceDataSources.add(serviceReference); 
                Object service = bundleContext.getService(serviceReference); 
                //here the code fails 
                BasicDataSource basicDataSource = (BasicDataSource) service; 

             } 
      } 

这是堆栈跟踪

java.lang.ClassCastException: Proxyfcb4dd22_6103_4978_b41b_0bacfb118a66 cannot be cast to org.apache.commons.dbcp2.BasicDataSource 

我需要BasicDataSource对象来获取它们的属性(maxTotal,username,url,validationQuery,initialSize,maxWaitMillis)

最后当我尝试转换为“javax.sql.DataSource”时,这段代码可以工作,但是我需要这个对象BasicDataSource。

编辑1

这是我的蓝图

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
     xsi:schemaLocation="
        http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

  <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}" />
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
      <property name="initialSize" value="${jdbc.initialSize}" />
      <property name="maxTotal" value="${jdbc.maxTotal}" />
      <property name="maxWaitMillis" value="${jdbc.maxWaitMillis}" />
      <property name="validationQuery" value="${jdbc.validationQuery}"/>
      <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
      <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
  </bean>

  <service interface="javax.sql.DataSource" ref="dataSource">
    <service-properties>
        <entry key="osgi.jndi.service.name" value="jdbc/ejemplodb"/>
     <entry key="datasource" value="ejemplodb"/>
    </service-properties>
  </service>

如果我在控制台中打印此代码

  ...
    Object service = bundleContext.getService(serviceReference);
    logger.debug("Service getClass() ", service.getClass());
    System.out.println("Service in println" + service);
  ...

我得到了

  Service getClass() Proxy79d23ceb_cee2_4031_bdc3_13fd572cdf8c
  Service in println org.apache.commons.dbcp2.BasicDataSource@7f753504

我希望你能帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

根据您提出的请求,返回的服务将是javax.sql.DataSource类型。转换为apache的基本数据源在那里无效。但只要你只需要数据库连接就可以使用javax.sql.Datasource吗?

javax.sql.DataSource basicDataSource = (javax.sql.DataSource) service; 

如果您需要服务类型为BasicDataSource,您可能最终会更改bundleContext.getAllServiceReferences("javax.sql.DataSource", null);以查找BasicDatSource,并将BasicDataSource的声明更改为Type { {1}}而不是BasicDataSource类型。