Ubuntu Tomcat7 java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory
Asked Answered
P

6

28

I'm trying to set up a JDBC DataSource in Tomcat 7 under Ubuntu 12.X, so I added the following to the context.xml file:

<Resource name="jdbc/myDS" auth="Container" type="javax.sql.DataSource"
    maxActive="5" maxIdle="2" maxWait="5000"
    driverClassName="org.postgresql.Driver" username="usr" password="***" url="jdbc:postgresql://localhost:5432/db" />

Obviously, using the right and tested database user id and password. When I restart Tomcat, I get the this error:

Feb 05, 2013 1:10:01 PM org.apache.catalina.core.NamingContextListener addResource
WARNING: Failed to register in JMX: javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]

I googled, and found out the JDBC driver has to be copied to the $CATALINA_HOME/lib folder, so I copied the postgresql-9.2-1000.jdbc4.jar to /usr/share/tomcat7/lib, but it didn't help. I tried copying the file to other locations, with the same results.

Another attempt was to change the tomcat-dbcp.jar symlink in /usr/share/tomcat7/lib from ../../java/tomcat-dbcp-7.0.30.jar to ../../java/tomcat-dbcp.jar. The only change was I got only one warning instead of four, but the datasource doesn't work either.

Java version:

jdoe@sever:~$ java -version
java version "1.7.0_09"
OpenJDK Runtime Environment (IcedTea7 2.3.4) (7u9-2.3.4-0ubuntu1.12.10.1)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)

Any hint, very welcomed.

Cheers.

Political answered 5/2, 2013 at 16:34 Comment(3)
The double dbcp in org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory is rather suspicious.Indies
Don't want to necro, but the double dbcp in the package name is correct. tomcat-dbcp.jar is basically a refactored/repackaged version of commons-dbcp.jar and this package is correct.Bryner
Thanks for asking! Got hit by this tooDebauch
C
38

The library tomcat-dbcp-7.0.30.jar from repositories is corrupted.

Replace it with:

sudo wget -O /usr/share/java/tomcat-dbcp-7.0.30.jar http://search.maven.org/remotecontent?filepath=org/apache/tomcat/tomcat-dbcp/7.0.30/tomcat-dbcp-7.0.30.jar

Chickpea answered 8/4, 2013 at 13:46 Comment(5)
OMG this angers me. I burned a lot of time yesterday chasing this JNDI issue. Do these 'maintainers' have smoke tests? You're supposed to be able to trust a core server component from a distro, right?Flap
Wow, I hope they fix this right away. What a pain! I created a hack-around where my pool class just creates a new connection each time using hard coded username&passwords. Thank you for letting me go back and do this the right way!Service
for me it didn't work, but I changed the path to /usr/share/java/tomcat7/tomcat-dbcp-7.0.30.jar and it workedDroplet
Actually, in Debian, it worked after I copied the aforementioned file to usr/share/tomcat7/lib (not /usr/share/java). There was not such file in the first place, so I didn't replace any corrupted version anyway).Sludge
I got the mixture of the two above on an EZ2 instance. The lib didn't exist at all, and I put it in /usr/share/java/tomcat7/tomcat-dbcp-7.0.30.jar. Seems ok. This is why I never install Tomcat from the repository and get it from their website instead. You don't get the the managed updates, but that is a small price to pay in comparison.Kike
P
26

The cause is a issue in the Ubuntu build/package process for Tomcat7. If I understand the issue correctly, Apache builds tomcat-dbcp.jar from binary files, while Ubuntu builds packages only from source. The Ubuntu project ends up needing to change the Java package name, which tends to break things for us poor users. The gory details may be found at the Ubuntu issues list.

The solution I found is to name the data source factory when I define the resource. In one case, I have a META-INF/context.xml file that contains:

<Resource name="jdbc/myDataSource"
    auth="Container"
    type="javax.sql.DataSource"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/myDatabase"
    username="username" password="password"
    validationQuery="SELECT COUNT(*) FROM MY_TABLE"
    factory="org.apache.commons.dbcp.BasicDataSourceFactory" />

The critical element is the "factory" declaration, which overrides the built-in default.

On our production machines, the resource is defined in the GlobalNamingResources element of the server.xml file. Specifying the factory is only needed on the Ubuntu systems.

Patsis answered 16/2, 2013 at 15:8 Comment(3)
Note that using this factory means that you are no longer using the Tomcat-provided commons-dbcp pool. The Tomcat-provided commons-dbcp pool does in fact have the 'double' dbcp.dbcp package name. This is to prevent conflicts with the "real" commons-dbcp library.Heinrik
This applies to recent Gentoo as wellNieberg
This is how we do it on CentOS 6 and CentOS 7 as well.Immune
F
1

i had the same problem on CentOS. I got arounbd this by downloading a fresh copy of tomcat from site and uploaded tomcat-dbcp.jar to my online server lib, restart server :)

Fortify answered 18/2, 2014 at 14:32 Comment(0)
L
1

I had the same problem on Fedora 20 with Tomcat 7.0.55. I replaced the 7.0.30 with 7.0.55 in the file path and file name and this worked for me. Not sure why but this file was completely missing from the YUM install for tomcat 7. Cant use a database without it.

Last answered 19/9, 2014 at 19:42 Comment(0)
L
0

That did it.

Make sure if the tomcat-dbcp-7.0.30.jar file does not have the below size, then it may be corrupt and you may need to replace it by the sudo wget command above.

-rw-r--r-- 1 root root 235411 May 1 2013 tomcat-dbcp-7.0.30.jar

lrwxrwxrwx 1 root root 22 Jan 10 2013 tomcat-dbcp.jar -> tomcat-dbcp-7.0.30.jar

Lewan answered 28/1, 2014 at 20:43 Comment(0)
B
0

If you don't feel like patching tomcat you can (on CentOS) also add the following to the JAVA_OPTS (e.g. by adding it in /usr/share/tomcat/conf/context.xml

JAVA_OPTS="-Djavax.sql.DataSource.Factory=org.apache.commons.dbcp.BasicDataSourceFactory"
Backbreaking answered 6/8, 2019 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.