Web application cannot establish connection to database - java

My application, when running on Tomcat, is not able to connect to my database when deployed as a .war in the webapps folder, but it can connect when I am running it through Eclipse directly. The code is exactly the same in both cases. In addition, other apps in the webapps folder, which originally could connect to the database, can no longer do so. The jdbc code is correct as I have tested it with offline applications or when running it through eclipse, but not when I access it on, say, Chrome, using localhost. What has happened to my tomcat server?
Note: the JDBC driver is the MS Access one.

The code is exactly the same in both cases.
No it's not; if it were exactly the same you'd be connecting properly.
but it can connect when I am running it through Eclipse directly
Does this mean that you have a main method that drives the code that connects properly? The way you do it is usually different from a web app - you know that, right?
If you could post an exception or log message it would help a great deal.
I'm guessing it could be any one of the following; guessing is necessary because you haven't provided enough information for a solid answer:
The JDBC driver JAR is not available when you run under one configuration; you'll see a ClassNotFoundException in that case.
You haven't configured a JNDI data source properly in Tomcat.
You didn't put the JDBC driver JAR in the Tomcat /lib directory.
Could be other possibilities.
UPDATE:
Since you're using Access, and providing no other information, I'm guessing that you're using a relative file path to get to the Access .mdb file. Your command line app that runs successfully in Eclipse works because you give a relative file path that is correct relative to the project root. When you deploy to Tomcat, that relative path is no longer correct, so your application can't find the database anymore.
Sounds like you aren't printing the stack trace for errors, so you're losing out on information that might help you figure this out.
Microsoft Access is not a multi-user database. A web based application running on Tomcat is certainly multi-threaded. You should not be using Access for a web application. I'd consider switching to another database.

First, you need to verify the path to your database file. In fact, I believe it has to be an absolute path for tomcat to work correctly. The simplest thing to do is to put your database file in C:\data or similar and then hard code the path in your code. Of course, the file will be outside of your war and thus not portable (i.e., deployable to a remote server).
Second, you do need to make sure that the JDBC driver is available. If you use Class.forName in your code to load the driver, you will only need to make sure that the jar containing the driver is in the tomcat classpath (which would include the lib directory of your webapp). If you use the -Djdbc.drivers JVM flag approach, you will need to add it to the tomcat startup scripts.

Related

War file on Win Server Tomcat 8.0

I export my maven java project in Eclipse to War file.
On the server, I open the manager of tomact and add the war application, but when I run the application it seems the application isn't running (or some problem with the resources).
From the other hand, when I start the tomcat by the startup.bat in the tomcat folder and add the War to the webapps folder, it is working. But if I logoff the server , I cannot connect the application remotly. The server is shut down. Why?
What can be the problems?
Thanks!
Me
Why is your application not running? Well - if there's a specific reason for it (and the manager application works) you will see it in the logfile - e.g. tomcat's log/catalina.out. Check it or give us a snippet of the error messages in there if you don't understand them.
I like to recommend not to use the manager application, rather deploy through the OS or any other maintenance tool (e.g. scripted). This typically results in a better maintainable system. As it already works well for you when you just add your WAR file to the webapps directory, everything seems well. Introducing a manager application in production just provides another means to attack your site - and I frequently see those applications poorly maintained (and the passwords poorly chosen & protected)
When you start tomcat with startup.bat, it will run the server process in a console window. This console process will be shut down when you log out. In order to have tomcat running even when you're not logged in (and in order for it to start automatically), you'll have to install it as a service. There's plenty of information on the internet on how to do this - should be extremely easy to find.

Tomcat integrated in Eclipse DB connection

I'm finding a rather unusual problem. I have a web app that connects to my DB using the jdbc driver and creates the DB tables when the application is launched (hibernate hbm2ddl.auto=create). When I create a war and deploy it to tomcat from outside eclipse (simply copying the war to the webapps folder in tomcat) and launch the application, it creates the tables correctly. However, I integrated Tomcat with Eclipse(catalina_home set to the same tomcat installation as before, thus same config files).But this time, when I deploy the app from within eclipse, the tables are not created. I have tried a couple of things but nothing seems to work.
If you need more specific information please dont hessitate to ask.
Thanks in advance.
I had the opposite problem, where things worked in Eclipse but not Tomcat by itself. You may need to be very explicit about the connection method to the database, that is what helped me. See my question and answer at Tomcat JDBC connection works in Eclipse but not stand-alone.

Upload opencms project on real server

I'm working on an open source java cms that name is opencms .
it is war file that you deploy it with tomcat and when it's up you go to localhost:8080/opencms and start to configure it as you want.
my problem is that when I've done my all jobs and making a cms site with it, what I must do to upload it on a real server ?
do I have to make war file from it then upload it or just i upload it's deployed folder to server tomcat ?
and what i must do with it's database ? it's enough to export a .sql file from my local database and execute it on server database or i must do some other things ?
please help me
thanks
You need an opencms installation on your "real" server that fits the one you created your page with.
It should be possible to just copy the content of opencms webapp folder inside tomcat to the tomcat on the target machine and also transfer copies of the associated databases to the "real" server.
Personally I prefer performing a standard installation of opencms on the target machine and configuring it (remove example content, change passwords, etc.).
Then I would use the export functionality of opencms to export the content on the development machine and import it into the target opencms instance.
The complexity depends on how much changes you have applied to the default installation - if you have developed your own modules, then it might be a bit more difficult.

jar file location - ear file or server lib folder?

When deploying an application to JBoss server, for a JAR like ojdbc14.jar (Oracle JDBC), is it better to include this jar in the ear file of the deployment, or should it be deployed to JBoss's server/default/lib folder?
The argument I've been given is that the application needs to support global transactions, hence the datasource must be defined at the application server level (e.g. copied to server/default/lib folder) so it can talk to other app servers, and for that to happen you need the drivers at the app server level.
I'd like to hear what is the correct approach here. Thanks.
I believe this is the correct approach.
You will typically define your datasource outside of your application (outside of your EAR/WAR) using the appropriate configuration files depending upon the application server you are using. Whereas on JBoss, you usually add your configuration to XML files, with WebLogic, you can create a datasource directly using the Admin console.
Given that this datasource will be created on server startup, the necessary libraries (in your case, ojdbc14.jar) must be available on the classpath of the application server.
I have also come across licensing issues where the use of a 3rd party library prevents you from bundling it within your application.
Also, defining your datasource outside of your application means you do not have to perform a rebuild when the datasource configuration needs to change.
So many time I have done that...
I will always put the jar inside the server/default/lib folder if it has something to have about JDBC.
Because your database driver is loaded one time as soon as the server is started, and not each time you deploy/undeploy the webapplication your are working on.
Your datasources will be availiable for any others sub-projects too, and it will be a great advantage when you have a specific project for integration testing purpose.

logging on windows client machine with Java web start

I am working on a java web start application which needs to log some statements.
The clients running this app can be running on windows,mac or linux.
I am planning to use log4j for my logging utilities and each log file to be named as , meaning each run of the app should create a new log file.
As there is nothing actually getting installed, i am not sure which directory should i store these files to, as these need to be persisted for future debugging. Also, then the question arises as to find how to find out the directory structure of the client machine.
Could someone please suggest on these?
First of all you need to get access to the local machine. You can sign your application and require full access, or you can use the web start facilities to get access to various services including file access. See the web start documentation for details.
Then you need to determine where to put your log files.
A good place to start is the user.home system property which usually points to the home direcotry of the active user, so if you can use a convention like ".yourapp/debug.log" you can put it there.
More than that require deeper knowledge of the target machine.

Categories