4

Spring用于我们团队的Java EE项目,hibernate用于底层ORM。

transactionManager 设置如下:

<bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> 

sessionFactory 设置如下:

<bean id="sessionFactory"  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan"
value="com.company.model" />
<property name="hibernateProperties">

<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.jdbc.fetch_size=50
</value>
</property>
</bean>

我的问题是考虑到整个设置,我没有看到休眠会话生命周期的任何属性设置。在休眠参考中,引入了上下文会话,据说有 CurrentSessionContext 的三个实现。

  1. JTA 2.Thread 3.Managed

我怎么知道使用了哪个实现。也许是 Spring,但我不知道。

4

2 回答 2

3

SessionFactory 由 Spring 使用给定的数据源创建,并从连接池中获取其数据库连接。我们通过 SessionFactory.getCurrentSession() 获得一个 Hibernate 会话。然后开始事务,执行工作,然后提交()或回滚(),最后关闭连接(连接对象将返回到池中)。当我们停止应用程序或关闭服务器时,休眠会话工厂将被销毁/关闭。如果你使用 HibernateTransactionManager,默认情况下,将使用 CurrentSessionContext 的线程实现。如果你想使用,jta 实现:你必须使用“JtaTransactionManager”作为事务管理器。

于 2012-05-21T07:02:15.377 回答
0

The hibernate.current_session_context_class configuration parameter defines which org.hibernate.context.CurrentSessionContext implementation should be used. For backwards compatibility, if this configuration parameter is not set but a org.hibernate.transaction.TransactionManagerLookup is configured, Hibernate will use the org.hibernate.context.JTASessionContext., which you can also configure by setting the property and using the shortform as 'jpa'.

So by default, you are using 'jpa' - you can override it by defining a property like.

<prop key="hibernate.current_session_context_class">managed</prop>

Read this for complete information on contextual sessions.

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/architecture.html#architecture-current-session

于 2012-05-22T13:07:41.723 回答