我在NetBeans6.5中使用MySQL数据库和Hibernate开发了一个Java web应用程序。开发数据库服务器和开发应用服务器(Tomcat6)都驻留在我的开发机器上。一切正常;应用程序正确地从数据库中提取数据。
现在,我准备将其移动到生产服务器。同样,DB服务器和应用服务器在同一台机器上。我部署了WAR文件并尝试访问应用程序;我可以访问静态页面,但使用数据库的Servlets出现错误,并出现异常:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
我很确定这个问题与Tomcat不知道数据源有关。看起来好像Netbeans为我处理了这个问题。我读到我可能需要添加一个资源条目,所以我听取了this site的一些建议,它给了我一个context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/EmployeeDirectory">
<Resource
name="jdbc/employeedirectory" auth="Container"
type="javax.sql.DataSource" username="EmployeeDir"
password="EmployeeDirectory" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/EmployeeDirectory?autoReconnect=true"
maxActive="15" maxIdle="7"
validationQuery="Select 1" />
</Context>A web.xml of:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Omit Servlet Info -->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/employeedirectory</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>和一个hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/employeedirectory</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Omit other Mappings -->
<mapping class="EmployeeDirectory.data.PhoneNumber" resource="EmployeeDirectory/data/PhoneNumber.hbm.xml"/>
</session-factory>
</hibernate-configuration>现在,我得到了一个org.hibernate.HibernateException: Could not find datasource错误。
我走在从开发到生产的正确道路上了吗?我遗漏了什么?
发布于 2009-05-13 04:02:30
我认为你在正确的轨道上。我将首先设置数据源,并在hibernate外部验证它。这里有一篇关于这方面的好文章:http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.htm和一些例子:http://www.mbaworld.com/docs/jndi-datasource-examples-howto.html
然后,我会将hibernate配置为使用数据源。从您的hibernate.cfg.xml文件来看,我认为您应该尝试将hibernate.connection.datasource更改为jdbc/employeedirectory
发布于 2009-05-13 10:48:50
应该在/tomcat/server.xml中定义jndi数据源,请参见Tomcat JNDI Datasource how-to,而不是在webapp/context.xml中。
发布于 2009-05-27 05:12:08
Tomcat6要求您将资源标记添加到context.xml,而不是server.xml。您可以在Tomcat5.x中使用。我在单独安装的Tomcat中运行得很好,但我仍在尝试使用NB 6.5中的连接池。
这个Apache站点有一个指向JNDITomcat6版本的链接,并告诉您将资源标记添加到context.xml。
https://stackoverflow.com/questions/855918
复制相似问题