Tomcat 7 - Maven项目中的连接池

时间:2016-05-23 10:34:09

标签: mysql maven tomcat jdbc jndi

是否有人能够逐步解释如何使用tomcat在maven项目中配置连接池?

我在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 /主/资源/ 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>

在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>

我的连接管理器:

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;
    }

我使用Tomcat 7,我对maven没什么经验。

我做错了什么吗?有什么东西丢失了?

例外:

  

org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法创建JDBC   类''用于连接URL的驱动程序'null'

编辑:已解决! - 我在/ webapps中移动了“META-INF / context.xml” - 在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>

0 个答案:

没有答案