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>