有人可以请我摆脱苦难,并解释为什么会发生这种情况以及如何解决它。


一个tomcat数据源

<Resource name="MY_JDBC_NAME" auth="Container"
  driverClass="oracle.jdbc.OracleDriver"
  jdbcUrl="MyURL"
  user="MyUser"
  password="MyPassword"
  factory="org.apache.naming.factory.BeanFactory"
  type="com.mchange.v2.c3p0.ComboPooledDataSource"
  maxPoolSize="25"
  minPoolSize="5"
  acquireIncrement="1"
  acquireRetryAttempts = "2"
  idleConnectionTestPeriod = "60"
  testConnectionOnCheckout = "true"
  preferredTestQuery = "SELECT 1 FROM DUAL"


/>
查找

ComboPooledDataSource ds = (ComboPooledDataSource) ctx.lookup(     "java:comp/env/MY_JNDI_NAME");



这导致莫名其妙

java.lang.ClassCastException: com.mchange.v2.c3p0.ComboPooledDataSource cannot be cast to com.mchange.v2.c3p0.ComboPooledDataSource


提前致谢

最佳答案

我正在用这个
可能对您也很完美,尝试一下,

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


....

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("MY_JDBC_NAME");

09-27 02:03