Java 配置Hibernate以使用Glassfish连接池

Java 配置Hibernate以使用Glassfish连接池,java,hibernate,glassfish,jndi,connection-pooling,Java,Hibernate,Glassfish,Jndi,Connection Pooling,在谷歌搜索了一整天之后,我看到了很多关于这个问题的讨论,甚至有一本据说可以解决这个问题的指南[1],但直到现在我还没有找到解决方案。 我想配置我的web服务,它依赖hibernate 3使用glassfish开源v3服务器上定义的连接池。为此,我在glassfish服务器中配置了一个名为TestPool的连接池。如果从glassfish接口ping此连接,我会收到一条“ping成功”消息。与此连接池相关联的还有一个名为JDBC/Test的JDBC资源 回到hibernate,我的hibernat

在谷歌搜索了一整天之后,我看到了很多关于这个问题的讨论,甚至有一本据说可以解决这个问题的指南[1],但直到现在我还没有找到解决方案。 我想配置我的web服务,它依赖hibernate 3使用glassfish开源v3服务器上定义的连接池。为此,我在glassfish服务器中配置了一个名为TestPool的连接池。如果从glassfish接口ping此连接,我会收到一条“ping成功”消息。与此连接池相关联的还有一个名为JDBC/Test的JDBC资源

回到hibernate,我的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 name="java:comp/env/hibernate/SessionFactory">
         <property name="connection.datasource">jdbc/Test</property>
         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>
我从hibernate获得以下输出:

INFO [http-thread-pool-8080-(4)] (Configuration.java:2126) - configuring from resource: hibernate.cfg.xml

INFO [http-thread-pool-8080-(4)] (Configuration.java:2145) - Configuration resource: hibernate.cfg.xml

WARN [http-thread-pool-8080-(4)] (DTDEntityResolver.java:73) - recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!

INFO [http-thread-pool-8080-(4)] (Configuration.java:2267) - Configured SessionFactory: java:comp/env/hibernate/SessionFactory
然后,当我跑的时候

config.configure("hibernate.cfg.xml");
 config.buildSessionFactory();
我得到这个日志:

INFO [http-thread-pool-8080-(4)] (AnnotationBinder.java:532) - Binding entity from annotated class: ....

...lots of bindings in here...

INFO [http-thread-pool-8080-(4)] (CollectionBinder.java:745) - Mapping collection: ...

...other mapping collections...

INFO [http-thread-pool-8080-(4)] (Configuration.java:1646) - Hibernate Validator not found: ignoring

INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.

INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.

INFO [http-thread-pool-8080-(4)] (HibernateSearchEventListenerRegister.java:75) - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.

INFO [http-thread-pool-8080-(4)] (NamingHelper.java:49) - JNDI InitialContext properties:{}

INFO [http-thread-pool-8080-(4)] (DatasourceConnectionProvider.java:84) - Using datasource: jdbc/Test

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:117) - Database ->
       name : MySQL
    version : 5.1.58-1ubuntu1
      major : 5
      minor : 1

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:123) - Driver ->
       name : MySQL-AB JDBC Driver
    version : mysql-connector-java-5.1.18 ( Revision: tonci.grgin@oracle.com-20110930151701-jfj14ddfq48ifkfq )
      major : 5
      minor : 1

INFO [http-thread-pool-8080-(4)] (Dialect.java:108) - Using dialect: org.hibernate.dialect.MySQLDialect

INFO [http-thread-pool-8080-(4)] (TransactionFactoryFactory.java:59) - Using default transaction strategy (direct JDBC transactions)

INFO [http-thread-pool-8080-(4)] (TransactionManagerLookupFactory.java:80) - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:179) - Automatic flush during beforeCompletion(): disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:183) - Automatic session close at end of transaction: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:190) - JDBC batch size: 15

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:193) - JDBC batch updates for versioned data: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:198) - Scrollable result sets: enabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:206) - JDBC3 getGeneratedKeys(): enabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:214) - Connection release mode: auto

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:238) - Maximum outer join fetch depth: 2

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:241) - Default batch fetch size: 1

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:245) - Generate SQL with comments: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:249) - Order SQL updates by primary key: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:253) - Order SQL inserts for batching: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:431) - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory

INFO [http-thread-pool-8080-(4)] (ASTQueryTranslatorFactory.java:47) - Using ASTQueryTranslatorFactory

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:261) - Query language substitutions: {}

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:266) - JPA-QL strict compliance: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:271) - Second-level cache: enabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:275) - Query cache: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:406) - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:285) - Optimize cache for minimal puts: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:294) - Structured second-level cache entries: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:323) - Statistics: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:327) - Deleted entity synthetic identifier rollback: disabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:343) - Default entity-mode: pojo

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:347) - Named query checking : enabled

INFO [http-thread-pool-8080-(4)] (SettingsFactory.java:351) - Check Nullability in Core (should be disabled when Bean Validation is on): disabled

INFO [http-thread-pool-8080-(4)] (SessionFactoryImpl.java:200) - building session factory


INFO [http-thread-pool-8080-(4)] (SessionFactoryObjectFactory.java:109) - Factory name: java:comp/env/hibernate/SessionFactory

INFO [http-thread-pool-8080-(4)] (NamingHelper.java:49) - JNDI InitialContext properties:{}

INFO [http-thread-pool-8080-(4)] (NamingHelper.java:91) - Creating subcontext: hibernate

WARN [http-thread-pool-8080-(4)] (SessionFactoryObjectFactory.java:121) - Could not bind factory to JNDI

javax.naming.NamingException: java:comp namespace cannot be modified
at com.sun.enterprise.naming.impl.JavaURLContext.createSubcontext(JavaURLContext.java:326)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:92)
因此,就我所见,hibernate正确地使用jdbc/Test作为数据源,但是当它尝试将SessionFactory绑定到java:comp/env/hibernate/SessionFactory时,它得到了“javax.naming.NamingException:java:comp命名空间无法修改”

我在glassfish的配置中遗漏了什么吗

相关的:


[1]

您的异常与使用Glassfish pool无关,它与JNDI绑定的
SessionFactory
有关

如果您只需要在Hibernate中使用Glassfish池,那么不需要将
SessionFactory
绑定到JNDI,只需删除
name=“java:comp/env/Hibernate/SessionFactory”
并像以前一样使用它(与
HibernateUtils
或类似的东西一起使用)

关于异常本身,它非常清楚地识别了问题。如果要从JNDI获取会话工厂,请尝试使用其他JNDI名称空间,例如,
name=“java:hibernate/SessionFactory”

另请参见:


删除名称属性解决了我的问题,谢谢。我认为我需要将SessionFactory绑定到JNDI名称。尽管如此,当我尝试使用您建议的其他名称空间时,我得到:javax.naming.NamingException:SerialContext中“java:hibernate”的查找失败[根异常为javax.naming.NameNotFoundException:没有绑定到名称java:hibernate的对象]