I want to implement connection pooling on my jdbc connection using jndi but i also want to use an extended BasicDataSource object. I have restriction on using tomcat 5.5. The requirement is to encrypt-decrypt password provided in jndi context resource attributes.
I am trying to implement something similar to this post Link
Here is my servlet which works perfectly without extending BasicDataSource.
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();
}
................
Here is my extended BasicDataSource class
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
}
}
Here is my context.xml file
<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>
Here is my WEB-INF resource declaration
<resource-ref>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
My servlet runs fine and i can initialize jdbc pool using above code. But when i try to use my extended class for encrypting -decrypting password then i get exception as below.
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)
Please suggest the proper way of extending this class for use with jndi