tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Tomcat 5.5 org.apache.naming.ResourceRef cannot be cast to javax.sql.DataSource
Date Mon, 25 Jan 2010 18:07:57 GMT
-----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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message