主题:解惑:在spring+hibernate中,只读事务是如何被优化的。
出自:http://www.iteye.com/topic/95124
**
*作者:张荣华(ahuaxuan)
*2007-06-28
*转载请注明出处及作者
*/
- publicvoid?commit()? throws ?HibernateException?{ ?? ??????? if ?(!begun)?{ ??
- ???????????thrownew?TransactionException( "Transaction?not?successfully?started" ); ?? ???????} ??
- ? ?? ???????log.debug( "commit" ); ??
- ? ?? ??????? if ?(?!transactionContext.isFlushModeNever()?&&?callback?)?{ ??
- ???????????transactionContext.managedFlush();? //if?an?exception?occurs?during?flush,?user?must?call?rollback() ?? ???????} ??
- //也就是在这里会判断是否需要刷新一级缓存中的持久对象,如果session的flushmode不为//never而且需要回调的话,那么就刷新一级缓存中的持久对象,向数据库发送sql语句 ?? ? ??
- ???????beforeTransactionCompletion(); ?? ??????? if ?(?callback?)?{ ??
- ???????????jdbcContext.beforeTransactionCompletion(? this ?); ?? ???????} ??
- ? ?? ??????? try ?{ ??
- ???????????commitAndResetAutoCommit(); ?? //提交事务,并且把事务的commit方式设置为auto,是不是和spring在事务开始和事务结束//时设置session的flush?mode的方式是一样的呀。 ??
- ???????????log.debug( "committed?JDBC?Connection" ); ?? ???????????committed?=? true ; ??
- ??????????? if ?(?callback?)?{ ?? ??????????????jdbcContext.afterTransactionCompletion(? true ,? this ?); ??
- ???????????} ?? ???????????afterTransactionCompletion(?Status.STATUS_COMMITTED?); ??
- ???????} ?? ??????? catch ?(SQLException?e)?{ ??
- ???????????log.error( "JDBC?commit?failed" ,?e); ?? ???????????commitFailed?=? true ; ??
- ??????????? if ?(?callback?)?{ ?? ??????????????jdbcContext.afterTransactionCompletion(? false ,? this ?); ??
- ???????????} ?? ???????????afterTransactionCompletion(?Status.STATUS_UNKNOWN?); ??
- ???????????thrownew?TransactionException( "JDBC?commit?failed" ,?e); ?? ???????} ??
- ??????? finally ?{ ?? ???????????closeIfRequired(); ??
- ???????} ?? ????} ??
- public ? void ?managedFlush()?{ ?? ???????? if ?(?isClosed()?)?{ ??
- ????????????log.trace(? "skipping?auto-flush?due?to?session?closed" ?); ?? ???????????? return ; ??
- ????????} ?? ????????log.trace( "automatically?flushing?session" ); ??
- ????????flush(); ?? ???????? //刷新这个session实例的一级缓存。 ??
- ???????? if ?(?childSessionsByEntityMode?!=? null ?)?{ ?? ????????????Iterator?iter?=?childSessionsByEntityMode.values().iterator(); ??
- ???????????? while ?(?iter.hasNext()?)?{ ?? ????????????????(?(Session)?iter.next()?).flush(); ??
- ????????????} ?? ????????} //刷新该session的子session的一级缓存。 ??
- ????} ??
- public ? void ?onFlush(FlushEvent?event)? throws ?HibernateException?{ ?? ???????? final ?EventSource?source?=?event.getSession(); ??
- ???????? if ?(?source.getPersistenceContext().hasNonReadOnlyEntities()?)?{ ?? ???????????? ??
- ????????????flushEverythingToExecutions(event); ?? //这个方法是flush前的准备工作,它把需要被flush的实体,集合,等等放到需要被flush ??
- //的一个队列中 ?? ????????????performExecutions(source); ??
- //这个方法是最重要的,因为在这里才是真正的执行sql语句,并且负责更新二级缓存(如果你//配置了二级缓存的话) ?? ????????????postFlush(source); ??
- //负责flush后的善后工作,比如说一个对象不再被另外一个对象关联了,那么就把这个对象//从一级缓存重剔除,等等。 ?? ???????????? if ?(?source.getFactory().getStatistics().isStatisticsEnabled()?)?{ ??
- ????????????????source.getFactory().getStatisticsImplementor().flush(); ?? ????????????} ??
- ???????????? ?? ????????} ??
- ????} ??