引起:java.lang.ClassCastException:org.apache.commons.dbcp.PoolingDataSource无法强制转换为javax.sql.DataSource

时间:2016-05-03 10:47:10

标签: java jdbc aem

我正在尝试在AEM中的OSGi配置下配置DataSource,如下所示:

jdbc.password="secrer"
jdbc.driver.class="com.mysql.jdbc.Driver"
datasource.name="ta"
jdbc.connection.uri="jdbc:mysql://localhost:3306/ta"
jdbc.validation.query=""
datasource.svc.properties=[""]
default.readonly=B"false"
default.autocommit=B"true"
jdbc.username="aem"
pool.size=L"1000"
pool.max.wait.msec=L"999999"

我的DataSourceProvider如下所示

import javax.sql.DataSource;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
    import com.day.commons.datasource.poolservice.DataSourceNotFoundException;
    import com.day.commons.datasource.poolservice.DataSourcePool;

    public class DataSourceProvider extends LocalSessionFactoryBean {

        private DataSourcePool myDataSourcePool;

        public void setMyDataSourcePool(DataSourcePool myDataSourcePool) throws DataSourceNotFoundException {
            this.myDataSourcePool = myDataSourcePool;
            this.setDataSource((DataSource) this.myDataSourcePool.getDataSource("ta")); //line with issue
        }
    }

一个“问题线”我正面临以下问题。有没有办法从org.apache.commons.dbcp.PoolingDataSource获取SQL DataSource。

Caused by: java.lang.ClassCastException: org.apache.commons.dbcp.PoolingDataSource cannot be cast to javax.sql.DataSource
    at com.test.datasource.DataSourceProvider.setMyDataSourcePool(DataSourceProvider.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.aries.blueprint.utils.ReflectionUtils$MethodPropertyDescriptor.internalSet(ReflectionUtils.java:628)
    at org.apache.aries.blueprint.utils.ReflectionUtils$PropertyDescriptor.set(ReflectionUtils.java:378)
    at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:939)
    ... 23 common frames omitted

Spring Bean配置

<reference id="dsPoolService"
        interface="com.day.commons.datasource.poolservice.DataSourcePool" />

    <bean id="sessionFactory" class="com.australia.datasource.DataSourceProvider"> 
        <property name="myDataSourcePool" ref="dsPoolService"/>
    </bean>

1 个答案:

答案 0 :(得分:-4)

通过查看上面的错误,您似乎缺少以下jar文件

org.apache.commons / org.apache.commons.dbcp.jar

尝试添加以上jar

获取dataSource,试试这段代码

conn = new GenericObjectPool(null);

conn.setTestOnBorrow(true);

dataSource = new PoolingDataSource(conn);