/*
* 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);
}
} }

org.springframework.orm.hibernate4.support.OpenSessionInterceptor的更多相关文章

  1. org.springframework.orm.hibernate4.support.OpenSessionInViewFilter

    ---恢复内容开始--- /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache ...

  2. ssh中org.springframework.orm.hibernate4.support.OpenSessionInViewFilter的作用及配置

     org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 是Spring为我们解决Hibernate的Session的关闭 ...

  3. Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.HibernateTemplate

    1.错误描述 严重: Context initialization failed org.springframework.beans.factory.CannotLoadBeanClassExcept ...

  4. org.springframework.orm.hibernate3.support.OpenSessionInViewFilter作用

    在Spring与Hibernate集成时在web.xml要加入这样的过滤器: <filter> <filter-name>openSessionInView</filte ...

  5. hibernate4 , spring3 使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 报错 Implementing class

    错误代码如下 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with ...

  6. org.springframework.orm.hibernate3.HibernateSystemException:

    org.springframework.orm.hibernate3.HibernateSystemException: The database returned no natively gener ...

  7. java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryBean

    Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryB ...

  8. Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider停住了

    2015.1.24进行了服务器的搬家,搬家后,更换了新的IP,导致新的IP访问以前IP的数据库服务无法成功Initializing connection provider: org.springfra ...

  9. org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑解决办法

    在项目中使用了SSH框架(Struts2 + Spring3+ Hibernate3),applicationContext中配置了sessionFactory <bean id="s ...

随机推荐

  1. (21) java web的struts2框架的使用

    在javaweb开发过程中,如果只使用servlet,jdbc,jsp进行开发,也可以遵从MVC的模式,这时候,servlet相当于control层,属于负责处理业务逻辑的控制器,同时也需要对获取和返 ...

  2. javascript 二级联动

    <html> <head> <title></title> <meta http-equiv="Content-Type" c ...

  3. 并不对劲的p2664树上游戏

    题目大意 有一棵\(n\)(\(n\leq10^5\))个点的树,每个点\(i\)有颜色\(c_i\)(\(c_i\leq10^5\)) 定义一条路径的得分为这条路径上的不同颜色个数 分别求每个点的以 ...

  4. 常用的 Nand Flash 指令如下:

    指令 功能 nand info 显示可使用的 Nand Flash nand device [dev] 显示或设定当前使用的 Nand Flash nand read  addr off  size ...

  5. shell 与 空格

    shell脚本中,不能随意添加空格,否则出错: 1,=等号两边必须无空格.否则出错.如i =$1和i= $1都是错的.但是在()内部不限制如for ((i= 1;i < 3;i= i+1))是正 ...

  6. hdu4352(数位DP + LIS(nlogn))

    题目描述: 给定一个区间中,将区间的每一个数看成一个字符串,求这个区间内每个字符串的最大上升 子序列等于k的个数. 可以采用nlogn的LIS(用一个C数组记录长度为i的最大上升子序列的结尾最小值), ...

  7. 数据库无法访问,用户 NT AUTHORITY/SYSTEM或NT AUTHORITY\NETWORK SERVICE登录失败的解决办法

    问题:win7中的在IIS 7.0中,在 Default Web Site 目录下挂一虚拟目录. 在相应的应用程序池 DefaultAppPool 设置标识设置成NetworkService. 但是打 ...

  8. 【转载】CAS操作

    [本文转载]http://blog.csdn.net/hsuxu/article/details/9467651 CAS CAS:Compare and Swap, 翻译成比较并交换. java.ut ...

  9. markdown(语法)入门学习:

    原文链接:https://segmentfault.com/a/1190000010223222#articleHeader9 1.标题 注:#后面保持空格 # h1 ## h2 ### h3 ### ...

  10. ThinkPHP3.2.3学习笔记1---控制器

    ThinkPHP是为了简化企业级应用开发和敏捷WEB应用开发而诞生的.最早诞生于2006年初,2007年元旦正式更名为ThinkPHP,并且遵循Apache2开源协议发布.ThinkPHP从诞生以来一 ...