Web项目中定时任务无法绑定SessionFactory的问题解决
Open Session In View
- <filter>
- <filter-name>openSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>openSessionInViewFilter</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- @Component
- @Aspect
- public class OpenSessionInCronInterceptor {
- private static final Logger logger = Logger.getLogger(OpenSessionInCronInterceptor.class.getName());
- @Autowired
- private SessionFactory sessionFactory;
- @Pointcut("execution(void com.ejiapei.service.cron.*.work())")
- protected void definePointcut() {
- }
- @Before("definePointcut()")
- public void preHandle() throws DataAccessException {
- logger.info("Opening Hibernate Session in OpenSessionInViewInterceptor");
- Session session = openSession();
- TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
- }
- /**
- * Unbind the Hibernate <code>Session</code> from the thread and close it).
- *
- * @see org.springframework.transaction.support.TransactionSynchronizationManager
- */
- @After("definePointcut()")
- public void afterCompletion() throws DataAccessException {
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
- logger.info("Closing Hibernate Session in OpenSessionInViewInterceptor");
- SessionFactoryUtils.closeSession(sessionHolder.getSession());
- }
- /**
- * Open a Session for the SessionFactory that this interceptor uses.
- * <p>The default implementation delegates to the
- * <code>SessionFactory.openSession</code> method and
- * sets the <code>Session</code>'s flush mode to "MANUAL".
- *
- * @return the Session to use
- * @throws org.springframework.dao.DataAccessResourceFailureException if the Session could not be created
- * @see org.hibernate.FlushMode#MANUAL
- */
- protected Session openSession() throws DataAccessResourceFailureException {
- try {
- Session session = SessionFactoryUtils.openSession(sessionFactory);
- session.setFlushMode(FlushMode.MANUAL);
- return session;
- } catch (HibernateException ex) {
- throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
- }
- }
- }
Web项目中定时任务无法绑定SessionFactory的问题解决的更多相关文章
- 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入
在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...
- 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能
在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能. 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先 ...
- web项目中配置多个数据源
web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源, ...
- spring web项目中整合netty, akka
spring web项目中整合netty, akka 本身的web项目仍然使用tomcat/jetty8080端口, 在org.springframework.beans.factory.Initia ...
- JAVA WEB项目中各种路径的获取
JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报 分类: JAVA开发(41) 1.可以在s ...
- Web 项目中分享到微博、QQ空间等分享功能
Web 项目中分享到微博.QQ空间等分享功能 网上有很多的模板以及代码,但是有很多都不能分享内容,简单的测试了下: 以新浪微博为例,文本框中的内容是title属性,下面的链接是url属性,如果你的链接 ...
- java web项目中 获取resource路径下的文件路径
public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...
- web项目中加入struts2、spring的支持,并整合两者
Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
随机推荐
- DB2导入导出数据
1.导出表数据到txt文件: export to /brcb_edp/data_public_edp/file/CCDM/file/FILE_CCDM_DR_CARD_CUST_DET.txt of ...
- Python基础学习----元组
# 元组和列表的区别: # 相同点: # 1.表示多个元素的序列 # 2.数据之间用 , 分开 # 3.索引都是[0]开始 # # 不同点: # 1.列表创建是[],元组是()表示 # 2.元组元素一 ...
- 各个数据库中top 的表示方法
Select Top在不同数据库中的使用用法: 1. Oracle数据库 SELECT * FROM TABLE1 WHERE ROWNUM<=N 2. Infomix数据库 SELECT FI ...
- @Qualifier注解详解
@Qualifier注解意味着可以在被标注bean的字段上可以自动装配.Qualifier注解可以用来取消Spring不能取消的bean应用. 下面的示例将会在Customer的person属性中自动 ...
- OpenGL实现3D自由变形
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- python常用模块之xml模块
python常用模块之xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,在json还没诞生的年代,大家都是使用xml,目前很多传统公司的系 ...
- 自己手写一个SpringMVC框架
前端框架很多,但没有一个框架称霸,后端框架现在Spring已经完成大一统.所以学习Spring是Java程序员的必修课. Spring框架对于Java后端程序员来说再熟悉不过了,以前只知道它用的反射实 ...
- xshell连接及优化
xshell 连接 问题:当你的xshell与服务器连接不上时 1:可先排查道路通不通 正常: 不正常: 1:查看服务器ip正不正确 2:vmware 编辑-虚拟网络编辑器 3:windows服务 ...
- (效果四)jst如何判断对象是否为空?
前言:在实现业务逻辑的过程中,很多工程师都会遇到需要判断一个对象,数组是否为空的情景,很多时候我们在请求数据的时候都需要判断请求的对象数据是否为空,如果直接使用,在数据请求为空时,控制台就会报错.因此 ...
- ranch分析学习(三)
接着上一篇继续研究 上一篇结尾的时候,我们谈到了连接,监听两个监督树,今天我们就来看看这两个监督树和他们的工作者都是干什么的,怎么实现的.文件编号接上篇. 6. ranch_acceptors_sup ...