读书人

HibernateInterceptor毕竟起到甚么作用

发布时间: 2012-11-12 12:31:58 作者: rapoo

HibernateInterceptor到底起到甚么作用?

<bean id="classDao" name="code"><bean id="classDao" name="code">public Object invoke(MethodInvocation methodInvocation) throws Throwable {boolean existingTransaction = false;Session session = SessionFactoryUtils.getSession(getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {logger.debug("Found thread-bound Session for Hibernate interceptor");existingTransaction = true;}else {logger.debug("Using new Session for Hibernate interceptor");if (getFlushMode() == FLUSH_NEVER) {session.setFlushMode(FlushMode.NEVER);}TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));}try {enableFilters(session);Object retVal = methodInvocation.proceed();flushIfNecessary(session, existingTransaction);return retVal;}catch (HibernateException ex) {throw convertHibernateAccessException(ex);}finally {disableFilters(session);if (existingTransaction) {logger.debug("Not closing pre-bound Hibernate Session after interceptor");}else {TransactionSynchronizationManager.unbindResource(getSessionFactory());SessionFactoryUtils.releaseSession(session, getSessionFactory());}}}

读书人网 >软件架构设计

热点推荐