2010-07-20 94 views
7

我有一个使用休眠的多模块maven web应用程序。使用Maven在数据源上部署嵌入式Tomcat

我使用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> 
  • 加入的下一个context.xml中与以下内容中的web.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的异常或类似的东西,而是我得到了

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' 

Soooooo的问题是:

  • 为什么没有按Tomcat知道我希望它使用Oracle驱动程序吗?
  • 如何告诉tomcat关于包含驱动程序的jar文件?

回答

11

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

+0

看起来很有希望,我会试试明天 – 2010-07-20 16:19:53

+0

谢谢指出默认的contextFile位置..我花了几个小时追逐.. – 2014-03-12 20:06:46