下面列出了怎么用org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Builds the selector.
*
* @param classLoaderService The class loading service used to (attempt to) resolve any un-registered
* strategy implementations.
*
* @return The selector.
*/
public static StrategySelector buildSelector(ClassLoaderService classLoaderService) {
final StrategySelectorImpl strategySelector = new StrategySelectorImpl(classLoaderService);
// build the baseline...
strategySelector.registerStrategyLazily(Dialect.class, new DefaultDialectSelector());
strategySelector.registerStrategyLazily(JtaPlatform.class, new DefaultJtaPlatformSelector());
addTransactionCoordinatorBuilders(strategySelector);
addMultiTableBulkIdStrategies(strategySelector);
addImplicitNamingStrategies(strategySelector);
addCacheKeysFactories(strategySelector);
// Required to support well known extensions e.g. Envers
// TODO: should we introduce a new integrator SPI to limit these to extensions supported by Quarkus?
for (StrategyRegistrationProvider provider : classLoaderService.loadJavaServices(StrategyRegistrationProvider.class)) {
for (StrategyRegistration discoveredStrategyRegistration : provider.getStrategyRegistrations()) {
applyFromStrategyRegistration(strategySelector, discoveredStrategyRegistration);
}
}
return strategySelector;
}
/**
* Construct a JtaTransactionCoordinatorImpl instance. package-protected to ensure access goes through
* builder.
*
* @param owner The transactionCoordinatorOwner
* @param autoJoinTransactions Should JTA transactions be auto-joined? Or should we wait for explicit join calls?
*/
JtaTransactionCoordinatorImpl(
TransactionCoordinatorBuilder transactionCoordinatorBuilder,
TransactionCoordinatorOwner owner,
boolean autoJoinTransactions) {
this.transactionCoordinatorBuilder = transactionCoordinatorBuilder;
this.transactionCoordinatorOwner = owner;
this.autoJoinTransactions = autoJoinTransactions;
this.observers = new ArrayList<>();
final JdbcSessionContext jdbcSessionContext = owner.getJdbcSessionOwner().getJdbcSessionContext();
this.jtaPlatform = jdbcSessionContext.getServiceRegistry().getService( JtaPlatform.class );
final SessionFactoryOptions sessionFactoryOptions = jdbcSessionContext.getSessionFactory().getSessionFactoryOptions();
this.preferUserTransactions = sessionFactoryOptions.isPreferUserTransaction();
this.performJtaThreadTracking = sessionFactoryOptions.isJtaTrackByThread();
synchronizationRegistered = false;
pulse();
}
public JtaTransactionCoordinatorImpl(
TransactionCoordinatorBuilder transactionCoordinatorBuilder,
TransactionCoordinatorOwner owner,
boolean autoJoinTransactions,
JtaPlatform jtaPlatform,
boolean preferUserTransactions,
boolean performJtaThreadTracking,
TransactionObserver... observers) {
this.transactionCoordinatorBuilder = transactionCoordinatorBuilder;
this.transactionCoordinatorOwner = owner;
this.autoJoinTransactions = autoJoinTransactions;
this.jtaPlatform = jtaPlatform;
this.preferUserTransactions = preferUserTransactions;
this.performJtaThreadTracking = performJtaThreadTracking;
this.observers = new ArrayList<>();
if ( observers != null ) {
Collections.addAll( this.observers, observers );
}
synchronizationRegistered = false;
pulse();
}
@Override
@SuppressWarnings( {"unchecked"})
public JtaPlatform initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object setting = configurationValues.get( AvailableSettings.JTA_PLATFORM );
JtaPlatform platform = registry.getService( StrategySelector.class ).resolveStrategy( JtaPlatform.class, setting );
if ( platform == null ) {
LOG.debugf( "No JtaPlatform was specified, checking resolver" );
platform = registry.getService( JtaPlatformResolver.class ).resolveJtaPlatform( configurationValues, registry );
}
if ( platform == null ) {
LOG.debugf( "No JtaPlatform was specified, checking resolver" );
platform = getFallbackProvider( configurationValues, registry );
}
return platform;
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
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);
}
}
@Override
public JtaPlatform initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
return NoJtaPlatform.INSTANCE;
}
@Override
public Class<JtaPlatform> getServiceInitiated() {
return JtaPlatform.class;
}
@Override
public JtaPlatform initiateService(Map map, ServiceRegistryImplementor serviceRegistryImplementor) {
return buildJtaPlatformInstance();
}
public JtaPlatform buildJtaPlatformInstance() {
return jtaIsPresent ? getJtaInstance() : getNoJtaInstance();
}
@Override
public Class<JtaPlatform> getServiceInitiated() {
return JtaPlatform.class;
}
public JtaPlatform getJtaPlatform() {
return sessionFactoryOptions.getServiceRegistry().getService( JtaPlatform.class );
}
@Override
public Class<JtaPlatform> getServiceInitiated() {
return JtaPlatform.class;
}
@SuppressWarnings({"WeakerAccess", "unused"})
protected JtaPlatform getFallbackProvider(Map configurationValues, ServiceRegistryImplementor registry) {
return null;
}
private JtaPlatform getJtaPlatform() {
return getFactory().getServiceRegistry().getService( JtaPlatform.class );
}
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.classLoaderService = serviceRegistryImplementor.getService( ClassLoaderService.class );
this.jtaPlatform = serviceRegistryImplementor.getService( JtaPlatform.class );
this.jdbcServices = serviceRegistryImplementor.getService( JdbcServices.class );
}
public void initJta() {
JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
jtaSessionContext = transactionManager == null ? null : new SpringJtaSessionContext(sessionFactory);
}
/** {@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;
}
/** {@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;
}