wood burning stoves*
The moose likes Struts and the fly likes Error in setting up a datasource in Tomcat 6.0's JNDI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Error in setting up a datasource in Tomcat 6.0 Watch "Error in setting up a datasource in Tomcat 6.0 New topic

Error in setting up a datasource in Tomcat 6.0's JNDI

Vidya Moorthy
Ranch Hand

Joined: Nov 13, 2003
Posts: 57
I am a struts newbie and was trying to access a MySQL 5.1 database from the struts application for a login registration tutorial. After reading a tutorial from Tomcat 6.0 documentation for setting up a datasource in JNDI, I created a test table in the DB and tried to access it from a jsp according to the tutorial. It works fine but when I try to access it from an action, I get an exception message like this

org.apache.struts.action.RequestProcessor processException
WARNING: Unhandled Exception thrown: class java.lang.ClassCastException
Mar 20, 2008 2:48:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet action threw exception
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
at com.yourcompany.struts.action.TestAction.execute(TestAction.java:58)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
This is the code which I used in the action. According to the MVC, DAO should be done from a model is what I infer. But I just wanted to try it from an action for test purposes

Please help me with this!
Thanks in advance

[ March 20, 2008: Message edited by: Vidya Moorthy ]

[ March 20, 2008: Message edited by: Vidya Moorthy ]
[ March 20, 2008: Message edited by: Vidya Moorthy ]
Joe Ess

Joined: Oct 29, 2001
Posts: 8843

You don't need this explicit cast because getConnection is declared to return Connection implementation:

I'm at a loss as to why that cast would throw a ClassCastException, however.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Vidya Moorthy
Ranch Hand

Joined: Nov 13, 2003
Posts: 57
That was it. I changed the line to
java.sql.Connection conn = ds.getConnection();
based on your suggestion and it worked. Wow, I have been stuck at this for almost 2 days now. Thanks a ton!
It is sorta covered in the JavaRanch Style Guide.
subject: Error in setting up a datasource in Tomcat 6.0's JNDI
Similar Threads
Problem in implementing connection pooling in tomcat6.0
java.sql.SQLException: No suitable driver found
JDBC fails with Tomcat
register data source in JNDI