Troubleshooting MySQL jdbc database connector error - java

Need help troubleshooting MySQL database connect issue. First, when I run my Java web application on my development workstation I can successfully connect to my remote/hosted MySQL database.
When I deploy my Java web application to my hosted web server I get the following error org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
I have the mysql-connector-java-5.1.38.bin.jar located in the WEB-INF/libraries folder within my application. Like I said before, this seems to work fine when I run my web application on my local development machine, but fails when deployed to my web host.
Tomcat v7.x is running on my development workstation and my web host is using Tomcat v7.x as well. My development workstation is Windows based. I believe the web host is running CentOS7.x.
The other confusing part is my web application worked with my previous web host as written.
How can I get this problem resolved?
EDIT 3/9/2016
Context.xml:
<Resource
name="jdbc/dbname_ccp"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://domain.com:3306/dbname_ccp"
username="username_admin"
password="dbname_password"
validationQuery="select 1"
/>
web.xml
<resource-ref>
<description>MySQL Datasource</description>
<res-ref-name>jdbc/dbname_ccp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I believe I have the correct user name, password, and database name since this same connection information works when I run my Java web application from my development workstation. When Deployed to the hosted web server I get the following error:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at utilities.DBConnection.getConnectionMySQL(DBConnection.java:33)
at model.account.UserAccountDAO.Register(UserAccountDAO.java:286)
at model.account.ProcessUserAccount.register(ProcessUserAccount.java:237)
at controller.AccountServlet.performTask(AccountServlet.java:50)
at controller.AccountServlet.doPost(AccountServlet.java:76)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:278)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 24 more
I've tried including the mysql-connector-java-5.1.38-bin.jar file in my project application WEB-INF/lib folder as well as in the tomcat /lib folder. It doesn't seem to matter where I place the jar, the server deployment always fails with the error listed above.
How can I get this problem resolved? Is there any other MySQL connection options?

I have meet this problem before,my solution is to add the mysql-connector-java-5.1.38.bin.jar to tomcat/common/lib instead of in web app libraries and solved it.

Related

How to add Google Cloud MySQL libs to Tomcat 8.5

