gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:48:26 27 4
gpt4 key购买 nike

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.xml

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

Web.xml

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

上下文.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,你注意到了吗?

关于java - Tomcat 上下文资源不会加载 h2 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com