001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.orm.hibernate4.support; 018 019import org.aopalliance.intercept.MethodInterceptor; 020import org.aopalliance.intercept.MethodInvocation; 021import org.hibernate.FlushMode; 022import org.hibernate.HibernateException; 023import org.hibernate.Session; 024import org.hibernate.SessionFactory; 025 026import org.springframework.beans.factory.InitializingBean; 027import org.springframework.dao.DataAccessResourceFailureException; 028import org.springframework.orm.hibernate4.SessionFactoryUtils; 029import org.springframework.orm.hibernate4.SessionHolder; 030import org.springframework.transaction.support.TransactionSynchronizationManager; 031 032/** 033 * Simple AOP Alliance {@link MethodInterceptor} implementation that binds a new 034 * Hibernate {@link Session} for each method invocation, if none bound before. 035 * 036 * <p>This is a simple Hibernate Session scoping interceptor along the lines of 037 * {@link OpenSessionInViewInterceptor}, just for use with AOP setup instead of 038 * MVC setup. It opens a new {@link Session} with flush mode "MANUAL" since the 039 * Session is only meant for reading, except when participating in a transaction. 040 * 041 * @author Juergen Hoeller 042 * @since 4.0.2 043 * @see OpenSessionInViewInterceptor 044 * @see OpenSessionInViewFilter 045 * @see org.springframework.orm.hibernate4.HibernateTransactionManager 046 * @see org.springframework.transaction.support.TransactionSynchronizationManager 047 * @see org.hibernate.SessionFactory#getCurrentSession() 048 */ 049public class OpenSessionInterceptor implements MethodInterceptor, InitializingBean { 050 051 private SessionFactory sessionFactory; 052 053 054 /** 055 * Set the Hibernate SessionFactory that should be used to create Hibernate Sessions. 056 */ 057 public void setSessionFactory(SessionFactory sessionFactory) { 058 this.sessionFactory = sessionFactory; 059 } 060 061 /** 062 * Return the Hibernate SessionFactory that should be used to create Hibernate Sessions. 063 */ 064 public SessionFactory getSessionFactory() { 065 return this.sessionFactory; 066 } 067 068 @Override 069 public void afterPropertiesSet() { 070 if (getSessionFactory() == null) { 071 throw new IllegalArgumentException("Property 'sessionFactory' is required"); 072 } 073 } 074 075 076 @Override 077 public Object invoke(MethodInvocation invocation) throws Throwable { 078 SessionFactory sf = getSessionFactory(); 079 if (!TransactionSynchronizationManager.hasResource(sf)) { 080 // New Session to be bound for the current method's scope... 081 Session session = openSession(); 082 try { 083 TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session)); 084 return invocation.proceed(); 085 } 086 finally { 087 SessionFactoryUtils.closeSession(session); 088 TransactionSynchronizationManager.unbindResource(sf); 089 } 090 } 091 else { 092 // Pre-bound Session found -> simply proceed. 093 return invocation.proceed(); 094 } 095 } 096 097 /** 098 * Open a Session for the SessionFactory that this interceptor uses. 099 * <p>The default implementation delegates to the {@link SessionFactory#openSession} 100 * method and sets the {@link Session}'s flush mode to "MANUAL". 101 * @return the Session to use 102 * @throws DataAccessResourceFailureException if the Session could not be created 103 * @see org.hibernate.FlushMode#MANUAL 104 */ 105 protected Session openSession() throws DataAccessResourceFailureException { 106 try { 107 Session session = getSessionFactory().openSession(); 108 session.setFlushMode(FlushMode.MANUAL); 109 return session; 110 } 111 catch (HibernateException ex) { 112 throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex); 113 } 114 } 115 116}