We want to deploy our Applications (.war-Files) via Tomcat 8.5 into a k8s-infrastructure, having the depending Database on Google Cloud SQL (MySQL).
Google offers the MySQL-Connector-Librarys ( see https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/blob/main/docs/jdbc-mysql.md & https://cloud.google.com/sql/docs/mysql/connect-connectors), sadly with only documentation about implementation into the java application itself.
for company strategic reasons we don't want to do that.
We already tried to use a fat jar with all the dependencies of the mysql-connector and put that (together with the connectorJ/8.jar) into the tomcat/libs/ directory and configured the resource in the server.xml as follows:
<Resource name="jdbc/cloudSQL" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000"
factory="com.google.cloud.sql.mysql.SocketFactory" driverClassName="com.mysql.cj.jdbc.Driver" removeAbandonedOnBorrow="true" removeAbandonedTimeout="60" logAbandoned="true"
username="${db.user}" password="${db.pass}" url="jdbc:mysql:///${db.name}?cloudSqlInstance=${db.instance}&autoReconnect=true"
testOnBorrow="true" validationQuery="SELECT 1" />
Doesn't work too bad, but is not successfully either:
javax.naming.NamingException: Could not create resource factory instance
[Root exception is java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to javax.naming.spi.ObjectFactory]
at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:86)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:332)
at org.apache.naming.NamingContext.lookup(NamingContext.java:846)
at org.apache.naming.NamingContext.lookup(NamingContext.java:157)
at org.apache.naming.NamingContextBindingsEnumeration.nextElementInternal(NamingContextBindingsEnumeration.java:115)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:69)
at org.apache.naming.NamingContextBindingsEnumeration.next(NamingContextBindingsEnumeration.java:32)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:138)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:145)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.createMBeans(GlobalResourcesLifecycleListener.java:112)
at org.apache.catalina.mbeans.GlobalResourcesLifecycleListener.lifecycleEvent(GlobalResourcesLifecycleListener.java:87)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:787)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:695)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476)
Caused by: java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to javax.naming.spi.ObjectFactory
at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:75) ... 22 more
I'd appreciate any idea how to tackle this issue.
I replicate the issue on Google Cloud, then instead of using the library from Google Cloud I was able to connect to Cloud SQL using Connector/J following the Tomcat instructions (https://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html)

java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource in eclipse using deployment assembly

I have two projects.
Java SE based let's say abcAPI.
Dynamic Web Project using tomcat server. let's say abc-web
I followed this tutorial automatically build and include one eclipse project into another to include abcAPI into abc-web. So that I have clear separation between these two. I included all necessary dependencies and API's in abcAPI project as well and I believe when using deployment assembly their shouldn't be any problem because underlying referencing project already has reference to it's libraries and dependencies in its libs folder.
I see no error before execution. But after the project runs and BasicDataSource is called I get this exception.
SEVERE: Servlet.service() for servlet [com.trolix.abcAPI.cbf.SrvStart] in context with path [/abc-web] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.commons.dbcp2.BasicDataSource
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at com.trolix.abcAPI.dbcon.DBConnection.<init>(DBConnection.java:38)
at com.trolix.abcAPI.dbcon.DBConnection.getInstance(DBConnection.java:51)
at com.trolix.abcAPI.dao.DAOStart.<clinit>(DAOStart.java:19)
at com.trolix.abcAPI.controller.CtrlStart.<init>(CtrlStart.java:14)
at com.trolix.abcAPI.manager.MgrStart.<init>(MgrStart.java:14)
at com.trolix.abcAPI.cbf.SrvStart.doPost(SrvStart.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Uptil now I have tried many things as suggested by SO community i.e.
Delete Server
Clean server
Restart Eclipse
Build Code
Deploy Again
etc.
But nothing has yet worked for me. Please guide!
I got the same error using iIntelliJ IDEA, Maven, Tomcat Server. As figured out the problem was in Artifacts(The dependency was not included to WEB-INF/lib).
To fix it - open Project Structure -> Artifacts -> drag-and-drop commons-* from Available Elements to lib

Tomcat 7 stops receiving HTTP-requests

I have a Tomcat 7 server that receives a lot of GET-requests. This works very well for some time, then suddenly it stops working (after 7-8 hours).
When it stops working, I am getting this error:
mai 06, 2015 12:47:58 AM org.apache.coyote.http11.AbstractHttp11Processor process
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
All others errors after this, is just the same. I have tried to stop the get-requests, but it crashes after a while anyways. Do you have any ideas?
This is right before it crashes:
[06/May/2015:10:47:11 +0200] "GET /ListenerServlet?request=getLastChanged&_=1430852558104 HTTP/1.1" 200 32
After the crash:
[06/May/2015:10:47:14 +0200] "GET /ListenerServlet?request=getLastChanged&_=1430902035081 HTTP/1.1" 500 1674
After the crash, the Servlet can't get the request parameter.. that throws this nullpointer:
SEVERE: Servlet.service() for servlet [RoomServlet] in context with path [] threw exception java.lang.NullPointerException
at factory.Factory.initEditMode(Factory.java:37)
at factory.Factory.init(Factory.java:20)
at factory.TabsFactory.<init>(TabsFactory.java:19)
at servlet.RoomServlet.getTabs(RoomServlet.java:285)
at servlet.RoomServlet.doGet(RoomServlet.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
followed by:
mai 07, 2015 9:58:00 AM org.apache.catalina.core.StandardContext listenerStop
SEVERE: Exception sending context destroyed event to listener instance of class websocket.drawboard.DrawboardContextListener
java.lang.NoClassDefFoundError: javax/websocket/Endpoint
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2944)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1208)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
at websocket.drawboard.DrawboardContextListener.contextDestroyed(DrawboardContextListener.java:32)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5014)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5659)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: javax.websocket.Endpoint
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
... 17 more
IF there is anything more I need to add, let me know! I really appreciate.
EDIT: Today was a good day, I got a new error! This might have something with the JDBC....
The last packet successfully received from the server was 33 921 291
milliseconds ago. The last packet sent successfully to the server was
1 milliseconds ago. No operations allowed after connection closed.
You can try installing Apache Tomcat v7.0.53 or higher.
Maybe you have a file descriptor leak, which could cause failure in class loading, you can check how many file descriptor are open by using lsof.
I found out the solution to this almost by random.
There was nothing related to:
org.apache.coyote.http11.AbstractHttp11Processor process
but it was rather a JDBC problem.. the connection closed after 8 hours, and I didn't get any errors in the Servlet' for this.
The solution for me was to add some lines in my application's Context:
I added:
url="jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true"
dontTrackOpenResources="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="300000"
This is my full Context now:
<Context>
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="100" maxWait="30000"
username="test" password="bestpassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true"
removeAbandoned="true" removeAbandonedTimeout="10"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
fairQueue="true"
dontTrackOpenResources="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="300000"
/>
</Context>
My server has not crashed in a week now, and I believe it wont crash again.

