/** * Retrieve a Session from the given SessionHolder, potentially from a * JTA transaction synchronization. * @param sessionHolder the SessionHolder to check * @param sessionFactory the SessionFactory to get the JTA TransactionManager from * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the * Session on transaction synchronization (may be {@code null}) * @return the associated Session, if any * @throws DataAccessResourceFailureException if the Session couldn't be created */ private static Session getJtaSynchronizedSession( SessionHolder sessionHolder, SessionFactory sessionFactory, SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException { // JTA synchronization is only possible with a javax.transaction.TransactionManager. // We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified // in Hibernate configuration, it will contain a TransactionManager reference. TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession()); if (jtaTm != null) { // Check whether JTA transaction management is active -> // fetch pre-bound Session for the current JTA transaction, if any. // (just necessary for JTA transaction suspension, with an individual // Hibernate Session per currently active/suspended transaction) try { // Look for transaction-specific Session. Transaction jtaTx = jtaTm.getTransaction(); if (jtaTx != null) { int jtaStatus = jtaTx.getStatus(); if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) { Session session = sessionHolder.getValidatedSession(jtaTx); if (session == null && !sessionHolder.isSynchronizedWithTransaction()) { // No transaction-specific Session found: If not already marked as // synchronized with transaction, register the default thread-bound // Session as JTA-transactional. If there is no default Session, // we're a new inner JTA transaction with an outer one being suspended: // In that case, we'll return null to trigger opening of a new Session. session = sessionHolder.getValidatedSession(); if (session != null) { logger.debug("Registering JTA transaction synchronization for existing Hibernate Session"); sessionHolder.addSession(jtaTx, session); jtaTx.registerSynchronization( new SpringJtaSynchronizationAdapter( new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false), jtaTm)); sessionHolder.setSynchronizedWithTransaction(true); // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session // with FlushMode.NEVER, which needs to allow flushing within the transaction. FlushMode flushMode = session.getFlushMode(); if (flushMode.lessThan(FlushMode.COMMIT)) { session.setFlushMode(FlushMode.AUTO); sessionHolder.setPreviousFlushMode(flushMode); } } } return session; } } // No transaction active -> simply return default thread-bound Session, if any // (possibly from OpenSessionInViewFilter/Interceptor). return sessionHolder.getValidatedSession(); } catch (Throwable ex) { throw new DataAccessResourceFailureException("Could not check JTA transaction", ex); } } else { // No JTA TransactionManager -> simply return default thread-bound Session, if any // (possibly from OpenSessionInViewFilter/Interceptor). return sessionHolder.getValidatedSession(); } }
/** * Register a JTA synchronization for the given Session, if any. * @param sessionHolder the existing thread-bound SessionHolder, if any * @param session the Session to register * @param sessionFactory the SessionFactory that the Session was created with * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the * Session on transaction synchronization (may be {@code null}) */ private static void registerJtaSynchronization(Session session, SessionFactory sessionFactory, SQLExceptionTranslator jdbcExceptionTranslator, SessionHolder sessionHolder) { // JTA synchronization is only possible with a javax.transaction.TransactionManager. // We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified // in Hibernate configuration, it will contain a TransactionManager reference. TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, session); if (jtaTm != null) { try { Transaction jtaTx = jtaTm.getTransaction(); if (jtaTx != null) { int jtaStatus = jtaTx.getStatus(); if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) { logger.debug("Registering JTA transaction synchronization for new Hibernate Session"); SessionHolder holderToUse = sessionHolder; // Register JTA Transaction with existing SessionHolder. // Create a new SessionHolder if none existed before. if (holderToUse == null) { holderToUse = new SessionHolder(jtaTx, session); } else { holderToUse.addSession(jtaTx, session); } jtaTx.registerSynchronization( new SpringJtaSynchronizationAdapter( new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true), jtaTm)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != sessionHolder) { TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse); } } } } catch (Throwable ex) { throw new DataAccessResourceFailureException( "Could not register synchronization with JTA TransactionManager", ex); } } }