views:

1037

answers:

12

Hi,

I am facing this peculiar problem. My webapp, works fine on my localhost. Its a JSP/Struts-Tomcat-MySQL app. However, when I host it on hostjava.net (shared tomcat), it is unable to connect to the database.

After some debugging, I have identified the problem, to be with JNDI lookup for datasource. If you want, you can take a look at the log at http://rohitesh.hostjava.net/MapsDummyLog.htm

Some details on the context information location : /META-INF/context.xml contains :

<Context path="" docBase="" debug="5" reloadable="true" crossContext="true" override="true">
   <Resource name="jdbc/ConnectionPooling" auth="Container" type="javax.sql.DataSource"
 maxActive="10" maxIdle="5" username="[username]" password="[password]" driverClassName="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost/[db name]?autoReconnect=true" />
</Context>

Can anyone help me find out, where am going wrong, please?

Cheers, Rohitesh.

A: 

Hi There,

"java.lang.NullPointerException at com.DAO.UserDAO.userLogin(UserDAO.java:109) at com.Actions.UserAction.execute(UserAction.java:66) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) "

This tells me that something is NOT being initialized properly. on what object are you invoking in line 109 of UserDAO.java ?

"10/25/08 (2:08 AM) ajp-127.0.0.1-8015-1 ERROR UserDAO.java:104 Cannot create JDBC driver of class '' for connect URL 'null'
10/25/08 (2:08 AM) ajp-127.0.0.1-8015-1 ERROR UserDAO.java:105 org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' "

Did you check the URL for the DB. it appears that the URL is invalid or NULL. Can you give a manual value for the JDBC connection URL ?

anjanb
A: 

Hi Anjanb,

The context.xml contains :

<Context path="" docBase="" debug="5" reloadable="true" crossContext="true" override="true">

   <Resource name="jdbc/ConnectionPooling" auth="Container" type="javax.sql.DataSource"
 maxActive="10" maxIdle="5" username="[username]" password="[password]" driverClassName="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost/[db name]?autoReconnect=true" />
</Context>

As you can see, the url is not NULL, or invalid. Othrwise, it wouldn't have worked on the local. I think its more to do with the location of the context.xml file.

Any thoughts on that?

Cheers, R

RD
+3  A: 

Shouldn't this:

url="jdbc:mysql://localhost/[db name]?autoReconnect=true"

really point at the name of the server on which the database is hosted? On th remote host the database server may not be the same machine as the tomcat instance. I would think that you have to say

url="jdbc:mysql://[Server_Name]/[db name]?autoReconnect=true"

Vincent Ramdhanie
A: 

Dear Vincent,

The peculiar thing is, that another part of the same application, accesses the database, with a hardcoded url (using DriverManager, and NOT datasource from the JNDI lookup), and that works just fine (check out the first two lines in the log, and the next couple of lines with cuisine names, which are read from the database). So, it is certain, that the database is also on the same server.

Cheers, R

RD
A: 
  • is the driver available? Try Class.forName("com.mysql.jdbc.Driver"); - and it can't be located in your webapps WEB-INF/lib because you are asking for the container (tomcat) to instantiate it for you: It needs to be in the servers classpath
  • Are you running Tomcat 5.5.x or 6.0.x? On 5.0.x the context.xml syntax, especially resource definition, has been different: Way more xml tags instead of the nicer and simpler attributes since 5.5.
Olaf
+2  A: 

Here goes my guess:

You said it's a shared hosting. Then shouldn't

<Context path=""

contain the context path of your particular application?

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing. All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are defining the default web application for this Host, which will process all requests not assigned to other Contexts.

So you context is invalid and, I presume, simply invisible (that's why null driver and null url).

It would work on your local as "" is a shared context for all apps deployed, including yours.

Vladimir Dyuzhev
A: 

Olaf :

  • Can't use that method, as I want to use connection pooling. So, I need to get the datasource via a JNDI Lookup, and get my db connection from there.

  • The hosting service is running tomcat 5.5.26. So, I guess the context.xml dformat is correct

Vladimir :

This is the new context.xml file, after I worked on your suggestion,

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/rohiteshd.hostjava.net" docBase="/var/www/vhosts/rohiteshd.hostjava.net/httpdocs" debug="5" reloadable="true" crossContext="true" override="true">

   <!-- JDBC Connection Pool -->
   <Resource name="jdbc/ConnectionPooling" auth="Container" type="javax.sql.DataSource"
 maxActive="10" maxIdle="5" username="[username]" password="[password]" driverClassName="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost/[db name]?autoReconnect=true" />

</Context>

And its still failing! :(

Regards, Rohitesh.

RD
+1  A: 

How about this restriction:

http://wiki.hostjava.net/index.php/HostJava.net_FAQ With Shared Tomcat access to server.xml file is restricted. Only support staff can add Realm (for example JDBC Realm)to server.xml file.

Looks like you need to ask support to add your datasource.

Vladimir Dyuzhev
A: 

Dear Vladimir,

But this is Tomcat 5.5. Doesn't it mean, that I shouldn't have to enter the realm data in server.xml? Putting the same in META-INF/context.xml should also work, shouldn't it?

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

Regards, Rohitesh.

RD
I'm not an expert in Tomcat configuration. The point is: on shared hosting it may be prohibited to create your own data sources. For security reasons or resources reasons. Ask support to add one for you.
Vladimir Dyuzhev
+1  A: 

Rohitesh,

Per Vladimir's answer and comments, you may want to consider requesting the server context (server.xml, or the more globally scoped context.xml) be updated.

If nothing else, this is, in my opinion, a best practice. While Tomcat does allow you to define the context (including JNDI resources) from within the web application itself, the only place you should use this feature is in a developer's local test server. It makes your web application more portable, as it allows to change the configuration of your external resources (in this case, the database, but it could be a mail server, or content server, rules engine etc) independently of your application.

I hope this helps.

Jack Leow
A: 

Another note: Do you have access to any other log files besides the one you posted? If you have access to the server logs/catalina.out file, it may show you the exact problem Tomcat is having setting up the data source (this would be right when you start up Tomcat).

Jack Leow
A: 

Sorry everyone. It was a problem of miscommunication between me and the Support team of the hosting facility. There was a clash of configs. They had set some old settings in server.xml. As that takes precedence over any other context information, I was facing those issues. Now it is taken care of.

But I still feel, that to allow people on a shared configuration, to have their context information in META-INF/context.xml file, is a better option. That way, they have more control over it.

But I Thank everyone, who took the time out, to respond. I learnt a lot from you. Thank you again.

Cheers, R

RD