protected TransactionManager getJtaTransactionManager(Session session) { SessionFactoryImplementor sessionFactoryImpl = null; if (sessionFactory instanceof SessionFactoryImplementor) { sessionFactoryImpl = ((SessionFactoryImplementor) sessionFactory); } else if (session != null) { SessionFactory internalFactory = session.getSessionFactory(); if (internalFactory instanceof SessionFactoryImplementor) { sessionFactoryImpl = (SessionFactoryImplementor) internalFactory; } } if (sessionFactoryImpl == null) { return null; } ServiceBinding<JtaPlatform> sb = sessionFactory.getServiceRegistry().locateServiceBinding(JtaPlatform.class); if (sb == null) { return null; } return sb.getService().retrieveTransactionManager(); }
@Override @SuppressWarnings( {"unchecked"}) public JtaPlatform initiateService(Map configurationValues, ServiceRegistryImplementor registry) { final Object setting = configurationValues.get( AvailableSettings.JTA_PLATFORM ); final JtaPlatform platform = registry.getService( StrategySelector.class ).resolveStrategy( JtaPlatform.class, setting ); if ( platform == null ) { LOG.debugf( "No JtaPlatform was specified, checking resolver" ); return registry.getService( JtaPlatformResolver.class ).resolveJtaPlatform( configurationValues, registry ); } return platform; }
public TransactionEnvironmentImpl(SessionFactoryImpl sessionFactory) { this.sessionFactory = sessionFactory; this.statisticsImplementor = sessionFactory.getStatisticsImplementor(); this.serviceRegistry = sessionFactory.getServiceRegistry(); this.jdbcServices = serviceRegistry.getService( JdbcServices.class ); this.jtaPlatform = serviceRegistry.getService( JtaPlatform.class ); this.transactionFactory = serviceRegistry.getService( TransactionFactory.class ); }
private boolean canAccessTransactionManager() { try { return serviceRegistry.getService( JtaPlatform.class ).retrieveTransactionManager() != null; } catch (Exception e) { return false; } }
/** * Registers transaction synchronization with session in order to clean up and close the session when transaction * finishes. * * @param session * Session to register into transaction synchronization. Cannot be null. * @return Returns <code>true</code> if the session was register into any available synchronization strategy, * <code>false</code> otherwise. */ private boolean registerSynchronization(final Session session) { // Tries Spring's transaction manager synchronization. if (TransactionSynchronizationManager.isSynchronizationActive()) { // If it's allowed, registers synchronization to cleanup session. TransactionSynchronizationManager.registerSynchronization(createTransactionSynchronization(session)); return true; } else { // Tries JTA transaction manager synchronization. JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class); // If it's allowed, registers synchronization to cleanup session. if (jtaPlatform.canRegisterSynchronization()) { List<TransactionSynchronization> synchronizations; synchronizations = Arrays.asList(createTransactionSynchronization(session)); Synchronization jtaSync; jtaSync = new JtaAfterCompletionSynchronization(synchronizations); jtaPlatform.registerSynchronization(jtaSync); return true; } } return false; }
/** * Create a new SpringSessionContext for the given Hibernate SessionFactory. * @param sessionFactory the SessionFactory to provide current Sessions for */ public SpringSessionContext(SessionFactoryImplementor sessionFactory) { this.sessionFactory = sessionFactory; try { JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class); this.transactionManager = jtaPlatform.retrieveTransactionManager(); if (this.transactionManager != null) { this.jtaSessionContext = new SpringJtaSessionContext(sessionFactory); } } catch (Exception ex) { LogFactory.getLog(SpringSessionContext.class).warn( "Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext", ex); } }
public JtaPlatform getJtaPlatform() { return jtaPlatform; }
void setJtaPlatform(JtaPlatform jtaPlatform) { this.jtaPlatform = jtaPlatform; }
@Override public Class<JtaPlatform> getServiceInitiated() { return JtaPlatform.class; }
@SuppressWarnings({"unchecked"}) private void attemptToRegisterJtaSync() { if ( synchronizationRegistered ) { return; } final JtaPlatform jtaPlatform = getTransactionEnvironment().getJtaPlatform(); if ( jtaPlatform == null ) { // if no jta platform was registered we wont be able to register a jta synchronization return; } // Has the local transaction (Hibernate facade) taken on the responsibility of driving the transaction inflow? if ( currentHibernateTransaction.isInitiator() ) { return; } final JoinStatus joinStatus = currentHibernateTransaction.getJoinStatus(); if ( joinStatus != JoinStatus.JOINED ) { // the transaction is not (yet) joined, see if we should join... if ( !transactionContext.shouldAutoJoinTransaction() ) { // we are supposed to not auto join transactions; if the transaction is not marked for join // we cannot go any further in attempting to join (register sync). if ( joinStatus != JoinStatus.MARKED_FOR_JOINED ) { if (isDebugging) { LOG.debug( "Skipping JTA sync registration due to auto join checking" ); } return; } } } // IMPL NOTE : At this point the local callback is the "maybe" one. The only time that needs to change is if // we are able to successfully register the transaction synchronization in which case the local callback would become // non driving. To that end, the following checks are simply opt outs where we are unable to register the // synchronization // Can we resister a synchronization if ( !jtaPlatform.canRegisterSynchronization() ) { if (isTracing) { LOG.trace( "registered JTA platform says we cannot currently register synchronization; skipping" ); } return; } // Should we resister a synchronization if ( !transactionFactory().isJoinableJtaTransaction( this, currentHibernateTransaction ) ) { if (isTracing) { LOG.trace( "TransactionFactory reported no JTA transaction to join; skipping Synchronization registration" ); } return; } jtaPlatform.registerSynchronization( new RegisteredSynchronization( getSynchronizationCallbackCoordinator() ) ); getSynchronizationCallbackCoordinator().synchronizationRegistered(); synchronizationRegistered = true; if (isDebugging) { LOG.debug( "successfully registered Synchronization" ); } }
@Override public JtaPlatform getJtaPlatform() { return jtaPlatform; }
private void addJtaPlatforms(StrategySelectorImpl strategySelector, Class<? extends JtaPlatform> impl, String... names) { for ( String name : names ) { strategySelector.registerStrategyImplementor( JtaPlatform.class, name, impl ); } }
public IgniteTransactionManagerFactory(JtaPlatform platform) { this.platform = platform; }
@Override public void injectServices(ServiceRegistryImplementor serviceRegistryImplementor) { this.jtaPlatform = serviceRegistryImplementor.getService( JtaPlatform.class ); this.jdbcServices = serviceRegistryImplementor.getService( JdbcServices.class ); }
private static TransactionManager extractJBossTransactionManager(EntityManagerFactory factory) { SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) ( (HibernateEntityManagerFactory) factory ).getSessionFactory(); return sessionFactory.getServiceRegistry().getService( JtaPlatform.class ).retrieveTransactionManager(); }
public void initJta() { JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class); TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager(); jtaSessionContext = transactionManager == null ? null : new SpringJtaSessionContext(sessionFactory); }
@Override public JtaPlatform getProvidedJtaPlatform() { return new AbstractJtaPlatform() { private static final long serialVersionUID = 1L; @Override protected TransactionManager locateTransactionManager() { return transactionManager; } @Override protected UserTransaction locateUserTransaction() { return new UserTransaction() { @Override public void begin() throws NotSupportedException, SystemException { transactionManager.begin(); } @Override public void commit() throws HeuristicMixedException, HeuristicRollbackException, IllegalStateException, RollbackException, SecurityException, SystemException { transactionManager.commit(); } @Override public int getStatus() throws SystemException { return transactionManager.getStatus(); } @Override public void rollback() throws IllegalStateException, SecurityException, SystemException { transactionManager.rollback(); } @Override public void setRollbackOnly() throws IllegalStateException, SystemException { transactionManager.setRollbackOnly(); } @Override public void setTransactionTimeout(int seconds) throws SystemException { transactionManager.setTransactionTimeout(seconds); } }; } }; }
@Override public JtaPlatform getProvidedJtaPlatform() { return delegate; }
/** {@inheritDoc} */ @Nullable @Override protected StandardServiceRegistryBuilder registryBuilder() { StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); DatasourceConnectionProviderImpl connProvider = new DatasourceConnectionProviderImpl(); BasicManagedDataSource dataSrc = new BasicManagedDataSource(); // JTA-aware data source. dataSrc.setTransactionManager(jotm.getTransactionManager()); dataSrc.setDefaultAutoCommit(false); JdbcDataSource h2DataSrc = new JdbcDataSource(); h2DataSrc.setURL(CONNECTION_URL); dataSrc.setXaDataSourceInstance(h2DataSrc); connProvider.setDataSource(dataSrc); connProvider.configure(Collections.emptyMap()); builder.addService(ConnectionProvider.class, connProvider); builder.addService(JtaPlatform.class, new TestJtaPlatform()); builder.applySetting(Environment.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class.getName()); return builder; }
/** * Provide subclasses with convenient access to the configured {@link JtaPlatform} * * @return The {@link org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform} */ protected JtaPlatform jtaPlatform() { return transactionCoordinator().getTransactionContext().getTransactionEnvironment().getJtaPlatform(); }
/** * Retrieve the JTA platform for this environment. * * @return The JTA platform */ public JtaPlatform getJtaPlatform();