1

I developed a small web-application to Tomcat8 to just discover that I should deploy it in Tomcat7. Downgrading such app to Tom7 is not an easy task, but what is bottering me is that I've got an error in creating the BasicDataSource from the library org.apache.tomcat.dbcp.dbcp2.BasicDataSource.

This library can be found in commons-dbcp2 dependency in Maven. Nevertheless, in spite of getting this in my POM, either Eclipse IDE and mvn install are not finding the reference and everything has melt down. Comming back to Tom8 didn't solved the problem, and I am wondering what configuration in Eclipse and Maven is messed up.

Q: How to solve this DPC2 dependency/configuration in order to run application on Tomcat 7, if possible?

Any help would be worth. Thanks.

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>myApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Application Name</name>
  <description>Application Description</description>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.12.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.5.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.0.2.RELEASE</version>
        <exclusions>
          <exclusion>
              <groupId>org.springframework</groupId>
              <artifactId>spring-aop</artifactId>
          </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-dbcp2</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.2</version>
    </dependency>
  </dependencies>
</project>

JAVA:

   @Bean(name="restDataSource")//Bean name adicionado posteriormente. Pode gerar problemas.
   public DataSource restDataSource() {
      BasicDataSource dataSource = new BasicDataSource(); //"BasicDataSource cannot be resolved to a type"
      dataSource.setDriverClassName(env.getProperty("database.driver"));
      dataSource.setUrl(env.getProperty("database.url"));
      dataSource.setUsername(env.getProperty("database.user"));
      dataSource.setPassword(env.getProperty("database.password"));
      dataSource.setInitialSize(Integer.valueOf(env.getProperty("pool.initialSize")));
      dataSource.setMaxIdle(Integer.valueOf(env.getProperty("pool.maxIdle")));
      dataSource.setMinIdle(Integer.valueOf(env.getProperty("pool.minIdle")));
      dataSource.setMaxTotal(Integer.valueOf(env.getProperty("pool.maxActive")));

      return dataSource;
   }

MAVEN ERROR:

[ERROR] location: class com.mycompany.PersistenceConfiguration
[ERROR] /foo/bar/.../conf/PersistenceConfiguration.java:[51,40] cannot find symbol
[ERROR] symbol:   class BasicDataSource
Alex
  • 3,325
  • 11
  • 52
  • 80
  • You have a different error message as a line comment in the source. Do you actually have a jar file containing the same *fully* *qualified* `BasicDataSource` type you're trying to use? – nitind Mar 19 '18 at 19:53
  • @nitind, I suppose that this jar should be downloaded and managed through maven dependency on commons-dbcp2 artifact, declared in POM. When I look to Maven dependencies in my project, there's a commons-dbcp2-2.0.jar inside the folder. I think that this should be the file. – Alex Mar 19 '18 at 19:58
  • Suppose? Should? Maybe @silver-shroud's answer is the right one. – nitind Mar 19 '18 at 22:03
  • problem seems deeper than I thought. – miskender Mar 20 '18 at 19:11
  • I have the feeling that Tomcat 8 embeds what is necessary to this kind of configuration. Since I have had to step-back to Tomcat 7, a plenty of issues are now raising up. – Alex Mar 20 '18 at 19:17

1 Answers1

0

For a mater of knowledge building, I am posting the solution that I've found:

I turned everything back to Tomcat 8 environment. After making sure that application was working properly, I started to change one thing at a time and tested if it would run properly. This way I did the following:

  • Switched org.apache.tomcat.dbcp.dbcp2.BasicDataSource to org.apache.commons.dbcp2.BasicDataSource

  • Changed project's facets from Dynamic Web Module 3.1 to 3.0, Java 1.8 to Java 1.7 and runtime Tomcat 8 to Tomcat 7.

  • After that, an error was being thrown from hibernate validator. Then I drawn it out form POM hibernate-validator, because it was not being used.

After all, application worked.

Alex
  • 3,325
  • 11
  • 52
  • 80