我正在尝试连接到Tomcat的server.xml中指定的JNDI数据源。 我的hibernate.cfg.xml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/myDB</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.dialect">org.hibernate.dialect.org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
<mapping class="com.gigatronik.bosch.plugin.remoteserver.db.ConnectEntity"/>
</session-factory>
</hibernate-configuration>
一切正常(所以我假设我的配置是正确的),直到我删除hibernate.dialect
属性 - 隐式启用方言自动检测:
WARN [http-apr-8080-exec-9] org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService HHH000342: Could not obtain connection to query metadata : Cannot create JDBC driver of class '' for connect URL 'null'
和
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final]
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234) ~[hibernate-core-5.0.5.Final.jar:5.0.5.Final]
... 78 more
使用Jetty,自动发现功能非常有效。好像只有Apache Tomcat才有这个问题。如何使用Tomcat启用自动检测?
更新 我的Tomcat配置:
...
<GlobalNamingResources>
...
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource"
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
url="jdbc:derby:myDB;create=true"
maxTotal="100" maxIdle="30" maxWaitMillis="10000" />
...
</GlobalNamingResources>
答案 0 :(得分:0)
我终于找到了问题原因:
未定义应用程序的context.xml中的ResourceLink。在我另外定义
之后<Context path="/foo.bar">
<ResourceLink global="jdbc/myDB" name="jdbc/myDB" type="javax.sql.DataSource"/>
</Context>
连接终于奏效了。事件JNDI现在可以正常工作。所以实际上@Stefan对我的问题的评论是不正确的。
请注意那些描述问题原因的hibernate错误。