| Author |
classpath issue with c3p0
|
Henrik Axelsson
Greenhorn
Joined: Sep 15, 2008
Posts: 7
|
|
Hi all, I am trying to solve a frustrating problem with a servlet running under tomcat 6. Running under Windows there was no problems, but now after trying to move it over to OS X there are odd problems occurring. We are using c3p0 to do connection pooling, however c3p0 is having trouble creating the database connections, it gives the following error: java.sql.SQLException: No suitable driver After looking at the c3p0 code, it attempts to get a reference to the driver by calling: But this is throwing the exception. If I place the following code into my webapp (which is copied from the c3p0 xml file im using): It works without a problem. Does this mean that is is not finding the com.mysql.jdbc.Driver class or is it caused by something else? I have tried copying the mysql connector jar file into tomcat_home/lib but that didn't help either. The only other thing I can think of is the jdbcUrl being used by c3p0 is getting mangled some how, but checking that is going to be bit of a pain.
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
Hi Henrik and welcome to Javaranch!
Originally posted by Henrik Axelsson: Hi all, I am trying to solve a frustrating problem with a servlet running under tomcat 6. Running under Windows there was no problems, but now after trying to move it over to OS X there are odd problems occurring. We are using c3p0 to do connection pooling, however c3p0 is having trouble creating the database connections, it gives the following error: java.sql.SQLException: No suitable driver After looking at the c3p0 code, it attempts to get a reference to the driver by calling: But this is throwing the exception. If I place the following code into my webapp (which is copied from the c3p0 xml file im using): It works without a problem. Does this mean that is is not finding the com.mysql.jdbc.Driver class or is it caused by something else? I have tried copying the mysql connector jar file into tomcat_home/lib but that didn't help either. The only other thing I can think of is the jdbcUrl being used by c3p0 is getting mangled some how, but checking that is going to be bit of a pain.
Indeed you are going to have to check what the value of the jdbcUrl variable is (you obviously want it to be "jdbc:mysql://localhost/testdb"). I'm not familiar with c3p0, but is it open source? Can you modify the code to put a logger statement or two in there to get that value out?
|
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
|
 |
Henrik Axelsson
Greenhorn
Joined: Sep 15, 2008
Posts: 7
|
|
thanks for the reply, yes it is open source, the source tree is bit of a mess though! I tried loading into netbeans but it wasn't too happy with it. I'll have to spend a bit more time on it. Considering the same file is working fine under windows, I'm fairly certain that it is being caused by classpath/classloader issues. I was having another problem loading the c3p0 configuration files, it was located in: project_dir/config and I was accessing it using . This worked fine on windows, and after adding the directory to the tomcat classpath I could access it from the main servlet class using but if i tried accessing it from another class, using it wouldn't find the file. In the end I move the config files into project_dir/deployment/WEB-INF/classes/config where it was found by both classes. Now I'm not tomcat expert, but it seems to me like there are some security or classpath settings that I could be missing that is likely causing both issues. Who knows though, it could be the urls getting messed up. Argh so frustrating!
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
I'm pretty sure it will be the class loaders yes. The c3p0.cfg file problem you had is definitely an indication of that (the Servlet is using a different class loader to your Otherclass) Another question, where is the value of jdbcUrl ultimately coming from? c3p0 must be reading it in from a configuration file somewhere...
|
 |
Henrik Axelsson
Greenhorn
Joined: Sep 15, 2008
Posts: 7
|
|
|
The value of the jdbcUrl comes from the c3p0-config.xml file. As this is the same file that we are using under windows (where it works fine) I don't think that this would be the issue. Just to make sure, I copy/pasted the connection strings from the config file into SquirrelSQL and it didn't have a problem with them.
|
 |
Henrik Axelsson
Greenhorn
Joined: Sep 15, 2008
Posts: 7
|
|
Ok solved the problem! It ended up being this: c3p0 was trying to find the configuration file using the system class loader, which doesn't have access to the /lib dir of the web app. Because it wasn't reading the config file, when I was trying to get connections, there were no pool configurations set up. The reason I got confused it that I also parse the config file to get the pool names and actually create the pools. (c3p0 just creates the pool configs, not the actual pools). So, if I added /project_dir/config to the class path of tomcat in the catalina.bat file it all works fine. I'll have to check to make 100% sure but, to me this seems like an error in c3p0, it really should be using Class.class.getClassLoader().getResource(); instead of ClassLoader.getSystemResource(); I've seen that other people have had this problem with different libraries when trying to run them in web containers. Hopefully this will save someone else wasting 2 days of their time hunting this down!
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
|
Great to hear you solved the problem and thanks for posting the solution for others to share!
|
 |
 |
|
|
subject: classpath issue with c3p0
|
|
|