Index: lams_common/src/java/org/lamsfoundation/lams/util/CustomizedOpenSessionInViewFilter.java =================================================================== diff -u --- lams_common/src/java/org/lamsfoundation/lams/util/CustomizedOpenSessionInViewFilter.java (revision 0) +++ lams_common/src/java/org/lamsfoundation/lams/util/CustomizedOpenSessionInViewFilter.java (revision 9ba1a07a01bccc9db69601d6ac67cedaf0cbcaa5) @@ -0,0 +1,79 @@ +/*************************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + * http://www.gnu.org/licenses/gpl.txt + * ***********************************************************************/ + +package org.lamsfoundation.lams.util; + +import net.sf.hibernate.FlushMode; +import net.sf.hibernate.Session; +import net.sf.hibernate.SessionFactory; + +import org.springframework.dao.DataAccessResourceFailureException; +import org.springframework.orm.hibernate.SessionFactoryUtils; +import org.springframework.orm.hibernate.support.OpenSessionInViewFilter; + + +/** + * + * @author Jacky Fang + * @since 2005-4-4 + * @version + * + */ +public class CustomizedOpenSessionInViewFilter extends OpenSessionInViewFilter +{ + + /** + * Get a Session for the SessionFactory that this filter uses. + * Note that this just applies in single session mode! + *

The default implementation delegates to SessionFactoryUtils' + * getSession method and sets the Session's flushMode to NEVER. + *

Can be overridden in subclasses for creating a Session with a custom + * entity interceptor or JDBC exception translator. + * @param sessionFactory the SessionFactory that this filter uses + * @return the Session to use + * @throws DataAccessResourceFailureException if the Session could not be created + * @see org.springframework.orm.hibernate.SessionFactoryUtils#getSession(SessionFactory, boolean) + * @see net.sf.hibernate.FlushMode#NEVER + */ + protected Session getSession(SessionFactory sessionFactory) + throws DataAccessResourceFailureException { + Session session = SessionFactoryUtils.getSession(sessionFactory, true); + session.setFlushMode(FlushMode.AUTO); + return session; + } + + /** + * Close the given Session. + * Note that this just applies in single session mode! + *

The default implementation delegates to SessionFactoryUtils' + * closeSessionIfNecessary method. + *

Can be overridden in subclasses, e.g. for flushing the Session before + * closing it. See class-level javadoc for a discussion of flush handling. + * Note that you should also override getSession accordingly, to set + * the flush mode to something else than NEVER. + * @param session the Session used for filtering + * @param sessionFactory the SessionFactory that this filter uses + */ + protected void closeSession(Session session, SessionFactory sessionFactory) { + SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory); + } +}