简体   繁体   中英

Can't connect to database using Tomcat 5.5 and Hibernate

Ok I have this weird problem. I'm using Tomcat 5.5 with Hibernate. I have added the resouce in the TOMCAT_HOME/conf/server.xml and also in my application's web.xml.
But I'm getting error when using Hibernate to build the session factory like this:

 configure.buildSessionFactory()

But I'm getting this error:

[ 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'

My server.xml resource configuration is like this:

<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>

My web.xml is like this:

    <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> 

This are the Hibernate properties I'm initializing in my Java code manually:

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"

So my hibernate.cfg.xml is basically empty, since configuration is done in Java code and through JNDI.

The weird part is that if I only modify the server.xml and web.xml to use type="com.mchange.v2.c3p0.ComboPooledDataSource" along with factory="org.apache.naming.factory.BeanFactory" and leaving all other stuff the same, it works!!!

Why is the above configuration not working? I took it from Tomcat 5.5 JNDI tutorial. I need to use it because after I get the above working I'm planning to extend org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory.

Thanks.

Is your database driver JAR on the TOMCAT/lib directory? It depends on which layer is creating and managing the DB pool. If you do move the JAR, also be sure to remove it from the WEB-INF/lib directory. One or the other, not both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM