我正在尝试从 Tomcat 7.0.23 升级到 7.0.27(实际上正在运行 Liferay)。尝试通过 Hibernate 访问数据源时出现以下异常:
16:28:27,694 ERROR [http-bio-8080-exec-1][DatasourceConnectionProvider:110] Could not find datasource: java:/comp/env/jdbc/MySqlDS
javax.naming.NameNotFoundException: Name [java:/comp/env/jdbc/MySqlDS] is not bound in this Context. Unable to find [java:].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:90)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
[...]
完全相同的代码在 7.0.23 中运行良好(我可以在任一实例中删除 WAR 文件)。
这是我的 $CATALINA_HOME/conf/context.xml 文件(已删除评论):
<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/MySqlDS" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
username="xxx"
password="xxx"
url="jdbc:mysql://localhost:3306/xx?useUnicode=true&characterEncoding=UTF-8&relaxAutoCommit=true"
maxActive="200" maxIdle="30" maxWait="10000"
minIdle="3"
testWhileIdle="true" testOnBorrow="true"
timeBetweenEvictionRunsMillis="120000"
minEvictableIdleTimeMillis="600000"
validationQuery="select 1"
/>
</Context>
来自应用程序的 web.xml:
<?xml version="1.0"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HibernateServlet</servlet-name>
<servlet-class>com.liferay.samplehibernate.servlet.HibernateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HibernateServlet</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
<taglib-location>
/WEB-INF/tld/liferay-portlet.tld
</taglib-location>
</taglib>
</jsp-config>
<resource-ref>
<description>data source</description>
<res-ref-name>jdbc/MySqlDS</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"?>
<!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="current_session_context_class">thread</property>
<!-- Mappings -->
<property name="connection.datasource">java:/comp/env/jdbc/MySqlDS</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping
resource="com/liferay/samplehibernate/model/FoodItem.hbm.xml" />
</session-factory>
</hibernate-configuration>
关于我所缺少的任何建议?谢谢。