配置 Hibernate 以使用 Glassfish 连接池

发布于 2024-12-11 17:20:59 字数 7611 浏览 0 评论 0原文

经过一整天的谷歌搜索,我看到了很多关于这个问题的讨论,甚至有一个据说可以解决问题的指南[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>

当我运行时,

config.configure("hibernate.cfg.xml");

我从 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.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: [email protected] )
      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/作为数据源进行测试,但是当它尝试将 SessionFactory 绑定到 java:comp/env/hibernate/SessionFactory 时,它会得到“javax.naming.NamingException: java:comp 命名空间不能 修改的”。

我在 glassfish 的配置中遗漏了一些东西吗?

相关:如何设置hibernate以使用Glassfish连接池?

[1] http://wooden Circuit.com/?page_id=42

After googleing for a whole day, I've seen a lot of discussion on this issue, even a guide which supposedly solves the problem[1], but until now I didn't get to a solution.
I want to configure my web service, which relies on hibernate 3 to use a connection pool defined on my glassfish open source v3 server. In order to do that, I have configured in my glassfish server a connection pool named TestPool. If ping this connection from the glassfish interface, I get a "Ping Succeeded" message. Associated with this connection pool, I also have a JDBC resource named jdbc/Test.

Back to hibernate, my hibernate.cfg.xml file looks like this:

<?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>

When I run

config.configure("hibernate.cfg.xml");

I get this output from 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

Then, when I run

 config.buildSessionFactory();

I get this log:

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: [email protected] )
      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)

So, for what I've seen, hibernate is correctly using jdbc/Test as the datasource, but when it tries to bind the SessionFactory to java:comp/env/hibernate/SessionFactory, it gets the "javax.naming.NamingException: java:comp namespace cannot be modified".

Am I missing something in the configuration of glassfish?

Related: How to set up hibernate to use Glassfish connection pool?

[1] http://woodencircuit.com/?page_id=42

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中性美 2024-12-18 17:20:59

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

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

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

另请参阅:

Your exception is not related to using Glassfish pool, it's related to JNDI-bound SessionFactory.

If all that you need is to use a Glassfish pool in Hibernate, you don't need to bind SessionFactory to JNDI, just remove name="java:comp/env/hibernate/SessionFactory" and use it as before (with HibernateUtils or something like that).

Regarding the exception itself, it identifies the problem pretty clearly. If you want to obtain session factory from JNDI, try to use some other JNDI namespace, for example, name = "java:hibernate/SessionFactory".

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文