Starting from the bottom up of your suggestions:
1) I tried removing the try/catch. No can do, Java compiler forces me to catch the exceptions. 2) However, Java does provide a getStackTrace. There were 33 elements, but since pages that don't try to connect ARE working I think only the below apply: 2010-01-25 19:37:09:Stack(5)javax.servlet.http.HttpServlet.service(HttpServlet.java:802) 2010-01-25 19:37:09:Stack(4)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98) 2010-01-25 19:37:09:Stack(3)org.apache.jsp.index_jsp._jspService(index_jsp.java:89)
2010-01-25 19:37:09:Stack(2)com.jsrsys.web.JsrSQL.(JsrSQL.java:42)
2010-01-25 19:37:09:Stack(1)com.jsrsys.web.JsrSQL.setDataBaseName(JsrSQL.java:86)
2010-01-25 19:37:09:Stack(0)com.jsrsys.web.JsrSQL.connect(JsrSQL.java:503)
2010-01-25 19:37:09:*WARNING*Connection to jdbc/MySql:: connect Other exception: org.apache.naming.ResourceRef cannot be cast to javax.sql.DataSource
2010-01-25 19:37:09:JsrSQL: Context cast successfull
2010-01-25 19:37:09:Driver=com.mysql.jdbc.Driver
Not much help here, but it is very good to be able to print the trace when debugging so glad I was able to learn how.

I am now going to remove the Class.forName code and see if that changes any thing. ----- Original Message ----- From: "Christopher Schultz" <ch...@christopherschultz.net>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Monday, January 25, 2010 12:07 PM
Subject: Re: Tomcat 5.5 org.apache.naming.ResourceRef cannot be cast to javax.sql.DataSource


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steve,

On 1/25/2010 11:16 AM, Steve Ryder wrote:
When I remove the factory line I now get: Cannot create resource instance
2010-01-25 15:56:06:*WARNING*Connection to jdbc/MySql:: connect Other
exception: Cannot create resource instance

Ok.

The libraries are all in common\lib.  The ones I have added since the
5.5 install are:
jsrweb (my Java code), mysql-connector-java-5.0.8-bin.jar,
naming-common.jar, naming-java.jar, mail.jar, and activation.jar.

I picked 5.5 because it is not getting active develpment, yet supports
Java 1.5, and has all the features I need hoping to minimize
incompatabilities.

Fair enough.

My Java "connect" code reads:   (indentation was lost when I copied)
private void connect(String dbName)
{
lastException = "OK";
try
{
// Class.forName("org.gjt.mm.mysql.Driver");  <---I changed this last
night hoping to fix problem.

sysout.display("Driver=com.mysql.jdbc.Driver");

Class.forName("com.mysql.jdbc.Driver");

}
catch (Exception E) {lastException=" Unable to load driver";
E.printStackTrace();

You don't need any of the above if you are using a connection pool: the
pool will register the driver for you.

sysout.display(lastException);}

What does this print, including the stack trace from the catch block?

try
{
sql = "Connection to jdbc/MySql:";
Context myContext = new InitialContext();
Context envContext = (Context)myContext.lookup("java:comp/env");

Ok.

javax.sql.DataSource ds =
(javax.sql.DataSource)envContext.lookup("jdbc/MySql");

conn = ds.getConnection();

stmt = conn.createStatement();

Ok.

sql = "USE "+ dbName; // attempt to mymic jdbcConnect set for dbName!

stmt.execute(sql);

This is not necessary as you have already selected your db via the
connection URL.

catch (SQLException E)

{ lastException="*WARNING*"+sql+ ": connect SQL exception: "

+ E.getMessage();

sysout.display(lastException);

//System.exit(4);

}
catch(Exception eOther)

{ lastException="*WARNING*"+sql+ ": connect Other exception: "

+ eOther.getMessage();

sysout.display(lastException);

//System.exit(4);

}

So, the above is the source of the error message which has all your
extra stuff like "*WARNING*Connection to jdbc/MySql:: connect SQL
exception:" plus the message from the real exception. I don't recognize
"Cannot create resource instance" so it would be helpful to show the
full stack trace and the real exception that caused the message to print.

I would recommend not catching "Exception" unless there is a good reason
to do so: allow the exception to propagate up the stack to a place where
it can be appropriately caught and dealt with, or at least logged
properly. What happens when this code fails and the "conn" member isn't
set properly? I'm guessing you either get NullPointerExceptions later in
your code, or you have to have a bunch of null checking in your query
code which is kind of ugly.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEUEARECAAYFAktd3f0ACgkQ9CaO5/Lv0PD6CgCXWjgZIBoBQrgS7ciU7Y4L8TGt
TQCbBC+bDwqmlEgAeiCXp/PdeweDFWM=
=aaxM
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to