Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName()

 
Paul O'Neill
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a DAO class getting a connection from another DAO class and it ultimately fails. The line it fails on is where it looks up Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver). I've only figured this out by putting System.outs everywhere. There's absolutely nothing that tells me why it fails at that line. It works when I run it on my dekstop using WSAD and fails out on a server, which indicates it's a classpath issue - I think. The mssqlserver.jar file is part of the classpath.

Thanks for any help out there.
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Paul,

do you have a try/catch around the Class.forName(...) line?
It might throw a ClassNotFoundException.

Herman
 
Paul O'Neill
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried that and it returns nothing. It's like it doesn't error but it never gets to the line after Class.forName either. I'm calling this method from another class.

Here's the code :

if (mConnection == null) {
try {
Class.forName(jdbcDriverName);
log.info("Executing getConnection() to get a new connection to " + jdbcURL);
mConnection = DriverManager.getConnection(jdbcURL,jdbcUserName,jdbcPassword);
mConnection.setAutoCommit(false);
log.info("Got Connection()");
}
catch (Exception e) {
e.printStackTrace();
log.warn("Error getting connection. Attempting retry.",e);
retryConnection();
}
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again,

A couple of questions:

-how do you know that your code fails at all, if it doesn't log any errors?
-is mConnection equal to null in this case?
-does the logging itself work: can you show me the log messages that you do get? (just copy/paste, don't type).
-maybe you should try to catch Throwable, so that you will also catch instances of java.lang.Error
-what do you do in retryConnection()?

Herman
 
Paul O'Neill
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Herman. I owe you!!

I never thought of using Throwable as more of a catch-all than Exception. This showed up I was missing a couple of classes because I didn't have the msbase.jar or msutil.jar in my classpath. I had been try-catching with Exception and it just didn't tell me anything.

Many thanks!!

Paul
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Paul,

you don't owe me anything, but thanks anyway.

Herman
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic