Error starting webapp with Mybatis, c3p0 on Tomcat 8 - java

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.

Related

is tomEE aware of maven dependency? [duplicate]

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.

Wildfly 10 classloader hell (hibernate doesn't see Postgres Driver installed as module)

I use wildfly Entity Manager for my application, but besides that I need to establish jdbc connection in runtime for other purposes, so I receiving settings from service, in runtime, during application start and establish this connection.
I tried to use sql2o and it works just fine, but then I tried hibernate (I'm creating it programatically), and it didn't work. I took a look on internals and figured out why it doesn't work.
When library calls DriverManager.getConnection() it checks callers ClassLoader. And when sql2o was loaded with an application just like Postgres Driver, the hibernate is not, and it fails.
creating of client sql2o that works just fine
creating hibernate programatically and it fails
Then I was trying to use PostgresDriver installed as Wildfly module and as deployment both ways didn't work.
Furthermore I tried to extract hibernate from wildfly with jboss-deployment-structure.xml, and use bundled from my application. Again it doesn't work.
Every time I'm getting
Caused by: java.sql.SQLException: No suitable driver found for jdbc:postgresql...
I found among registered drivers PostgresDriver loaded with Wildfly's
with classloader id "org.postgres:main", when caller (hibernate's) ClassLoader
has id = "org.hibernate:main". So it's wildfly's modules, but why don't they see each other???
The problem:
I use ear packaging with 2 subdeployments (jar + war), how can I solve this problem, and make wildfly's hibernate see installed as wildfly module Postgres Driver?

JDBC Connection Pool / JDBC resource

I am a beginner working on a project in Netbeans and using Glasshfish, but I have to deploy on TOMCAT on Elastic BeanStalk. I don't know anything about tomcat. To connect to my database, I use Entity Classes and a named connection jdbc/whatever (I hear this is called a JDNI name) in my persistence.xml.
On the Glassfish server bundled with Netbeans, I added a Connection pool and a JDBC resource. This was done with a GUI. It was super simple. Create the JDBC connection pool after you stuffed the right driver in the right folder by pointing to the location of server/password/etc. Then point the jdbc resource at the pool.
Now I need to do this with TOMCAT and XML. I have no idea what I am doing...
How do I create a connection pool which all my applications can reach?
How do I then get JNDI? set up that persistence.xml can reference and make the magic db stuff happen?
How do I then set up a jdbc authentication realm based on this database?
It was magic in Glassfish is it was so GUI. But I don't have a GUI for TOMCAT.
I am using spring.

Play Framework 2.2.2 on Tomcat 7 - Remove BoneCP

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.

Convert Spring Roo Application into a JBoss 6 Application

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.

Categories

Resources