春天如何管理hibernate会话生命周期 [英] how spring manage hibernate session lifecycle

查看:131
本文介绍了春天如何管理hibernate会话生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



transactionManager设置如下:

 < bean id = transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager> 
< property name =sessionFactoryref =sessionFactory/>
< / bean>

sessionFactory设置如下:

 < bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean> 
< property name =dataSourceref =dataSource/>
< property name =packagesToScan
value =com.company.model/>
< property name =hibernateProperties>

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

我的quesiton被认为是整个设置,我没有看到hibernate会话生命周期的任何属性设置。在hibernate参考中,引入了Contextual会话,并且声称有三个CurrentSessionContext实现。


  1. JTA 2.Thread 3.Managed

我怎么知道哪个实现已经被使用,可能是Spring,但我不知道。
< SessionFactory由Spring使用给定的dataSource创建,并从连接池中获取数据库连接。我们通过SessionFactory.getCurrentSession()获得一个Hibernate会话。然后开始事务,完成工作,然后commit()或rollback(),并在最后关闭连接(连接对象将返回到池)。当我们停止应用程序或关闭服务器时,Hibernate会话工厂将被销毁/关闭。并且默认情况下,如果使用HibernateTransactionManager,将使用CurrentSessionContext的Thread实现。如果您想使用jta实现:您必须使用JtaTransactionManager作为事务管理器。


Spring is used in our team's Java EE project, and hibernate is used for underlying ORM.

transactionManager is set like below:

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

sessionFactory is set like below:

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

my quesiton is thoughtout the whole setting, I didn't see any property setting for hibernate session lifecycle.In hibernate reference, Contextual sessions was introduced and it's said that there three implementations of CurrentSessionContext.

  1. JTA 2.Thread 3.Managed

How do I know which implementation has been used.Maybe by Spring, but I've no idea.

解决方案

The SessionFactory is created by Spring using given dataSource and is taking its DB connections from connection pool. We get a Hibernate session via SessionFactory.getCurrentSession(). then start transaction, do the work and then commit() or rollback(), and at the end close the connection(connection object will be returned to the pool). Hibernate session factory will be destroyed/closed when we either stop application or shutdown the server. And By default, Thread implementations of CurrentSessionContext will be used, if you use HibernateTransactionManager. If you want to use, jta implementation: you have to use "JtaTransactionManager" as a Transaction manager.

这篇关于春天如何管理hibernate会话生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