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 ...
随机推荐
- Android Studio运行app时提示Error: Please select Android SDK
最近开启了一个android项目,点“Run”键运行app,并报错Error:Please select Android SDK: 选择 File -> Project Structure 调整 ...
- mysql--DML--视图,索引,事务,字符集,函数
Mysql中,表/列可以改名,database不能改名. phpMyAdmin似乎有这功能? 他是建新库,把所有表复制到新库,再删旧库完成的. 删除一个数据库: drop database 数据库名; ...
- Masonry scrollview循环布局
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...
- 【POJ 1947】 Rebuilding Roads
[题目链接] 点击打开链接 [算法] f[i][j]表示以i为根的子树中,最少删多少条边可以组成j个节点的子树 树上背包,即可 [代码] #include <algorithm> #inc ...
- 转3xian之所在 (一位ACM大牛的博文)
3xian的经历和见解...我深思... 最后一天,漫天飘起了雪花,假装欢送我离去. 这次WF之战不太顺利,早期的C题大概花了1秒钟构思,然而由于输出格式多了一个空格直到两个半小时才逃脱Wrong A ...
- 利用Redis Sentinel实现redis主从自动切换
redis主从配置很简单,只需要在slave的配置里加slaveof 192.168.0.100 6379(master的ip和端口) 如果master有密码再设置 masterauth passwo ...
- c语言和c++栈的简单实现以及构造器的原理
也就是训练将原来的c语言 用类表示出来.. 关于构造器: //1与类名相同 没有返回值 被系统生成对象时自动调用,用于初始化 //2 可以有参数 构造器重载 默认参数 //3 重载和默认不能同时存在, ...
- 我理解的 js 异步成长总结
本文是自己的理解,如果有错误的地方,还请各路大神指出 首先说下我最常用的 Promise getHandlePickupQrPromise() { // 定义返回 Promise对象 // Promi ...
- 微信支付接口调用问题(android正常,iphone调不起)
转自:http://blog.csdn.net/tt123123/article/details/53897035 碰到的问题 :根据微信提供的示例代码(ASP.NET),配置好一切后, 用andro ...
- 洛谷 P1966 火柴排队
题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为:∑(ai−bi)2 其中ai 表示 ...