0

Is someone able to explain step by step how to configure a connection pool in a maven project with tomcat ?

I have in pom.xml:

...

<build>
     <finalName>myApp</finalName>

     <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/${project.build.finalName}</path>
            <containerConfigXML>src/main/resources/META-INF/context.xml</containerConfigXML>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>6.0.2</version>
                </dependency>
            </dependencies>
        </plugin>
      </plugins>

</build>

src/main/resources/META-INF/context.xml:

<Context>
    <Resource name="jdbc/My_DB"
    auth="Container"
    type="javax.sql.DataSource"
    username="root"
    password="pass"
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/My_DB"
    maxActive="100"
    maxIdle="30"
    maxWait="10000"
    validationQuery="SELECT 1" />
</Context>

And in web.xml:

...
    <resource-ref>
            <description>DB Connection</description>
            <res-ref-name>jdbc/My_DB</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
        </resource-ref>

My Connection Manager:

public class ConnectionManager {

    DataSource ds;  


    public ConnectionManager() {

        try {
            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            ds = (DataSource) envContext.lookup("jdbc/My_DB");
        } catch (Exception e) {
            throw new MyException(e);
        }
    }

    public Connection getConnection() {

        Connection connection = null;

        try {
            connection = ds.getConnection();
        } catch (SQLException e) {

            throw new MyException(e);
        }


        return connection;
    }

I use Tomcat 7 and I have little experience with maven.

Did I do something wrong? Something is missing?

Exception:

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

EDIT: SOLVED! -I moved "META-INF/context.xml" in /webapps -In pom.xml:

<plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>2.2</version>

                <configuration>
                    <contextFile>./src/main/webapp/META-INF/context.xml</contextFile>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.38</version>
                    </dependency>
                </dependencies>
            </plugin>
fuerteVentura22
  • 93
  • 1
  • 2
  • 13
  • Have you tried google?! [Cannot create JDBC driver of class ' ' for connect URL 'null'](http://stackoverflow.com/questions/11516747/cannot-create-jdbc-driver-of-class-for-connect-url-null-i-do-not-underst) – KlajdPaja May 23 '16 at 10:57
  • Yes, I tried, but it does not work with that solution. – fuerteVentura22 May 23 '16 at 12:16
  • Have you tried removing the resource config from `src/main/resources/META-INF/context.xml` to tomcat's `conf/context.xml` – KlajdPaja May 23 '16 at 12:39
  • My last advice would be to use mysql-connector-java v. 5.1.38, maybe there is some bug related to java versions... – KlajdPaja May 23 '16 at 12:58

0 Answers0