org.hibernate.context.internal.JTASessionContext Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.context.internal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import jakarta.transaction.Synchronization;
import jakarta.transaction.Transaction;
import jakarta.transaction.TransactionManager;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.context.spi.AbstractCurrentSessionContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.internal.CoreMessageLogger;
import org.jboss.logging.Logger;
/**
* An implementation of {@link org.hibernate.context.spi.CurrentSessionContext} which scopes the notion
* of a current session to a JTA transaction. Because JTA gives us a nice tie-in to clean up after
* ourselves, this implementation will generate Sessions as needed provided a JTA transaction is in
* effect. If a session is not already associated with the current JTA transaction at the time
* {@link #currentSession()} is called, a new session will be opened and it will be associated with that
* JTA transaction.
*
* Note that the sessions returned from this method are automatically configured with both the
* {@link org.hibernate.cfg.Environment#FLUSH_BEFORE_COMPLETION auto-flush} and
* {@link org.hibernate.cfg.Environment#AUTO_CLOSE_SESSION auto-close} attributes set to true, meaning
* that the Session will be automatically flushed and closed as part of the lifecycle for the JTA
* transaction to which it is associated. Additionally, it will also be configured to aggressively
* release JDBC connections after each statement is executed. These settings are governed by the
* {@link #isAutoFlushEnabled()}, {@link #isAutoCloseEnabled()}, and {@link #getConnectionReleaseMode()}
* methods; these are provided (along with the {@link #buildOrObtainSession()} method) for easier
* subclassing for custom JTA-based session tracking logic (like maybe long-session semantics).
*
* @author Steve Ebersole
*/
public class JTASessionContext extends AbstractCurrentSessionContext {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
JTASessionContext.class.getName()
);
private transient Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy