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.hibernate3.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.transaction.support.TransactionSynchronizationManager;
029
030/**
031 * Simple AOP Alliance {@link MethodInterceptor} implementation that binds a new
032 * Hibernate {@link Session} for each method invocation, if none bound before.
033 *
034 * <p>This is a simple Hibernate Session scoping interceptor along the lines of
035 * {@link OpenSessionInViewInterceptor}, just for use with AOP setup instead of
036 * MVC setup. It opens a new {@link Session} with flush mode "MANUAL" since the
037 * Session is only meant for reading, except when participating in a transaction.
038 *
039 * <p>Note: This can serve as a streamlined alternative to the outdated
040 * {@link org.springframework.orm.hibernate3.HibernateInterceptor}, providing
041 * plain Session binding without any automatic exception translation or the like.
042 *
043 * @author Juergen Hoeller
044 * @since 4.0.2
045 * @see OpenSessionInViewInterceptor
046 * @see OpenSessionInViewFilter
047 * @see org.springframework.orm.hibernate3.HibernateTransactionManager
048 * @see org.springframework.transaction.support.TransactionSynchronizationManager
049 * @see org.hibernate.SessionFactory#getCurrentSession()
050 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
051 */
052@Deprecated
053public class OpenSessionInterceptor implements MethodInterceptor, InitializingBean {
054
055        private SessionFactory sessionFactory;
056
057
058        /**
059         * Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
060         */
061        public void setSessionFactory(SessionFactory sessionFactory) {
062                this.sessionFactory = sessionFactory;
063        }
064
065        /**
066         * Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
067         */
068        public SessionFactory getSessionFactory() {
069                return this.sessionFactory;
070        }
071
072        @Override
073        public void afterPropertiesSet() {
074                if (getSessionFactory() == null) {
075                        throw new IllegalArgumentException("Property 'sessionFactory' is required");
076                }
077        }
078
079
080        @Override
081        public Object invoke(MethodInvocation invocation) throws Throwable {
082                SessionFactory sf = getSessionFactory();
083                if (!TransactionSynchronizationManager.hasResource(sf)) {
084                        // New Session to be bound for the current method's scope...
085                        Session session = openSession();
086                        try {
087                                TransactionSynchronizationManager.bindResource(sf, new org.springframework.orm.hibernate3.SessionHolder(session));
088                                return invocation.proceed();
089                        }
090                        finally {
091                                org.springframework.orm.hibernate3.SessionFactoryUtils.closeSession(session);
092                                TransactionSynchronizationManager.unbindResource(sf);
093                        }
094                }
095                else {
096                        // Pre-bound Session found -> simply proceed.
097                        return invocation.proceed();
098                }
099        }
100
101        /**
102         * Open a Session for the SessionFactory that this interceptor uses.
103         * <p>The default implementation delegates to the {@link SessionFactory#openSession}
104         * method and sets the {@link Session}'s flush mode to "MANUAL".
105         * @return the Session to use
106         * @throws DataAccessResourceFailureException if the Session could not be created
107         * @see org.hibernate.FlushMode#MANUAL
108         */
109        protected Session openSession() throws DataAccessResourceFailureException {
110                try {
111                        Session session = getSessionFactory().openSession();
112                        session.setFlushMode(FlushMode.MANUAL);
113                        return session;
114                }
115                catch (HibernateException ex) {
116                        throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
117                }
118        }
119
120}