org.springframework.orm.hibernate4.support.OpenSessionInterceptor
/*
* 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的更多相关文章
- org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
---恢复内容开始--- /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache ...
- ssh中org.springframework.orm.hibernate4.support.OpenSessionInViewFilter的作用及配置
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 是Spring为我们解决Hibernate的Session的关闭 ...
- Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.HibernateTemplate
1.错误描述 严重: Context initialization failed org.springframework.beans.factory.CannotLoadBeanClassExcept ...
- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter作用
在Spring与Hibernate集成时在web.xml要加入这样的过滤器: <filter> <filter-name>openSessionInView</filte ...
- hibernate4 , spring3 使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 报错 Implementing class
错误代码如下 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with ...
- org.springframework.orm.hibernate3.HibernateSystemException:
org.springframework.orm.hibernate3.HibernateSystemException: The database returned no natively gener ...
- java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryBean
Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.LocalSessionFactoryB ...
- Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider停住了
2015.1.24进行了服务器的搬家,搬家后,更换了新的IP,导致新的IP访问以前IP的数据库服务无法成功Initializing connection provider: org.springfra ...
- org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑解决办法
在项目中使用了SSH框架(Struts2 + Spring3+ Hibernate3),applicationContext中配置了sessionFactory <bean id="s ...
随机推荐
- RK3288以太网的mac地址调试笔记【学习笔记】【原创】
平台信息:内核:linux3.1.0系统:android/android6.0平台:RK3288 作者:庄泽彬(欢迎转载,请注明作者) 邮箱:2760715357@qq.com 说明:提供以太网mac ...
- string operation in powershell
https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/15/keep-your-hands-clean-use-powershell- ...
- oracle分区表有什么作用
oracle分区表有什么作用 https://zhidao.baidu.com/question/1818955865408544348.html (1) 表空间及分区表的概念 表空间: 是一个或多个 ...
- P4284 [SHOI2014]概率充电器 dp
这个题题干说的不清楚,一开始我以为只能是旁边紧挨着的传火,导致我一开始根本不知道哪错了.后来,我想到树形dp,但是需要正反考虑,()既要考虑父亲,又要考虑儿子),互相都有影响,所以没太想出来.后来知道 ...
- artemplate include
include用于嵌入字模板 {{include 'template_name'}} 子模板 默认共享当前的数据 也可以自己指定数据 {{include 'template_name' templat ...
- Tomcat闪退的解决办法
1 首先考虑的是端口占用 2 现在讲的是这一种方法 (1)找到解压缩的文件的bin目录 找到startup.bat 记事本打开 开头加上两句话 SET JAVA_HOME=C:\Program Fil ...
- bzoj 3751: [NOIP2014]解方程【数学】
--我真是太非了,自己搞了7个质数都WA,从别人那粘5个质数就A了-- 就是直接枚举解,用裴蜀定理计算是否符合要求,因为这里显然结果很大,所以我们对多个质数取模看最后是不是都为0 #include&l ...
- jquery的validate的用法
//引入js文件 //jquery 文件 <script src="__PUBLIC__/static/wap/js/jquery.min.js?v=2.1.4">&l ...
- SQL Server OPENQUERY使用
以下以创建好的ORAC链接服务器为例: A. 执行 SELECT 传递查询 SELECT * FROM OPENQUERY(ORAC, 'SELECT ID,NAME FROM SCOTT.RYB') ...
- 标准字符cp功能
#include<stdio.h> #include<fcntl.h> int main(int argc,char *argv[]) { FILE *src_fp,*des_ ...