Jdbc 在扩展源对象上使用JNDI

Jdbc 在扩展源对象上使用JNDI,jdbc,jndi,connection-pooling,tomcat5.5,Jdbc,Jndi,Connection Pooling,Tomcat5.5,我想使用jndi在jdbc连接上实现连接池,但我还想使用扩展的BasicDataSource对象。我对使用tomcat 5.5有限制。要求对jndi上下文资源属性中提供的密码进行加密和解密 我正在尝试实现类似于本文的东西 这是我的servlet,它可以在不扩展源代码的情况下完美地工作 public class ReturnBlobOld extends HttpServlet { private static final long serialVersionUID = 1L;

我想使用jndi在jdbc连接上实现连接池,但我还想使用扩展的BasicDataSource对象。我对使用tomcat 5.5有限制。要求对jndi上下文资源属性中提供的密码进行加密和解密

我正在尝试实现类似于本文的东西

这是我的servlet,它可以在不扩展源代码的情况下完美地工作

public class ReturnBlobOld extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private org.apache.tomcat.dbcp.dbcp.BasicDataSource ds = null;
    // this is my extended DS which is not working
    //private custompages.NewBasicDataSource ds = null;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        getDataSource();
    }

    public void getDataSource() {
        Context ctxt = null;
        try {
            ctxt = new InitialContext();
            ds = (BasicDataSource) ctxt.lookup("java:comp/env/jdbc/mysql");
          //ds = (NewBasicDataSource) ctxt.lookup("java:comp/env/jdbc/mysql");          
        ctxt.close();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public void doGet(....){
           ........
          Connection connection = null;
          Statement statement = null;
        ResultSet res = null;
    try {
        connection = ds.getConnection();
        statement = connection.createStatement();
           }
     ................
这是我的扩展BasicDataSource类

package custompages;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class NewBasicDataSource extends BasicDataSource {

    protected synchronized DataSource createDataSource() throws SQLException {
        String decryptedPassword = decryptPassword( super.getPassword() );
        super.setPassword( decryptedPassword );
        return super.createDataSource();
    }
    private String decryptPassword( String password ) {
        return "abcdef";//logic to decrypt current password
    }
}
这是我的context.xml文件

<context>
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="abcdef"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.2.2:3306/world"
maxActive="1"
maxIdle="1"
maxWait="5000"
timeBetweenEvictionRunsMillis="5000"
minEvictableIdleTimeMillis="100"
removeAbandoned="true"
removeAbandonedTimeout="3"/>
</context>
请建议扩展该类以与jndi一起使用的正确方法, 这是我现在正在做的另一个选择。我已经删除了JNDI资源,并直接在servlet中创建了一个新的BasicDataSource对象,而不是从JNDI工厂方法调用中生成它。为了自定义,我可以使用属性文件参数化属性吗

    public void getDataSource() {

        ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUsername("root");
        ds.setPassword("abcde");
        ds.setUrl("jdbc:mysql://192.168.2.2:3306/world");
        ds.setMaxActive(8);
        ds.setMaxIdle(8);
        ds.setMaxWait(10000);
        ds.setTimeBetweenEvictionRunsMillis(300000);
        ds.setMinEvictableIdleTimeMillis(1800000);
        ds.setRemoveAbandonedTimeout(300);
        ds.setRemoveAbandoned(true);
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource
custompages.ReturnBlobOld.getDataSource(ReturnBlobOld.java:39)
custompages.ReturnBlobOld.init(ReturnBlobOld.java:32)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:881)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:674)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:541)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
java.lang.Thread.run(Thread.java:595)
    public void getDataSource() {

        ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUsername("root");
        ds.setPassword("abcde");
        ds.setUrl("jdbc:mysql://192.168.2.2:3306/world");
        ds.setMaxActive(8);
        ds.setMaxIdle(8);
        ds.setMaxWait(10000);
        ds.setTimeBetweenEvictionRunsMillis(300000);
        ds.setMinEvictableIdleTimeMillis(1800000);
        ds.setRemoveAbandonedTimeout(300);
        ds.setRemoveAbandoned(true);