Spring Hibernate整合难点


public User saveAndFindAll(){
User user = new User();
user.setUserName("chenglong");
user.setNickName("huanhuan");
user.setLoginTimes(5);
user.setPhone("13261165539");
user.setRegTime(new Date());

userDao.save(user); //插入
return userDao.findAll(User.class);//查询
}


[size=medium][b]如果没有配置事务,日志是这样:(注意加粗处session的开关以及session.flush()的时机)[/b][/size]

2010-11-11 14:29:59,855 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - [b]Opening Hibernate Session[/b]
Hibernate: insert into User (userName, nickName, regTime, loginTimes, phone, lastLogin) values (?, ?, ?, ?, ?, ?)
2010-11-11 14:29:59,925 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - [b]Eagerly flushing Hibernate session[/b]
2010-11-11 14:29:59,930 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - [b]Closing Hibernate Session[/b]
2010-11-11 14:29:59,930 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - [b]Opening Hibernate Session[/b]
Hibernate: select this_.userId as userId0_0_, this_.userName as userName0_0_, this_.nickName as nickName0_0_, this_.regTime as regTime0_0_, this_.loginTimes as loginTimes0_0_, this_.phone as phone0_0_, this_.lastLogin as lastLogin0_0_ from User this_
2010-11-11 14:29:59,955 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - [b]Eagerly flushing Hibernate session[/b]
Hibernate: update User set userName=?, nickName=?, regTime=?, loginTimes=?, phone=?, lastLogin=? where userId=?
Hibernate: update User set userName=?, nickName=?, regTime=?, loginTimes=?, phone=?, lastLogin=? where userId=?
2010-11-11 14:29:59,980 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - [b]Closing Hibernate Session[/b]

[size=medium][b]如果配置了事务,日志是这样:(注意加粗处session的处理,以及事务的处理)[/b][/size]
2010-11-11 14:19:01,645 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - [b]Initializing transaction synchronization[/b]
2010-11-11 14:19:01,645 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor] -Getting transaction for
[com.supben.service.UserService.saveAndFindAll]
2010-11-11 14:19:01,650 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@89e2f1] for key [org.hibernate.impl.SessionFactoryImpl@68cd79] [b]bound to thread [/b][main]
2010-11-11 14:19:01,650 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@89e2f1] for key [org.hibernate.impl.SessionFactoryImpl@68cd79] bound to thread [main]
2010-11-11 14:19:01,650 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] -[b] Found thread-bound Session for HibernateTemplate[/b]
Hibernate: insert into User (userName, nickName, regTime, loginTimes, phone, lastLogin) values (?, ?, ?, ?, ?, ?)
2010-11-11 14:19:01,680 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - [b]Not closing pre-bound Hibernate Session after HibernateTemplate[/b]
2010-11-11 14:19:01,685 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@89e2f1] for key [org.hibernate.impl.SessionFactoryImpl@68cd79] [b]bound to thread [/b][main]
2010-11-11 14:19:01,685 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@89e2f1] for key [org.hibernate.impl.SessionFactoryImpl@68cd79] bound to thread [main]
2010-11-11 14:19:01,685 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - [b]Found thread-bound Session for HibernateTemplate[/b]
2010-11-11 14:19:01,690 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@89e2f1] for key [org.hibernate.impl.SessionFactoryImpl@68cd79] [b]bound to thread [/b][main]
Hibernate: select this_.userId as userId0_0_, this_.userName as userName0_0_, this_.nickName as nickName0_0_, this_.regTime as regTime0_0_, this_.loginTimes as loginTimes0_0_, this_.phone as phone0_0_, this_.lastLogin as lastLogin0_0_ from User this_
2010-11-11 14:19:01,715 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - [b]Not closing pre-bound Hibernate Session after HibernateTemplate[/b]
2010-11-11 14:19:01,715 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor] - Completing transaction for [com.supben.service.UserService.saveAndFindAll]
2010-11-11 14:19:01,715 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Triggering beforeCommit synchronization
2010-11-11 14:19:01,715 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Triggering beforeCompletion synchronization
2010-11-11 14:19:01,715 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Initiating transaction commit
2010-11-11 14:19:01,715 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - [b]Committing Hibernate transaction on Session [/b][org.hibernate.impl.SessionImpl@b86944]
Hibernate: update User set userName=?, nickName=?, regTime=?, loginTimes=?, phone=?, lastLogin=? where userId=?
2010-11-11 14:19:01,745 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Triggering afterCommit synchronization
2010-11-11 14:19:01,745 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Triggering afterCompletion synchronization
2010-11-11 14:19:01,750 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Clearing transaction synchronization
2010-11-11 14:19:01,750 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [org.springframework.orm.hibernate3.SessionHolder@89e2f1] for key [org.hibernate.impl.SessionFactoryImpl@68cd79] from thread [main]
2010-11-11 14:19:01,750 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@1117a20] for key [org.apache.commons.dbcp.BasicDataSource@1192059] from thread [main]
2010-11-11 14:19:01,750 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - Closing Hibernate Session [org.hibernate.impl.SessionImpl@b86944] after transaction
2010-11-11 14:19:01,750 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - [b]Closing Hibernate Session[/b]

很容易看到:如果没有配置事务,hibernateDaoSupport采用的是传统的open-per-operation模式。即:打开一个session----执行添加----从内存flush到数据库中----
关闭session----打开一个session----执行查询----flush到数据库中(2条update操作是flush的一部分...)----关闭session

如果配置了,操作过程则是:打开事务----开启一个session并绑定到当前线程中,执行添加----检查是否要关闭session(否)----拿到当前线程的session----执行查询----检查session是否要关闭(是)----提交事务----关闭session。

显然,后者的session管理更科学了,看起来save之后没有调用flush,好像保证不了save之后数据的正确性。其实不是!因为他们是同一个session中,所以仍然不影响结果!

[size=medium]
[b]
[color=red]
结论:无论数据库支不支持事务,或者说业务需不需要。都用aop的方式对service方法配上事务管理!
[/color]
这时候,spring会完全负责hibernateDaoSupport中的getSession(),getCurrentSession()以及getHibernateTemplate()三个方法中的session开关。即spring能保证在一个service方法中中只出现一个 hibernate session。在第一次发sql前打开,在最后一次发sql后关闭。

严禁用SessionFactory的openSession()自己获取一个session。这样得到的session无法控制的。

至于openSessionInView。整个request--response的生命周期,一直用一个session,好像没什么很强的必要性,而且带来的负面问题极多,所以不建议使用。
[/b][/size]


附上:事务的声明式配置方法



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

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="remove*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.supben.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值