Tomcat war deployed via IntelliJ IDEA 13 is returning error

I'm getting the following error deploying my first WAR file in IntelliJ. It is a local issue, supposedly, as this is just a WAR file we deploy to test our setup.
All 80 Maven tests are passing, but when I browse to the web app, I get this error:
java.lang.IllegalArgumentException: javax.naming.NameNotFoundException: Name [jdbc/ipam] is not bound in this Context. Unable to find [jdbc].
com.myapp1.ipam.rest.JaxRsApplication.<init>(JaxRsApplication.java:26)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:526)
org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1104)
org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:902)
org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:872)
org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:964)
org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:956)
org.glassfish.jersey.server.ApplicationHandler.createApplication(ApplicationHandler.java:336)
org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:315)
org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:285)
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:310)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
This seems to be where the problem lies:
public JaxRsApplication() {
DataSource datasource;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
datasource = (DataSource) envCtx.lookup("jdbc/ipam");
} catch (NamingException e) {
throw new IllegalArgumentException(e);
}
this.init(new SystemConfiguration(datasource));
}
This is my first time using IntelliJ, and I am new to Java, so I'm not sure what to do here.
Here is context-fragment.xml
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/ipam"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
auth="Container"
type="javax.sql.DataSource"
initialSize="2"
maxActive="5"
maxIdle="5"
minIdle="1"
maxWait="10000"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
defaultReadOnly="false"
username="**********"
password="**********"
driverClassName="org.postgresql.Driver"
validationQuery="select 1"
validationQueryTimeout="5"
logValidationErrors="true"
testOnBorrow="true"
testWhileIdle="true"
url="jdbc:postgresql://db1host/ipam_dev_table_here"/>
</Context>
Make sure that you have setup IntelliJ to deploy the context.xml file when it starts tomcat. You can do this by
Select "Project Structure" from the File menu
From there, select the "Facets" option. Make sure that you have a Web facet configured. If not, add one.
Once the web facet is added, select "Add Application Server specific descriptor..."
Select Tomcat Context Descriptor from the options and click OK.
By default IntelliJ will point to META-INF/context.xml. If this is not the location of your context file, you will need to edit the Path to the file.

Unable to access database after upgrading from Tomcat 5.0.27 to Tomcat 7.0.30

To solve another issue I need to replace my rather old tomcat 5 installation with a tomcat 7 installtion. My application connects to a database using a datsource defined in the applications context.xml. But after moving to Tomcat 7 although my code
for getting a datasource seems to work without error (it doesnt throw an exception or return null)
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/jaikoz");
an attempt to get connection fails:
getDataSource.getConnection()
it fails with
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at com.jthink.appserver.jaikoz.util.db.DBManager.getConnection(DBManager.java:92)
com.jthink.appserver.jaikoz.action.Check.handleRequest(CheckVersion2.java:145)
at com.jthink.appserver.jaikoz.Session.run(Session.java:66)
at com.jthink.appserver.jaikoz.Controller.doTask(Controller.java:68)
at com.jthink.appserver.jaikoz.Controller.doGet(Controller.java:61)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
I'm wondering if a> something works differently on tomcat7 or b> I've forgotten to do something.
This is what I did:
Copy all webapps from tomcat5 to tomcat7
Copy jaikoz.xml file from Conf/Catalina/localhost to tomcat7
Copy common/lib/mysql-connector-java-3.1.4-beta-bin.jar into webapps/jaikoz/WEB-INF/lib
There doesn't seem to be a commons folder on tomcat5 so I've just put the mysql jar into the applications war, but maybe that is wrong
My jaikoz.xml file is
<Context path="/jaikoz" displayname="jaikoz" docBase="../webapps/jaikoz" debug="0" privileged="true">
<Resource name="jdbc/jaikoz"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/jaikoz">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>0</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>0</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>********</value>
</parameter>
<parameter>
<name>password</name>
<value>**********</value>
</parameter>
<!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/jaikoz?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>
It looks like you have another JDBC driver found on the classpath: the ODBC driver (sun.jdbc.odbc.JdbcOdbcDriver). Obviously it doesn't like the MySQL JDBC URL, so you should remove it. It might be installed either in Tomcat itself, or in your application.
Besides, your MySQL JDBC driver looks like a old one (mysql-connector-java-3.1.4-beta-bin.jar). The current version of the driver is 5.1.22, or 3.1.14 for the 3.1 series. You may want to upgrade, especially if you're connecting to a more recent version of MySQL than 3.1.

Categories