mysql - 命名异常 : Cannot create resource instance with tomcat7-maven-plugin h2/mysql database

标签 mysql maven tomcat7 h2 tomcat-jdbc

我正在尝试创建 Maven Web 应用程序项目,该项目在打包时生成独立的可执行 jar,为此我使用 tomcat7-maven-plugin。最初的 hello world 类型的应用程序运行良好,但我正在努力使其与数据库一起使用。 这是我的 pom.xml 的样子:

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
    <execution>
        <id>tomcat-run</id>
        <goals>
            <goal>exec-war-only</goal>
        </goals>
        <phase>package</phase>
        <configuration>
            <contextFile>src/main/resources/tomcat/context.xml</contextFile>
            <path>/helloworld</path>
            <enableNaming>true</enableNaming>
            <finalName>standalone.jar</finalName>
            <charset>utf-8</charset>
        </configuration>
    </execution>
</executions>
<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.183</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
</dependencies>

src/main/webapp/WEB-INF/web.xml:

<web-app>
...
<servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.mycompany.test.TestServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/helloworld</url-pattern>
    </servlet-mapping>
    <resource-ref>
        <description>H2 DS</description>
        <res-ref-name>jdbc/H2DB</res-ref-name>
        <res-type>org.apache.tomcat.jdbc.pool.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    <resource-ref>
        <description>Mysql DS</description>
        <res-ref-name>jdbc/TestDB</res-ref-name>
        <res-type>org.apache.tomcat.jdbc.pool.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
...
</web-app>

src/main/resources/tomcat/context.xml

<Context path="/helloworld">
    <Resource type="org.apache.tomcat.jdbc.pool.DataSource"
        name="jdbc/H2DB"
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        driverClassName="org.h2.Driver"
        url="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1"
        username="testuser"
        password="testpassword"
        initialSize="5"
        maxActive="10"
        maxIdle="5"
        minIdle="2"
    />
    <Resource name="jdbc/TestDB" auth="Container" type="org.apache.tomcat.jdbc.pool.DataSource"
        url="jdbc:mysql://localhost:3306/testdb"
        username="testuser" 
        password="testpassword" 
        driverClassName="com.mysql.jdbc.Driver"
        initialSize="5"
        maxActive="10"
        maxIdle="5"
        minIdle="2"
    />
</Context>

在我的java代码中我正在尝试:

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/TestDB");

或 java:/comp/env/jdbc/H2DB 用于 h2 数据库 当 tomcat 启动我的 servlet 时,这两种方式都会遇到相同的异常:

SEVERE: Servlet /helloworld threw load() exception javax.naming.NamingException: Cannot create resource instance at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146) at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source) at org.apache.naming.NamingContext.lookup(NamingContext.java:843) at org.apache.naming.NamingContext.lookup(NamingContext.java:154) at org.apache.naming.NamingContext.lookup(NamingContext.java:831) at org.apache.naming.NamingContext.lookup(NamingContext.java:154) at org.apache.naming.NamingContext.lookup(NamingContext.java:831) ...

如果有任何想法,我将不胜感激。 提前致谢。

最佳答案

通过将 mysql-connector-java 添加到 tomcat7-maven-plugin 的配置中解决了问题,如 here 所述.

关于mysql - 命名异常 : Cannot create resource instance with tomcat7-maven-plugin h2/mysql database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27621282/

相关文章:

java - 从 Tomcat servlet 运行 sudo 命令

mysql - 将 Sql 结果集存储在嵌套结构中,如果为空则省略

mysql - MySQL中如何确定特定时间间隔的查询?

php - UNION Mysql 查询不起作用

java - 如何使用 Maven 构建存储(新构建时间戳文件夹和最新文件夹)?

java - 普通用户的 geotools 和 maven

java - 无法在 web.xml 中解析 Servlet

java - 将 SLF4J 记录到 ElasticBeanstalk

java - Tomcat 7 集成在 eclipse 类路径问题

MySQL:MAX(COUNT(*)) -- 错误 1111