好吧,我有这个奇怪的问题。我正在使用Tomcat 5.5和Hibernate。我已经在TOMCAT_HOME/conf/server.xml和我的应用程序的web.xml中添加了资源。
但我使用Hibernate建立会话工厂这样当收到错误:无法连接到数据库使用Tomcat 5.5和休眠
configure.buildSessionFactory()
但我发现了这个错误:
[ WARN]21:04:29 (SettingsFactory.java:buildSettings:144) - Could not obtain connection metadata
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
我的server.xml中的资源配置是这样的:
<Context path="/myApp" docBase="myApp" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/myApp" auth="Container" type="javax.sql.DataSource" driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxPoolSize="100" minPoolSize="5"
acquireIncrement="5"
user="username"
password="1234"
jdbcUrl="jdbc:sqlserver://192.168.1.108:1433;databaseName=myAppDB;autoReconnect=true" />
</Context>
我的web.xml是这样的:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myApp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
这是Hibernate属性我手动初始化在我的Java代码:
hibernate.useJndi = "true"
hibernate.connection.datasource="java:/comp/env/jdbc/myApp"
hibernate.connection.jndi.datasource="jdbc/myApp"
hibernate.dialect="org.hibernate.dialect.SQLServerDialect"
hibernate.current_session_context_class="thread"
所以我的hibernate.cfg.xml基本上是空的,因为配置在Java代码,并通过JNDI来完成。
奇怪的部分是,如果我只修改server.xml和web.xml以使用type =“com.mchange.v2.c3p0.ComboPooledDataSource”以及factory =“org.apache.naming.factory.BeanFactory “而让所有其他东西都一样,它的工作原理!
为什么上述配置不起作用?我从Tomcat 5.5 JNDI教程中获得了它。我需要使用它,因为在获得上述工作之后,我打算扩展org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory。
谢谢。
@Marquinio:很可能是这个问题。另请参阅http://social.msdn.microsoft.com/forums/zh-CN/sqldataaccess/thread/28c1bf87-694a-4520-93d2-51d961f50e35 – shinynewbike 2010-12-08 06:16:24