001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.spring.hibernate;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
019    
020    import org.hibernate.Session;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class LastSessionRecorderUtil {
026    
027            public static void syncLastSessionState() throws SystemException {
028                    Session session = _lastSessionThreadLocal.get();
029    
030                    if ((session != null) && session.isOpen()) {
031                            try {
032                                    session.flush();
033                                    session.clear();
034                            }
035                            catch (Exception e) {
036                                    throw new SystemException(e);
037                            }
038                    }
039            }
040    
041            protected static void setLastSession(Session session) {
042                    _lastSessionThreadLocal.set(session);
043            }
044    
045            private static ThreadLocal<Session> _lastSessionThreadLocal =
046                    new AutoResetThreadLocal<Session>(
047                            LastSessionRecorderUtil.class.getName() +
048                                    "._lastSessionThreadLocal");
049    
050    }