我在tomcat上的hibernate项目中使用四个配置文件(web.xml、context.xml、hibernate.cfg.xml和persistence.xml)来定义我的数据源到H2数据库。我不清楚这四个配置文件在定义数据源时的用途。
例如,我应该在我的 persistence.xml 属性或 context.xml 中定义用户名和密码。我发现不同的样本以不同的方式定义。
这是我对这些文件的设置,这导致我出现以下异常
(javax.persistence.PersistenceException) javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: Could not open connection
它是由这个嵌套异常引起的
org.apache.tomcat.dbcp.dbcp.SQLNestedException:
Cannot create JDBC driver of class '' for connect URL 'null'
我的 web.xml
<env-entry>
<env-entry-name>name</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>Tomcat</env-entry-value>
</env-entry>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<listener>
<description>PersistenceListener</description>
<listener-class>test.sample.raindance.hibernate.PersistenceListener</listener-class>
</listener>
<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>
上下文.xml
<Context antiJARLocking="true" path="/">
<Resource
name="jdbc/MyApp"
auth="Container"
type="javax.sql.DataSource"
username="sa"
password=""
driverClassName="org.h2.Driver"
url="jdbc:h2:~/test;AUTO_SERVER=TRUE"
maxActive="8"
maxIdle="4"
maxWait="10000"/>
休眠.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.max_fetch_depth">3</property>
<property name="connection.datasource">java:/comp/env/jdbc/MyApp</property>
<!-- Mapping files -->
<mapping class="test.sample.raindance.hibernate.Game"/>
</session-factory>
</hibernate-configuration>
持久性.xml
<persistence-unit name="hibernate-test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/comp/env/jdbc/MyApp</non-jta-data-source>
<class>test.sample.raindance.hibernate.Game</class>
<properties>
<property name="hibernate.connection.datasource" value="java:/comp/env/jdbc/MyApp"/>
<property name="hibernate.id.new_generator_mappings" value ="true"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> -->
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="connection.autocommit" value="false"/>
</properties>
</persistence-unit>
</persistence>
这些设置有什么问题!可能是我写了错误的属性或错误的设置文件。
谢谢你的帮助