This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Trouble with connection to MySQL in Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Trouble with connection to MySQL in Tomcat" Watch "Trouble with connection to MySQL in Tomcat" New topic
Author

Trouble with connection to MySQL in Tomcat

Andre Solberg
Greenhorn

Joined: Sep 16, 2010
Posts: 13
Hi,

I have some trouble with getting my application to work on Tomcat.
It deploys fine, but when I try to to run it, I get this:



This is the DbCon.java



Main menu items are loaded from the database, so the index.jsp is calling the DbCon:



This works perfectly when running locally through the preview server in Eclipse, but not when running on Tomcat. It has to be something wrong with the connection URL right? But shouldn't I be getting some SQL Exceptions in the Tomcat log? It's only the nullpointer, and I don't understand why. I have the MySQL connector jar file in WEB-INF/lib in my project, and also tried to add it to the server, but it changes nothing.

Any advice would be appreciated!

Apache Tomcat/6.0.18
JVM 1.6.0_18-b18
MySQL client version: 5.0.75

Thanks!
Andre
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

Well, the first bit of advice is don't put logic on JSPs! That's a beginner tactic that doesn't hold up on large-scale systems.

Since your stack trace indicates failure on line 54, and there are only about 52 lines in your class, I have to conclude that either your class file isn't up to date or you chopped out some stuff for the purposes of illustration.

You are not checking to see if the DriverManager.getConnection() returns null even though no Exception has been thrown.


Customer surveys are for companies who didn't pay proper attention to begin with.
Andre Solberg
Greenhorn

Joined: Sep 16, 2010
Posts: 13
I know, I am moving the logic to servlets, but I need to get this working.

The class is up to date, I just didn't include the imports etc.
So the failure happens at the ResultSet res = stmt.executeQuery(query); on line 47.

I guess my question is why I only get nullpointer in the log. When I test locally with an invalid URL, I get the exact same stacktrace in the browser, but then I also get the SQL exceptions in the Eclipse console. I need all information in the log, also in the future when other errors happen...

Thanks!
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

You realize that a Connection object must have been acquired just to get to that statement, don't you? It's the "stmt" that's null, so the createStatement failed.

System.out/err does not "write to the Tomcat log". It writes to the console data streams of the JVM. Depending on the OS and Tomcat configuration, that output could come out anywhere or nowhere. This is especially a problem on Windows systems, and it's one of the reasons we recommend using a true logger instead of the system datastreams.

Andre Solberg
Greenhorn

Joined: Sep 16, 2010
Posts: 13
I figured, and I wanted to know the exact cause of why the Connection object was null. I just found what I was looking for, in deamon.log which was in another folder than the regular log.
It said

Sep 19 16:19:27 www4 jsvc.exec[3333]: Failed to connect to db.
Sep 19 16:19:27 www4 jsvc.exec[3333]: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Sep 19 16:29:27 www4 jsvc.exec[3333]: Caused by: java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission [0:0:0:0:0:0:0:1]:3306 connect,resolve)

So I guess it's a host issue.

edit: I'm not the one who configured Tomcat, but he's busy so thats why I posted here Guess I'll try to get a hold of him
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
Andre Solberg wrote:I figured, and I wanted to know the exact cause of why the Connection object was null. I just found what I was looking for, in deamon.log which was in another folder than the regular log.
It said

Sep 19 16:19:27 www4 jsvc.exec[3333]: Failed to connect to db.
Sep 19 16:19:27 www4 jsvc.exec[3333]: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Sep 19 16:29:27 www4 jsvc.exec[3333]: Caused by: java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission [0:0:0:0:0:0:0:1]:3306 connect,resolve)

So I guess it's a host issue.



It still not clear why did it work in Eclipse. I got an impression that you are trying to connect to the same database from the same machine
Andre Solberg wrote:This works perfectly when running locally through the preview server in Eclipse, but not when running on Tomcat.
Andre Solberg
Greenhorn

Joined: Sep 16, 2010
Posts: 13
No, I guess I was a little unclear on that. In eclipse I use a local test database. There is another database when deploying to Tomcat.

But it's all working good now. After searching around I found this:

Add the following lines to /etc/tomcat6/policy.d/webapps.policy file:
permission java.net.SocketPermission "127.0.0.1:3306", "connect,resolve";
permission java.util.PropertyPermission "file.encoding", "read";

Also comment out in /etc/mysql/my.cnf
#bind-address = 127.0.0.1

Don't know why, but it's working so I'm pleased...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

Do you have security switched on?

I noticed just the other day that when you run Tomcat with the "security" option that the sandbox rules it uses lock out some things we tend to expect to have access to, and I believe that the examples given were specifically related to JDBC.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with connection to MySQL in Tomcat
 
Similar Threads
sslext struts tiles exception.
Problem in printing a pdf file using pdfrenderer
Marshalling exception
EJB 2 + Sql Server 2000
Not able to run the Dukes bank application in J2EE 1.4 tutorial