1   /*
2    * Hibernate, Relational Persistence for Idiomatic Java
3    *
4    * Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
5    * indicated by the @author tags or express copyright attribution
6    * statements applied by the authors.  All third-party contributions are
7    * distributed under license by Red Hat Inc.
8    *
9    * This copyrighted material is made available to anyone wishing to use, modify,
10   * copy, or redistribute it subject to the terms and conditions of the GNU
11   * Lesser General Public License, as published by the Free Software Foundation.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15   * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16   * for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with this distribution; if not, write to:
20   * Free Software Foundation, Inc.
21   * 51 Franklin Street, Fifth Floor
22   * Boston, MA  02110-1301  USA
23   */
24  package org.hibernate.context.internal;
25  
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import javax.transaction.Synchronization;
30  import javax.transaction.Transaction;
31  import javax.transaction.TransactionManager;
32  
33  import org.jboss.logging.Logger;
34  
35  import org.hibernate.ConnectionReleaseMode;
36  import org.hibernate.HibernateException;
37  import org.hibernate.Session;
38  import org.hibernate.context.spi.AbstractCurrentSessionContext;
39  import org.hibernate.context.spi.CurrentSessionContext;
40  import org.hibernate.engine.spi.SessionFactoryImplementor;
41  import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
42  import org.hibernate.internal.CoreMessageLogger;
43  import org.hibernate.service.jta.platform.spi.JtaPlatform;
44  
45  /**
46   * An implementation of {@link CurrentSessionContext} which scopes the notion
47   * of a current session to a JTA transaction.  Because JTA gives us a nice
48   * tie-in to clean up after ourselves, this implementation will generate
49   * Sessions as needed provided a JTA transaction is in effect.  If a session
50   * is not already associated with the current JTA transaction at the time
51   * {@link #currentSession()} is called, a new session will be opened and it
52   * will be associated with that JTA transaction.
53   * <p/>
54   * Note that the sessions returned from this method are automatically configured with
55   * both the {@link org.hibernate.cfg.Environment#FLUSH_BEFORE_COMPLETION auto-flush} and
56   * {@link org.hibernate.cfg.Environment#AUTO_CLOSE_SESSION auto-close} attributes set to
57   * true, meaning that the Session will be automatically flushed and closed
58   * as part of the lifecycle for the JTA transaction to which it is associated.
59   * Additionally, it will also be configured to aggressively release JDBC
60   * connections after each statement is executed.  These settings are governed
61   * by the {@link #isAutoFlushEnabled()}, {@link #isAutoCloseEnabled()}, and
62   * {@link #getConnectionReleaseMode()} methods; these are provided (along with
63   * the {@link #buildOrObtainSession()} method) for easier subclassing for custom
64   * JTA-based session tracking logic (like maybe long-session semantics).
65   *
66   * @author Steve Ebersole
67   */
68  public class JTASessionContext extends AbstractCurrentSessionContext {
69      private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, JTASessionContext.class.getName());
70  
71  	private transient Map<Object, Session> currentSessionMap = new ConcurrentHashMap<Object, Session>();
72  
73  	public JTASessionContext(SessionFactoryImplementor factory) {
74  		super( factory );
75  	}
76  
77  	@Override
78  	public Session currentSession() throws HibernateException {
79  		final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService( JtaPlatform.class );
80  		final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
81  		if ( transactionManager == null ) {
82  			throw new HibernateException( "No TransactionManagerLookup specified" );
83  		}
84  
85  		Transaction txn;
86  		try {
87  			txn = transactionManager.getTransaction();
88  			if ( txn == null ) {
89  				throw new HibernateException( "Unable to locate current JTA transaction" );
90  			}
91  			if ( !JtaStatusHelper.isActive( txn.getStatus() ) ) {
92  				// We could register the session against the transaction even though it is
93  				// not started, but we'd have no guarantee of ever getting the map
94  				// entries cleaned up (aside from spawning threads).
95  				throw new HibernateException( "Current transaction is not in progress" );
96  			}
97  		}
98  		catch ( HibernateException e ) {
99  			throw e;
100 		}
101 		catch ( Throwable t ) {
102 			throw new HibernateException( "Problem locating/validating JTA transaction", t );
103 		}
104 
105 		final Object txnIdentifier = jtaPlatform.getTransactionIdentifier( txn );
106 
107 		Session currentSession = currentSessionMap.get( txnIdentifier );
108 
109 		if ( currentSession == null ) {
110 			currentSession = buildOrObtainSession();
111 
112 			try {
113 				txn.registerSynchronization( buildCleanupSynch( txnIdentifier ) );
114 			}
115 			catch ( Throwable t ) {
116 				try {
117 					currentSession.close();
118 				}
119 				catch ( Throwable ignore ) {
120 					LOG.debug( "Unable to release generated current-session on failed synch registration", ignore );
121 				}
122 				throw new HibernateException( "Unable to register cleanup Synchronization with TransactionManager" );
123 			}
124 
125 			currentSessionMap.put( txnIdentifier, currentSession );
126 		}
127 		else {
128 			validateExistingSession( currentSession );
129 		}
130 
131 		return currentSession;
132 	}
133 
134 	/**
135 	 * Builds a {@link CleanupSynch} capable of cleaning up the the current session map as an after transaction
136 	 * callback.
137 	 *
138 	 * @param transactionIdentifier The transaction identifier under which the current session is registered.
139 	 * @return The cleanup synch.
140 	 */
141 	private CleanupSynch buildCleanupSynch(Object transactionIdentifier) {
142 		return new CleanupSynch( transactionIdentifier, this );
143 	}
144 
145 	/**
146 	 * Strictly provided for subclassing purposes; specifically to allow long-session
147 	 * support.
148 	 * <p/>
149 	 * This implementation always just opens a new session.
150 	 *
151 	 * @return the built or (re)obtained session.
152 	 */
153 	protected Session buildOrObtainSession() {
154 		return baseSessionBuilder()
155 				.autoClose( isAutoCloseEnabled() )
156 				.connectionReleaseMode( getConnectionReleaseMode() )
157 				.flushBeforeCompletion( isAutoFlushEnabled() )
158 				.openSession();
159 	}
160 
161 	/**
162 	 * Mainly for subclass usage.  This impl always returns true.
163 	 *
164 	 * @return Whether or not the the session should be closed by transaction completion.
165 	 */
166 	protected boolean isAutoCloseEnabled() {
167 		return true;
168 	}
169 
170 	/**
171 	 * Mainly for subclass usage.  This impl always returns true.
172 	 *
173 	 * @return Whether or not the the session should be flushed prior transaction completion.
174 	 */
175 	protected boolean isAutoFlushEnabled() {
176 		return true;
177 	}
178 
179 	/**
180 	 * Mainly for subclass usage.  This impl always returns after_statement.
181 	 *
182 	 * @return The connection release mode for any built sessions.
183 	 */
184 	protected ConnectionReleaseMode getConnectionReleaseMode() {
185 		return ConnectionReleaseMode.AFTER_STATEMENT;
186 	}
187 
188 	/**
189 	 * JTA transaction synch used for cleanup of the internal session map.
190 	 */
191 	protected static class CleanupSynch implements Synchronization {
192 		private Object transactionIdentifier;
193 		private JTASessionContext context;
194 
195 		public CleanupSynch(Object transactionIdentifier, JTASessionContext context) {
196 			this.transactionIdentifier = transactionIdentifier;
197 			this.context = context;
198 		}
199 
200 		/**
201 		 * {@inheritDoc}
202 		 */
203 		public void beforeCompletion() {
204 		}
205 
206 		/**
207 		 * {@inheritDoc}
208 		 */
209 		public void afterCompletion(int i) {
210 			context.currentSessionMap.remove( transactionIdentifier );
211 		}
212 	}
213 }