【问题标题】:DataSource cannot be cast to com.mchange.v2.c3p0.PooledDataSourceDataSource 无法转换为 com.mchange.v2.c3p0.PooledDataSource
【发布时间】:2012-12-24 06:53:11
【问题描述】:

我正在尝试用这段代码创建一个 context.xml:

    ComboPooledDataSource cpds = new ComboPooledDataSource();
    try {
        cpds.setDriverClass( "net.sourceforge.jtds.jdbc.Driver" ); //loads the jdbc driver     
        cpds.setJdbcUrl( "jdbc:jtds:sqlserver://10.38.28.77/MyDB" );   
        cpds.setUser("sa");                                  
        cpds.setPassword("password");   
        cpds.setMaxStatements( 180 );
    } catch (PropertyVetoException e) {
        e.printStackTrace();
    }      
    return cpds.getConnection();

我的意思是,将这个硬编码设置转换为 context.xml,例如:

        <Resource name="jdbc/MyDBHrd" 
         driverClass="net.sourceforge.jtds.jdbc.Driver"
         maxPoolSize="100"
         minPoolSize="15"
         acquireIncrement="1"
         auth="Container"
         maxStatements="50"
         idleConnectionTestPeriod="3600"
         testConnectionOnCheckin="true"
         automaticTestTable="connection_test"
         maxIdleTime="21600"
         factory="org.apache.naming.factory.BeanFactory"
         type="com.mchange.v2.c3p0.ComboPooledDataSource"
         jdbcUrl="jdbc:jtds:sqlserver://192.168.114.130/MyDB"
         user="sa" password="password" />

然而这个 context.xml 在使用时:

    InitialContext cxt = new InitialContext();
    String jndiName = "java:/comp/env/jdbc/MyDBHrd";
    // Look up our data source
    ComboPooledDataSource dataSource = (ComboPooledDataSource) cxt.lookup(jndiName);

抛出:

ClassCastException: org.apache.tomcat.dbcp.dbcp.DataSource cannot be cast to com.mchange.v2.c3p0.PooledDataSource

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    您应该将数据源声明为普通类型,而不是 Combo 类型。您要求 Tomcat 创建一个数据源,它不会为您创建一个。如果您需要将纯数据源包装在其中之一中,请使用 Java。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-19
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多