This question already has answers here:
Where to put 3rd party libs when using Maven with Tomcat?
(2 answers)
Closed 3 years ago.
I am trying to use postgresql driver with tomcat.
but when i run tomcat I am getting FileNotFound exception(Class.forName("org.postgresql.Driver")).
Is tomEE aware of maven dependency.
how can I overcome it
No. Maven not involved after Tomcat/TomEE deployment
No, TomEE does not know about Maven or your POM.
As commented by Andreas, your Maven-driven web-app project will produce a WAR file or EAR file. That file contains any dependencies you may have configured in your POM.
For deployment, you will be moving that WAR or EAR file to the TomEE server. At that point there is no more Maven involvement.
These comments above apply to your eventual deployment for production. While in development, you may be using an IDE such as IntelliJ/NetBeans/Eclipse that can call upon an external web container such as Tomcat or TomEE to run and debug your web app. Maven settings may be involved in that special case, as part of hooking up your IDE to the external web container. Even in this special case, Tomcat/TomEE is not aware of Maven having possibly participated in its launching or configuration.
JDBC drivers are special
Furthermore, deploying a JDBC driver to Tomcat, TomEE, or other Jakarta Servlet container is a complicated matter because of classloader issues and the JDBC driver registration process. Generally, you should not be bundling a JDBC driver within your WAR/EAR.
Search Stack Overflow to learn more. Remember that TomEE is built on Apache Tomcat, so most anything you read about Tomcat applies.
See:
Where to put 3rd party libs when using Maven with Tomcat?
To prevent a memory leak, the JDBC Driver has been forcibly unregistered
How should I connect to JDBC database / datasource in a servlet based application?
By the way, in modern Java with its JDBC driver registration feature (DriverManager), you no longer need to call Class.forName. That call is now legacy.
DataSource
Tip: Learn to use a DataSource implementation provided by your driver. Regarding Postgres, if using the JDBC driver from jdbc.postgresql.org, see this chapter.
PGSimpleDataSource pgDataSource = new PGSimpleDataSource();
pgDataSource.setDataSourceName("Acme Corp invoicing database");
pgDataSource.setServerName("localhost");
pgDataSource.setDatabaseName("test");
pgDataSource.setUser("testuser");
pgDataSource.setPassword("testpassword");
DataSource dataSource = pgDataSource ; // Perhaps save as an "attribute" on your web app's "context".
Ask the data source for a Connection object when needing to talk to the database. Usually best to use try-with-resources syntax.
try
(
Connection conn = dataSource.getConnection() ;
)
{
… do your database work
}
Later you can learn to configure this DataSource info externally, outside your code base. That configuration is done through JNDI and a naming server such as the LDAP-style server built into Tomcat.
Related
I built a simple Java web application. It provides a series of RESTful APIs for the user to carry out certain operations on a Java DB through a web interface. I used NetBeans environment during the development, and Glassfish for testing.
Now that I finished it, I would like to be able to deploy it on another machine using binaries (although as for now I use the same machine until I learn how to do it).
I installed Tomcat 7, and moved the .war file into Tomcat's webapp folder. The application deploys. Thereafter I try to read some data from the databse using a button I created just for this, but get the following error
I am not sure what went wrong, but I have two theories.
1) The web application cannot connect to the database. Yet when I attempted to run the application again, after starting JavaDB from NetBeans, there was no difference.
2) Somehow, the application cannot reach the Node service. I assumed that there will be no need to change the API links while moving the app, but perhaps I was wrong.
Or maybe there is some other issue I did not consider? I will be grateful for any advice about how to properly deploy such an application.
EDIT: The issue was solved by using TomEE.
The error is come from your application server of choice.
TomCat is only a servlet container (means it only support Servlet/JSP).
Any other feature (JAX-RS, CDI etc) require a Java EE certified server e.g. GlassFish, WildFly,Payara, WebLogic, OpenLiberty or TomEE.
TomEE could be your best bet if you want to use TomCat in your production or test environment, it is basically TomCat + Java EE other feature.
EDIT:
TomEE don't have a GUI for JNDI datasource configuration like GlassFish, you need to edit conf/tomee.xml
<Resource id="myDataSource" type="javax.sql.DataSource">
jdbcDriver = org.apache.derby.jdbc.ClientDriver
jdbcUrl = jdbc:derby://localhost:1527/dbname
userName = app
password = app
</Resource>
And in your java code:
#Path("resources")
#Stateless
public class MyResources{
#Resource(name="myDataSource")
DataSource dataSource;
#GET
public Response SomeMethod(){
//Do stuff here
}
}
You can check here for more detail configuration on data source.
I'm building a web app using spring boot with the goal of having an executable jar that customers can just run without the hassle of deploying to a tomcat web server.
This web app uses a jdbc database connection and customers can use a database of their choosing by simply supplying a jdbc driver jar.
However executable jars do not allow to use of -cp or -classpath, so how can customers best supply their jdbc database driver jar to my spring boot web app? Has anybody experienced a similar issue and found a work around for this without packaging every possible jdbc driver into the web app jar?
You could use Spring Boot's PropertiesLauncher and its loader.path property to point to an external directory into which JDBC driver jars could be added by your users. You can learn more about PropertiesLauncher and its loader.path property in Spring Boot's reference documentation.
I am trying to deploy my webapp on Tomcat 8 that uses Mybatis 3.2.7 and c3p0 for connection pooling to connect to an SQLServer database. I have the sqljdbc4.jar in my classpath. I query the database during my webapp startup to get some values.
The application works in Tomcat 7, however on Tomcat 8, I cannot connect to the database. I debugged a lot using eclipse and the root cause is in the file BasicResourcePool.class file in c3p0 where it is waiting for resource to become available but then throws an java.lang.InterruptedException.
Due to this, Mybatis is throwing a java.SQL.SQLException and thus my webapp does not start as it cannot connect to the database.
Has someone else upgraded to Tomcat 8 and has successfully used Mybatis-c3p0? If yes am I missing something over here?
Solved this. It was JDBC driver issue. Mybatis isn't very good in showing underlying exceptions it seems.
Found this on tomcat 8's documentation:
Thus, the web applications that have database drivers in their
WEB-INF/lib directory cannot rely on the service provider mechanism
and should register the drivers explicitly.
So, I added a Class.forName() with the appropriate driverClass during app startup and this solved my issue.
I have a Play Framework 2.2.2 application that I am deploying as a .war file and running under Tomcat 7. My application runs for days without problems on my local dev machine (through Play's built in server, not Tomcat), but once I deploy it under Tomcat, after several hours the Tomcat server will lock up, taking down all the other applications running on it as well.
I think the problem is that the BoneCP connection pool in Play, and the built-in connection pool of Tomcat are conflicting. There isn't much or any useful information in the Tomcat logs, so I'm kind of left guessing here.
I'd like to disable the BoneCP connection pooling within my Play application, but cannot find any information on how to do so.
Any advice appreciated!
There are several possible solutions for this, which might be more or less preferrable for your deployment environment.
Play gives you an "out-of-the-box" database connection, which you don't need to use. Drop the Play JDBC component from your build file (remove jdbc from your libraryDependencies) and setup your JDBC connections manually by yourself. For example, you can make a singleton TomcatConnectionPool that has a function getConnection() that gives you the JDBC connection you need for use in your Play actions.
Write your own plugin specifically extending Play's DBPlugin interface so that it's a database plugin. Implement it like Play's BoneCPPlugin but make it use the Tomcat connection pool instead of BoneCP.
Use someone else's already made custom Play Database Plugin, like this one that uses c3p0. I have some anecdotal evidence that c3p0 works well with Tomcat, but your mileage my vary.
I have a Spring Roo app that is deploying to Tomcat with no issues. I'm trying to deploy it to JBoss 6, but I'm finding it impossible to do so.
I've exhausted all resources from Google and I simply receive errors everywhere. Unfortunately, they do not seem specific enough to start narrowing them down to list here.
What can information could I provide to help resolve this situation?
Essentially, I need to know what I need to change from a standard Spring Roo app, using Hibernate and Mysql to work with JBoss 6.
EDIT:
This is the error that I am getting
[ClassLoaderManager] Unexpected error during load of:org.apache.commons.collections.DoubleOrderedMap$1$1: java.lang.IllegalAccessError: class org.apache.commons.collections.DoubleOrderedMap$1$1 cannot access its superclass org.apache.commons.collections.DoubleOrderedMap$DoubleOrderedMapIterator
Impossible to tell, since you posted no errors.
I'm guessing that it's a problem with the configuration difference between JBOSS and Tomcat.
You set up JDBC data source connection pools differently. Tomcat has the context.xml in the server /conf folder. JBOSS has other XML config files in its server/default/deploy folder. Did you create those correctly?
I assume that you're using JNDI names for injected data sources.
Your JDBC driver JAR for MySQL goes in the Tomcat /lib folder and the JBOSS server/default/deploy/lib folder, not the wAR WEB-INF/lib.
But you should be able to take a WAR with all the Spring Roo stuff, put it into an EAR with jboss-web.xml configuration, and start it up.