/// <summary> /// Determines whether the given JMS Session is transactional, that is, /// bound to the current thread by Spring's transaction facilities. /// </summary> /// <param name="session">The session to check.</param> /// <param name="cf">The ConnectionFactory that the Session originated from</param> /// <returns> /// <c>true</c> if is session transactional, bound to current thread; otherwise, <c>false</c>. /// </returns> public static bool IsSessionTransactional(ISession session, IConnectionFactory cf) { if (session == null || cf == null) { return(false); } EmsResourceHolder resourceHolder = (EmsResourceHolder)TransactionSynchronizationManager.GetResource(cf); return(resourceHolder != null && resourceHolder.ContainsSession(session)); }
/// <summary> /// Resume the resources of the current transaction. /// </summary> /// <param name="transaction">Transaction object returned by /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoGetTransaction"/>.</param> /// <param name="suspendedResources">The object that holds suspended resources as returned by /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoSuspend"/>.</param> /// <remarks>Transaction synchronization will be resumed afterwards. /// </remarks> /// <exception cref="Spring.Transaction.TransactionException"> /// In the case of system errors. /// </exception> protected override void DoResume(object transaction, object suspendedResources) { EmsResourceHolder conHolder = (EmsResourceHolder)suspendedResources; TransactionSynchronizationManager.BindResource(ConnectionFactory, conHolder); }
public EmsResourceSynchronization(EmsResourceHolder resourceHolder, object resourceKey, bool transacted) { this.resourceKey = resourceKey; this.resourceHolder = resourceHolder; this.transacted = transacted; }
public virtual IConnection GetConnection(EmsResourceHolder holder) { return(existingCon != null ? existingCon : holder.GetConnection()); }
public virtual ISession GetSession(EmsResourceHolder holder) { return(holder.GetSession(typeof(ISession), existingCon)); }
/// <summary> /// Obtain a EMS Session that is synchronized with the current transaction, if any. /// </summary> /// <param name="resourceKey">the TransactionSynchronizationManager key to bind to /// (usually the ConnectionFactory)</param> /// <param name="resourceFactory">the ResourceFactory to use for extracting or creating /// EMS resources</param> /// <param name="startConnection">whether the underlying Connection approach should be /// started in order to allow for receiving messages. Note that a reused Connection /// may already have been started before, even if this flag is <code>false</code>.</param> /// <returns> /// the transactional Session, or <code>null</code> if none found /// </returns> /// <throws>EMSException in case of EMS failure </throws> public static ISession DoGetTransactionalSession(Object resourceKey, ResourceFactory resourceFactory, bool startConnection) { AssertUtils.ArgumentNotNull(resourceKey, "Resource key must not be null"); AssertUtils.ArgumentNotNull(resourceKey, "ResourceFactory must not be null"); EmsResourceHolder resourceHolder = (EmsResourceHolder)TransactionSynchronizationManager.GetResource(resourceKey); if (resourceHolder != null) { ISession rhSession = resourceFactory.GetSession(resourceHolder); if (rhSession != null) { if (startConnection) { IConnection conn = resourceFactory.GetConnection(resourceHolder); if (conn != null) { conn.Start(); } } return(rhSession); } } if (!TransactionSynchronizationManager.SynchronizationActive) { return(null); } EmsResourceHolder resourceHolderToUse = resourceHolder; if (resourceHolderToUse == null) { resourceHolderToUse = new EmsResourceHolder(); } IConnection con = resourceFactory.GetConnection(resourceHolderToUse); ISession session = null; try { bool isExistingCon = (con != null); if (!isExistingCon) { con = resourceFactory.CreateConnection(); resourceHolderToUse.AddConnection(con); } session = resourceFactory.CreateSession(con); resourceHolderToUse.AddSession(session, con); if (startConnection) { con.Start(); } } catch (EMSException) { if (session != null) { try { session.Close(); } catch (Exception) { // ignore } } if (con != null) { try { con.Close(); } catch (Exception) { // ignore } } throw; } if (resourceHolderToUse != resourceHolder) { TransactionSynchronizationManager.RegisterSynchronization( new EmsResourceSynchronization(resourceHolderToUse, resourceKey, resourceFactory.SynchedLocalTransactionAllowed)); resourceHolderToUse.SynchronizedWithTransaction = true; TransactionSynchronizationManager.BindResource(resourceKey, resourceHolderToUse); } return(session); }
/// <summary> Fetch an appropriate Session from the given EmsResourceHolder. /// </summary> /// <param name="holder">the EmsResourceHolder /// </param> /// <returns> an appropriate Session fetched from the holder, /// or <code>null</code> if none found /// </returns> protected virtual ISession GetSession(EmsResourceHolder holder) { return holder.GetSession(); }
public virtual ISession GetSession(EmsResourceHolder holder) { return EnclosingInstance.GetSession(holder); }
/// <summary> Fetch an appropriate Connection from the given EmsResourceHolder. /// </summary> /// <param name="holder">the EmsResourceHolder /// </param> /// <returns> an appropriate Connection fetched from the holder, /// or <code>null</code> if none found /// </returns> protected virtual IConnection GetConnection(EmsResourceHolder holder) { return holder.GetConnection(); }
public virtual IConnection GetConnection(EmsResourceHolder holder) { return EnclosingInstance.GetConnection(holder); }
public EmsResourceSynchronization(EmsResourceHolder resourceHolder, object resourceKey, bool transacted) { this.resourceKey = resourceKey; this.resourceHolder = resourceHolder; this.transacted = transacted; }
public virtual IConnection GetConnection(EmsResourceHolder holder) { return (existingCon != null ? existingCon : holder.GetConnection()); }
public virtual ISession GetSession(EmsResourceHolder holder) { return holder.GetSession(typeof(ISession), existingCon); }
/// <summary> /// Obtain a EMS Session that is synchronized with the current transaction, if any. /// </summary> /// <param name="resourceKey">the TransactionSynchronizationManager key to bind to /// (usually the ConnectionFactory)</param> /// <param name="resourceFactory">the ResourceFactory to use for extracting or creating /// EMS resources</param> /// <param name="startConnection">whether the underlying Connection approach should be /// started in order to allow for receiving messages. Note that a reused Connection /// may already have been started before, even if this flag is <code>false</code>.</param> /// <returns> /// the transactional Session, or <code>null</code> if none found /// </returns> /// <throws>EMSException in case of EMS failure </throws> public static ISession DoGetTransactionalSession(Object resourceKey, ResourceFactory resourceFactory, bool startConnection) { AssertUtils.ArgumentNotNull(resourceKey, "Resource key must not be null"); AssertUtils.ArgumentNotNull(resourceKey, "ResourceFactory must not be null"); EmsResourceHolder resourceHolder = (EmsResourceHolder)TransactionSynchronizationManager.GetResource(resourceKey); if (resourceHolder != null) { ISession rhSession = resourceFactory.GetSession(resourceHolder); if (rhSession != null) { if (startConnection) { IConnection conn = resourceFactory.GetConnection(resourceHolder); if (conn != null) { conn.Start(); } } return rhSession; } } if (!TransactionSynchronizationManager.SynchronizationActive) { return null; } EmsResourceHolder resourceHolderToUse = resourceHolder; if (resourceHolderToUse == null) { resourceHolderToUse = new EmsResourceHolder(); } IConnection con = resourceFactory.GetConnection(resourceHolderToUse); ISession session = null; try { bool isExistingCon = (con != null); if (!isExistingCon) { con = resourceFactory.CreateConnection(); resourceHolderToUse.AddConnection(con); } session = resourceFactory.CreateSession(con); resourceHolderToUse.AddSession(session, con); if (startConnection) { con.Start(); } } catch (EMSException) { if (session != null) { try { session.Close(); } catch (Exception) { // ignore } } if (con != null) { try { con.Close(); } catch (Exception) { // ignore } } throw; } if (resourceHolderToUse != resourceHolder) { TransactionSynchronizationManager.RegisterSynchronization( new EmsResourceSynchronization(resourceHolderToUse, resourceKey, resourceFactory.SynchedLocalTransactionAllowed)); resourceHolderToUse.SynchronizedWithTransaction = true; TransactionSynchronizationManager.BindResource(resourceKey, resourceHolderToUse); } return session; }