使用JNDI进行Hibernate:无法为连接URL'null'创建类''的JDBC驱动程序

时间:2011-03-29 21:37:46

标签: hibernate spring tomcat jndi

我尝试使用Hibernate通过JNDI处理MySQL,但不能(来自catalina.out):

INFO: Deploying web application archive mystamps.war
Cannot create JDBC driver of class '' for connect URL 'null'
java.lang.NullPointerException
    at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
    at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
    at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)

(我减少了完整的堆栈跟踪,因为它没用:没有原因或任何其他异常。)

弹簧servlet.xml中:

    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/mystamps" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="ru.mystamps.web.dao" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
        </property>
    </bean>

/etc/tomcat6/server.xml:

<Host name="my-stamps" appBase="webapps"
    unpackWARs="false" xmlValidation="false"
    xmlNamespaceAware="false">
</Host>

/etc/tomcat6/Catalina/my-stamps/mystamps.xml:

<?xml version='1.0' encoding='utf-8'?>
<Context path="" docBase="mystamps" allowLinking="true" reloadable="true">
    <Resource name="jdbc/mystamps" auth="Containter"
        type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
        username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/mystamps" />
</Context>

我使用以下版本:Spring MVC 3.0.5,Hibernate 3.6.2.Final,MySQL 5.1.52,mysql-connector-java 5.1.15(位于/ usr / share / tomcat6 / lib)和Tomcat 6.0。 30

我使用谷歌并尝试自己修复它,但没有运气:(

你能帮助我吗 - 我做错了什么?

TIA

2 个答案:

答案 0 :(得分:0)

您是否尝试将mysql-connector jar文件放入打包的war文件的WEB-INF / lib文件夹中?

或者以下链接可能会有所帮助:http://ubuntuforums.org/showthread.php?t=430133

答案 1 :(得分:0)

有同样的错误,当我将JNDI声明的位置移动到context.xml时,我的错误就消失了。此错误意味着spring无法找到JNDI信息。 server.xml中不需要链接

/etc/tomcat6/context.xml

<?xml version='1.0' encoding='utf-8'?>
<Context path="" docBase="mystamps" allowLinking="true" reloadable="true">
    <Resource name="jdbc/mystamps" auth="Containter"
        type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
        username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/mystamps" />
</Context>

参考:http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

希望这有帮助!