001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.hibernate;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.transaction.TransactionAttribute;
019    import com.liferay.portal.kernel.transaction.TransactionLifecycleListener;
020    import com.liferay.portal.kernel.transaction.TransactionStatus;
021    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
022    
023    import org.hibernate.Session;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class LastSessionRecorderUtil {
029    
030            public static final TransactionLifecycleListener
031                    TRANSACTION_LIFECYCLE_LISTENER = new TransactionLifecycleListener() {
032    
033                    @Override
034                    public void committed(
035                            TransactionAttribute transactionAttribute,
036                            TransactionStatus transactionStatus) {
037                    }
038    
039                    @Override
040                    public void created(
041                            TransactionAttribute transactionAttribute,
042                            TransactionStatus transactionStatus) {
043    
044                            syncLastSessionState();
045                    }
046    
047                    @Override
048                    public void rollbacked(
049                            TransactionAttribute transactionAttribute,
050                            TransactionStatus transactionStatus, Throwable throwable) {
051                    }
052    
053            };
054    
055            public static void syncLastSessionState() {
056                    Session session = _lastSessionThreadLocal.get();
057    
058                    if ((session != null) && session.isOpen()) {
059                            try {
060                                    session.flush();
061                                    session.clear();
062                            }
063                            catch (Exception e) {
064                                    throw new SystemException(e);
065                            }
066                    }
067            }
068    
069            protected static void setLastSession(Session session) {
070                    _lastSessionThreadLocal.set(session);
071            }
072    
073            private static final ThreadLocal<Session> _lastSessionThreadLocal =
074                    new AutoResetThreadLocal<Session>(
075                            LastSessionRecorderUtil.class.getName() +
076                                    "._lastSessionThreadLocal");
077    
078    }