8

我有一个使用休眠的多模块 Maven Web 应用程序。

我使用 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 我添加
<resource-ref>
    <res-ref-name>jdbc/datasourcename</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
  • 在 web.xml 旁边添加了一个 context.xml,内容如下:
<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>

这不起作用,这是意料之中的,因为我没有找到提供包含 Oracle jdbc 驱动程序的 jar 文件的方法。我期待一个 ClassNotFound Exception 或类似的东西,但我得到了一个

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'

Sooooo问题是:

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

1 回答 1

12

您需要在插件声明中添加 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>

顺便说一句,默认contextFile值为src/main/webapp/META-INF/context.xml.

于 2010-07-20T13:25:23.210 回答