Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vidya Moorthy
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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
Vidya

[ 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
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidya Moorthy
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe
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!
Vidya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic