我正在尝试使用此代码创建一个 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