Tomcat 上下文资源不会加载 h2 驱动程序
Tomcat context resource won't load h2 driver
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>
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,你注意到了吗?
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>
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,你注意到了吗?