org.springframework.orm.hibernate4.support.OpenSessionInterceptor
2015-07-01 21:04
274 查看
/* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.orm.hibernate4.support; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.hibernate.FlushMode; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.orm.hibernate4.SessionFactoryUtils; import org.springframework.orm.hibernate4.SessionHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; /** * Simple AOP Alliance {@link MethodInterceptor} implementation that binds a new * Hibernate {@link Session} for each method invocation, if none bound before. *简单的aop 绑定,实现绑定一个新的hibernate对象,如果没有绑定,则绑定每一个hibernate方法。 * <p>This is a simple Hibernate Session scoping interceptor along the lines of 这是一个简单的hibernae session 范围之上的拦截器 * {@link OpenSessionInViewInterceptor}, just for use with AOP setup instead of 只是以 AOP 设置代替 MVC 设置。 * MVC setup. It opens a new {@link Session} with flush mode "MANUAL" since the 它 打开 一个 新的 flush mode="manual" ,因为这个是只读的, * Session is only meant for reading, except when participating in a transaction. * 除非加入了事务。 * @author Juergen Hoeller * @since 4.0.2 * @see OpenSessionInViewInterceptor * @see OpenSessionInViewFilter * @see org.springframework.orm.hibernate4.HibernateTransactionManager * @see org.springframework.transaction.support.TransactionSynchronizationManager * @see org.hibernate.SessionFactory#getCurrentSession() */ public class OpenSessionInterceptor implements MethodInterceptor, InitializingBean { private SessionFactory sessionFactory; /** * Set the Hibernate SessionFactory that should be used to create Hibernate Sessions. */ public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * Return the Hibernate SessionFactory that should be used to create Hibernate Sessions. */ public SessionFactory getSessionFactory() { return this.sessionFactory; } @Override public void afterPropertiesSet() { if (getSessionFactory() == null) { throw new IllegalArgumentException("Property 'sessionFactory' is required"); } } @Override public Object invoke(MethodInvocation invocation) throws Throwable { SessionFactory sf = getSessionFactory(); //检测当前的SessionFactory是否绑定到当前现场中。 if (!TransactionSynchronizationManager.hasResource(sf)) { // New Session to be bound for the current method's scope... //通过后面的openSession,获得当前现场绑定的sessionFactory并打开session,然后将其绑定到当前方法中。(暂时不说太理解这个?) Session session = openSession(); try { //将SessionFactory和session绑定到当前线程。(奇怪,上面的if不是已经检测到sessionFactory绑定到当前的thread了吗,) TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); return invocation.proceed(); //继续进行下一个拦截器 } finally { SessionFactoryUtils.closeSession(session); TransactionSynchronizationManager.unbindResource(sf); //将sf从当前thread解绑。 } } else { // Pre-bound Session found -> simply proceed. return invocation.proceed(); } } /** * Open a Session for the SessionFactory that this interceptor uses. * <p>The default implementation delegates to the {@link SessionFactory#openSession} * method and sets the {@link Session}'s flush mode to "MANUAL". * @return the Session to use * @throws DataAccessResourceFailureException if the Session could not be created * @see org.hibernate.FlushMode#MANUAL */ protected Session openSession() throws DataAccessResourceFailureException { try { Session session = getSessionFactory().openSession(); session.setFlushMode(FlushMode.MANUAL); return session; } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); } } }
相关文章推荐
- javaSE学习笔记之控制语句
- 如何更改Java括号中的默认对齐
- 用java实现大文件分割、排序、合并
- 学习笔记_Java_day13_JSP三大指令()
- javaSE学习笔记之运算符
- 《Java程序实习》日记(周三)
- Java之旅(2)—反射
- java自己主动打开包装盒很容易导致两个误区
- (JAVA)找不到或无法加载主类 com.sun.tools.javac.main
- git基础
- java中四种引用类型
- LeetCode229 MajorityElementII java题解
- Java基础知识整理(三)
- java文本相似度计算
- 黑马程序员——高新技术---枚举及部分JDK1.5新特性
- 《Java程序设计》第17周课程设计:《猜猜看》游戏 第三天
- Java ClassLoader个人见解
- 将数据导出到Excel(java操作Excel)
- java.io.NotSerializableException错误解决方法
- Java 生产者消费者并发实例