public static void ReleaseResources(RabbitResourceHolder resourceHolder) { if (resourceHolder == null || resourceHolder.SynchronizedWithTransaction) { return; } RabbitUtils.CloseChannel(resourceHolder.GetChannel()); RabbitUtils.CloseConnection(resourceHolder.GetConnection()); }
public void TestResourceHolder() { var h1 = new RabbitResourceHolder(); var h2 = new RabbitResourceHolder(); var connectionFactory = new Mock <IConnectionFactory>(); TransactionSynchronizationManager.SetActualTransactionActive(true); ConnectionFactoryUtils.BindResourceToTransaction(h1, connectionFactory.Object, true); Assert.Same(h1, ConnectionFactoryUtils.BindResourceToTransaction(h2, connectionFactory.Object, true)); TransactionSynchronizationManager.Clear(); }
public static RabbitResourceHolder BindResourceToTransaction(RabbitResourceHolder resourceHolder, IConnectionFactory connectionFactory, bool synched) { if (TransactionSynchronizationManager.HasResource(connectionFactory) || !TransactionSynchronizationManager.IsActualTransactionActive() || !synched) { return((RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory)); // NOSONAR never null } TransactionSynchronizationManager.BindResource(connectionFactory, resourceHolder); resourceHolder.SynchronizedWithTransaction = true; if (TransactionSynchronizationManager.IsSynchronizationActive()) { TransactionSynchronizationManager.RegisterSynchronization(new RabbitResourceSynchronization(resourceHolder, connectionFactory)); } return(resourceHolder); }
private static RabbitResourceHolder DoGetTransactionalResourceHolder(IConnectionFactory connectionFactory, IResourceFactory resourceFactory) { if (connectionFactory == null) { throw new ArgumentNullException(nameof(connectionFactory)); } if (resourceFactory == null) { throw new ArgumentNullException(nameof(resourceFactory)); } var resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory); if (resourceHolder != null) { var model = resourceFactory.GetChannel(resourceHolder); if (model != null) { return(resourceHolder); } } var resourceHolderToUse = resourceHolder; if (resourceHolderToUse == null) { resourceHolderToUse = new RabbitResourceHolder(); } var connection = resourceFactory.GetConnection(resourceHolderToUse); RC.IModel channel; try { /* * If we are in a listener container, first see if there's a channel registered * for this consumer and the consumer is using the same connection factory. */ channel = ConsumerChannelRegistry.GetConsumerChannel(connectionFactory); if (channel == null && connection == null) { connection = resourceFactory.CreateConnection2(); if (resourceHolder == null) { /* * While creating a connection, a connection listener might have created a * transactional channel and bound it to the transaction. */ resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory); if (resourceHolder != null) { channel = resourceHolder.GetChannel(); resourceHolderToUse = resourceHolder; } } resourceHolderToUse.AddConnection(connection); } if (channel == null) { channel = resourceFactory.CreateChannel(connection); } resourceHolderToUse.AddChannel(channel, connection); if (!resourceHolderToUse.Equals(resourceHolder)) { BindResourceToTransaction(resourceHolderToUse, connectionFactory, resourceFactory.IsSynchedLocalTransactionAllowed); } return(resourceHolderToUse); } catch (Exception ex) { RabbitUtils.CloseConnection(connection); throw RabbitExceptionTranslator.ConvertRabbitAccessException(ex); } }
public IConnection GetConnection(RabbitResourceHolder holder) { return(holder.GetConnection()); }
public RC.IModel GetChannel(RabbitResourceHolder holder) { return(holder.GetChannel()); }