Tomcat上下文资源不会加载h2驱动程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat上下文资源不会加载h2驱动程序相关的知识,希望对你有一定的参考价值。

java.sql.SQLException: Cannot load JDBC driver class 'com.h2.Driver'
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2145)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2037)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1543)
at com.apon.MessageService.getMessage(MessageService.java:68)

建立 我正在使用RESTEasy构建Java RESTService,Apache Shiro用于安全性,jOOQ用作我的ORM。我希望tomcat创建一个资源,处理连接。

当我启动tomcat(tomcat 8.5,而不是EE版本只是常规)时,我没有错误。当我转到/ api / message时,我执行下面的代码getMessage()。

执行失败的代码

@Resource(name = "jdbc/TestingRest-db")
DataSource dataSource;
@GET
@Produces(MediaType.APPLICATION_JSON)
@RequiresUser
public List<MessagePojo> getMessage() {
    InitialContext initCtx = null;
    try {
        initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");

        DataSource ds = (DataSource) envCtx.lookup("jdbc/TestingRest-db");
        if (dataSource == null) {
            // This will always be executed, because it doesn't fill the resource??
            dataSource = ds;
        }
    } catch (NamingException e) {
        e.printStackTrace();
        return null;
    }
    try {
        // This line doesn't really help.
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    List<MessagePojo> list = new ArrayList();
    try {
        Connection connection = dataSource.getConnection();
        messageMyDao = new MessageMyDao(DSL.using(connection, JDBCUtils.dialect(connection)).configuration());
        list = messageMyDao.findAll();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return list;
}

我做了什么 我基本上遵循了教程https://www.mulesoft.com/tcat/tomcat-mysql。我只是用H2代替mysql。但是,似乎驱动程序并没有真正加载。

我尝试执行一些不同的代码,即只使用连接

// Load h2 driver on the classpath. If you do not do this, getConnection will fail.
    try {
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    // The following connection works perfectly fine.
    connection = DriverManager.getConnection(url, userName, password);

这确实有效!所以问题实际上在于通过tomcat加载驱动程序。不是我的应用程序中的pom设置或类路径(h2 jar也位于WEB-INF / lib中)。

Pom.hml

<properties>
    <resteasy.version>3.1.4.Final</resteasy.version>
    <apache.shiro.version>1.3.2</apache.shiro.version>
    <apache.log4j.version>2.9.1</apache.log4j.version>
    <jooq.version>3.10.2</jooq.version>
</properties>
<dependencies>
    <!-- RESTService framework: RESTEasy -->
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-servlet-initializer</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>3.1.4.Final</version>
    </dependency>

    <!-- Security framework: Apache Shiro -->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>${apache.shiro.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-web</artifactId>
        <version>${apache.shiro.version}</version>
    </dependency>

    <!-- Use jOOQ as ORM. -->
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq</artifactId>
        <version>${jooq.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-meta</artifactId>
        <version>${jooq.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen</artifactId>
        <version>${jooq.version}</version>
    </dependency>
    <!-- H2 database -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.196</version>
    </dependency>

Veb.hml

<listener>
    <listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>
<listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/api/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

<resource-ref>
    <res-ref-name>jdbc/TestingRest-db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <injection-target>
        <injection-target-class>com.MessageService</injection-target-class>
        <injection-target-name>dataSource</injection-target-name>
    </injection-target>
</resource-ref>

的context.xml

  <Resource name="jdbc/TestingRest-db" auth="Container" type="javax.sql.DataSource"
           maxTotal="100" maxIdle="30" maxWaitMillis="10000"
           driverClassName="com.h2.Driver"
           url="jdbc:jdbc:mysql:file:./TestingRest-db"/>

我得不到的东西 首先,我不明白为什么@Resource不适用于DataSource。另外,我不知道为什么我的h2驱动程序没有加载。在java代码中我可以创建没有问题的新连接,但我不能在tomcat中。

答案

com.h2.Driver vs org.h2.Driver,你注意到了吗?

以上是关于Tomcat上下文资源不会加载h2驱动程序的主要内容,如果未能解决你的问题,请参考以下文章

NamingException:无法使用 tomcat7-maven-plugin h2/mysql 数据库创建资源实例

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

如何在 tomcat 容器中安装 servlet 并将其加载到每个 Web 应用程序的上下文中?

片段项目不会折叠

WPF 资源加载在 LoadFrom 加载上下文中失败

06-阿里面试题:Tomcat类加载设计 [线程上下文类加载器+破坏性双亲委派机制]