使用 Maven 在带有数据源的嵌入式 Tomcat 上进行部署

发布于 2024-09-10 11:20:50 字数 1622 浏览 3 评论 0原文

我有一个多模块 Maven Web 应用程序,它使用 hibernate。

我使用 tomcat:run 目标是为了在 maven 的嵌入式 tomcat 服务器上运行它。到目前为止一切正常。

但现在我需要从 hibernate.properties 中的显式 jdbc 配置切换到数据源。我做了以下操作:

  • hibernate.properties

hibernate.connection.driver_class=oracle.jdbc.OracleDriver
hibernate.connection.url=jdbc:somejdbcurl
hibernate.connection.username=aUser
hibernate.connection.password=aPassword

更改为

hibernate.connection.datasource=java:comp/env/jdbc/datasourcename
  • 在我添加的 web.xml 中将
<资源引用>
    jdbc/datasourcename;
    javax.sql.DataSource
    容器

  • 在 web.xml 旁边添加了一个 context.xml ,其中包含以下内容:
<前><代码><上下文> <资源名称=“jdbc/数据源名称” 授权=“容器” 类型=“javax.sql.DataSource” 用户名=“aUser” 密码=“aPassword” driverClassName =“oracle.jdbc.OracleDriver” url =“jdbc:somejdbcurl” maxActive="2" maxIdle="2"/>

这不起作用,这是预料之中的,因为我没有找到提供包含 Oracle jdbc 驱动程序的 jar 文件的方法。我预计会出现 ClassNotFound 异常或类似的异常,但相反,我

org.hibernate.exception.GenericJDBCException: Cannot open connection

在堆栈中得到了一个带有根本原因的异常:

Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

问题是:

  • 为什么 tomcat 不知道我希望它使用 Oracle 驱动程序?
  • 如何告诉 tomcat 包含驱动程序的 jar 文件?

I have a multi module maven web application, which uses hibernate.

I use the tomcat:run goal in order to run it on the embedded tomcat server of maven. Everything works fine so far.

But now I need to switch from an explicite jdbc configuration in the hibernate.properties to a datasource. I did the following:

  • changed hibernate.properties

from

hibernate.connection.driver_class=oracle.jdbc.OracleDriver
hibernate.connection.url=jdbc:somejdbcurl
hibernate.connection.username=aUser
hibernate.connection.password=aPassword

to

hibernate.connection.datasource=java:comp/env/jdbc/datasourcename
  • in web.xml I added
<resource-ref>
    <res-ref-name>jdbc/datasourcename</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
  • added a context.xml next to the web.xml with the following content:
<Context>
  <Resource name="jdbc/datasourcename"
         auth="Container"
         type="javax.sql.DataSource"
         username="aUser" password="aPassword"
         driverClassName="oracle.jdbc.OracleDriver"
         url="jdbc:somejdbcurl"
         maxActive="2" maxIdle="2"/>
</Context>

This doesn't work, which is expected, since I didn't find a way to provide the jar file, containing the Oracle jdbc driver. I expected a ClassNotFound Exception or something similar, but instead I got a

org.hibernate.exception.GenericJDBCException: Cannot open connection

with a root cause down in the stack:

Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

Soooooo the questions are:

  • Why doesn't tomcat know that I want it to use the Oracle Driver?
  • How do I tell the tomcat about the jar file which contains the driver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

享受孤独 2024-09-17 11:20:50

您需要在插件声明中添加 JDBC 驱动程序:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>tomcat-maven-plugin</artifactId>
  <version>1.0</version>
  <configuration>
    ...
  </configuration>
  <dependencies>
    <dependency>
      <groupId>...</groupId>
      <artifactId>...</artifactId>
      <version>...</version>
    </dependency>
  </dependencies>
</plugin>

顺便说一下,contextFilesrc/main/webapp/META-INF/context.xml

You need to add the JDBC driver inside the plugin declaration:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>tomcat-maven-plugin</artifactId>
  <version>1.0</version>
  <configuration>
    ...
  </configuration>
  <dependencies>
    <dependency>
      <groupId>...</groupId>
      <artifactId>...</artifactId>
      <version>...</version>
    </dependency>
  </dependencies>
</plugin>

By the way, the default value for the contextFile is src/main/webapp/META-INF/context.xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文