protected override ITransactionAttribute FindTransactionAttribute(Attribute[] attributes)
        {
            if (attributes == null)
            {
                return null;
            }

            var attribute = attributes.FirstOrDefault(attr => attr is TransactionAttribute) as TransactionAttribute;

            if(attribute == null)
            {
                return null;
            }

            var ruleBasedTransactionAttribute = new RuleBasedTransactionAttribute
                                                    {
                                                        PropagationBehavior = attribute.TransactionPropagation,
                                                        TransactionIsolationLevel = attribute.IsolationLevel,
                                                        ReadOnly = attribute.ReadOnly,
                                                        EnterpriseServicesInteropOption =
                                                            attribute.EnterpriseServicesInteropOption
                                                    };

            IEnumerable<Attribute> rollBackFor = attribute.RollbackFor.Select(type => new RollbackRuleAttribute(type) as Attribute);
            IEnumerable<Attribute> noRollBackFor = attribute.NoRollbackFor.Select(type => new NoRollbackRuleAttribute(type) as Attribute);
            IList<Attribute> rollBackRules = rollBackFor.Union(noRollBackFor).ToList();                   
            
            ruleBasedTransactionAttribute.RollbackRules = rollBackRules.ToList();

            return ruleBasedTransactionAttribute;
        }
        public void RuleBasedTransactionAttributeToString()
        {
            RuleBasedTransactionAttribute source = new RuleBasedTransactionAttribute();

            source.PropagationBehavior       = TransactionPropagation.Supports;
            source.TransactionIsolationLevel = IsolationLevel.RepeatableRead;
            source.TransactionTimeout        = 10;
            source.ReadOnly = true;
            source.AddRollbackRule(new RollbackRuleAttribute("ArgumentException"));
            source.AddRollbackRule(new NoRollbackRuleAttribute("IllegalTransactionStateException"));

            TransactionAttributeEditor editor = new TransactionAttributeEditor();

            editor.SetAsText(source.ToString());
            ITransactionAttribute ta = editor.Value;

            Assert.AreEqual(source, ta);
            Assert.AreEqual(ta.PropagationBehavior, TransactionPropagation.Supports);
            Assert.AreEqual(ta.TransactionIsolationLevel, IsolationLevel.RepeatableRead);
            Assert.AreEqual(ta.TransactionTimeout, 10);
            Assert.IsTrue(ta.ReadOnly);
            Assert.IsTrue(ta.RollbackOn(new ArgumentException()));
            Assert.IsFalse(ta.RollbackOn(new IllegalTransactionStateException()));

            source.ClearRollbackRules();
            Assert.IsFalse(ta == source);
            source.AddRollbackRule(new RollbackRuleAttribute("ArgumentException"));
            source.AddRollbackRule(new NoRollbackRuleAttribute("IllegalTransactionStateException"));
            Assert.AreEqual(ta, source);
        }
        /// <summary>
        /// Return the transaction attribute, given this set of attributes
        /// attached to a method or class. Return null if it's not transactional.
        /// </summary>
        /// <remarks>
        /// Protected rather than private as subclasses may want to customize
        /// how this is done: for example, returning a
        /// <see cref="Spring.Transaction.Interceptor.ITransactionAttribute"/>
        /// affected by the values of other attributes.
        /// This implementation takes into account
        /// <see cref="Spring.Transaction.Interceptor.RollbackRuleAttribute"/>s, if
        /// the TransactionAttribute is a RuleBasedTransactionAttribute.
        /// </remarks>
        /// <param name="attributes">
        /// Attributes attached to a method or class. May be null, in which case a null
        /// <see cref="Spring.Transaction.Interceptor.ITransactionAttribute"/> will be returned.
        /// </param>
        /// <returns>
        /// The <see cref="ITransactionAttribute"/> configured transaction attribute, or null
        /// if none was found.
        /// </returns>
        protected virtual ITransactionAttribute FindTransactionAttribute(Attribute[] attributes)
        {
            if (null == attributes)
            {
                return(null);
            }
            ITransactionAttribute transactionAttribute = null;

            foreach (Attribute currentAttribute in attributes)
            {
                transactionAttribute = currentAttribute as ITransactionAttribute;
                if (null != transactionAttribute)
                {
                    break;
                }
            }

            RuleBasedTransactionAttribute ruleBasedTransactionAttribute = transactionAttribute as RuleBasedTransactionAttribute;

            if (null != ruleBasedTransactionAttribute)
            {
                IList rollbackRules = new LinkedList();
                foreach (Attribute currentAttribute in attributes)
                {
                    RollbackRuleAttribute rollbackRuleAttribute = currentAttribute as RollbackRuleAttribute;
                    if (null != rollbackRuleAttribute)
                    {
                        rollbackRules.Add(rollbackRuleAttribute);
                    }
                }
                ruleBasedTransactionAttribute.RollbackRules = rollbackRules;
                return(ruleBasedTransactionAttribute);
            }
            return(transactionAttribute);
        }
			public void RuleForSelectiveRollbackOnCheckedWithClass()
		{
			IList list = new LinkedList();
			list.Add( new RollbackRuleAttribute(typeof(TransactionSystemException)));
			RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute( TransactionPropagation.Required, list );
			ruleForSelectionRollbackOnChecked( rta );
		}
        public void RuleBasedTransactionAttributeToString()
        {
            RuleBasedTransactionAttribute source = new RuleBasedTransactionAttribute();
            source.PropagationBehavior = TransactionPropagation.Supports;
            source.TransactionIsolationLevel = IsolationLevel.RepeatableRead;
            source.TransactionTimeout = 10;
            source.ReadOnly = true;
            source.AddRollbackRule( new RollbackRuleAttribute("ArgumentException"));
            source.AddRollbackRule( new NoRollbackRuleAttribute("IllegalTransactionStateException"));

            TransactionAttributeEditor editor = new TransactionAttributeEditor();
            editor.SetAsText( source.ToString() );
            ITransactionAttribute ta = editor.Value;
            Assert.AreEqual( source, ta );
            Assert.AreEqual( ta.PropagationBehavior, TransactionPropagation.Supports );
            Assert.AreEqual( ta.TransactionIsolationLevel, IsolationLevel.RepeatableRead );
            Assert.AreEqual( ta.TransactionTimeout, 10 );
            Assert.IsTrue( ta.ReadOnly );
            Assert.IsTrue(ta.RollbackOn(new ArgumentException()));
            Assert.IsFalse( ta.RollbackOn(new IllegalTransactionStateException()));

            source.ClearRollbackRules();
            Assert.IsFalse( ta == source );
            source.AddRollbackRule( new RollbackRuleAttribute("ArgumentException"));
            source.AddRollbackRule( new NoRollbackRuleAttribute("IllegalTransactionStateException"));
            Assert.AreEqual( ta, source );
        }
			public void DefaultRule()
		{
			RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute();
			Assert.IsTrue(rta.RollbackOn(new SystemException()));
            //mlp 3/17 changed rollback to rollback on all exceptions.
			Assert.IsTrue(rta.RollbackOn(new ApplicationException()));
			Assert.IsTrue( rta.RollbackOn(new TransactionSystemException()));
		}
        public void RuleForSelectiveRollbackOnCheckedWithString()
        {
            IList <RollbackRuleAttribute> list = new List <RollbackRuleAttribute>();

            list.Add(new RollbackRuleAttribute("Spring.Transaction.TransactionSystemException"));
            var rta = new RuleBasedTransactionAttribute(TransactionPropagation.Required, list);

            ruleForSelectionRollbackOnChecked(rta);
        }
        public void RuleForSelectiveRollbackOnCheckedWithClass()
        {
            IList list = new LinkedList();

            list.Add(new RollbackRuleAttribute(typeof(TransactionSystemException)));
            RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionPropagation.Required, list);

            ruleForSelectionRollbackOnChecked(rta);
        }
        public void DefaultRule()
        {
            RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute();

            Assert.IsTrue(rta.RollbackOn(new SystemException()));
            //mlp 3/17 changed rollback to rollback on all exceptions.
            Assert.IsTrue(rta.RollbackOn(new ApplicationException()));
            Assert.IsTrue(rta.RollbackOn(new TransactionSystemException()));
        }
        protected override ITransactionAttribute FindTransactionAttribute(Attribute[] attributes)
        {
            if (attributes == null)
            {
                return null;
            }
            foreach (Attribute attr in attributes)
            {
                if (attr is TransactionAttribute)
                {
                    TransactionAttribute ta = (TransactionAttribute)attr;
                    RuleBasedTransactionAttribute rbta = 
                        new RuleBasedTransactionAttribute();

                    //TODO another reminder to sync property names
                    rbta.PropagationBehavior = ta.TransactionPropagation;
                    rbta.TransactionIsolationLevel = ta.IsolationLevel;
                    rbta.ReadOnly = ta.ReadOnly;
                    rbta.TransactionTimeout = ta.Timeout;

#if NET_2_0
                    rbta.EnterpriseServicesInteropOption = ta.EnterpriseServicesInteropOption;
#endif
                    Type[] rbf = ta.RollbackFor;

                    IList rollBackRules = new ArrayList();

                    if (rbf != null)
                    {
                        foreach (Type t in rbf)
                        {
                            RollbackRuleAttribute rule = new RollbackRuleAttribute(t);
                            rollBackRules.Add(rule);
                        }
                    }


                    Type[] nrbfc = ta.NoRollbackFor;

                    if (nrbfc != null)
                    {
                        foreach (Type t in nrbfc)
                        {
                            NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(t);
                            rollBackRules.Add(rule);
                        }
                    }

                    rbta.RollbackRules = rollBackRules;

                    return rbta;

                }
            }
            return null;
        }
			public void RuleForCommitOnSubclassOfChecked()
		{
			IList list = new LinkedList();
			list.Add( new RollbackRuleAttribute("System.Data.DataException"));
			list.Add( new NoRollbackRuleAttribute("Spring.Transaction.TransactionSystemException"));
			RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute( TransactionPropagation.Required, list );
			
			Assert.IsTrue( rta.RollbackOn(new SystemException()));
			Assert.IsFalse( rta.RollbackOn(new TransactionSystemException()));																		 
		}
Пример #12
0
        protected override ITransactionAttribute FindTransactionAttribute(Attribute[] attributes)
        {
            if (attributes == null)
            {
                return(null);
            }
            foreach (Attribute attr in attributes)
            {
                if (attr is TransactionAttribute)
                {
                    TransactionAttribute          ta   = (TransactionAttribute)attr;
                    RuleBasedTransactionAttribute rbta =
                        new RuleBasedTransactionAttribute();

                    //TODO another reminder to sync property names
                    rbta.PropagationBehavior       = ta.TransactionPropagation;
                    rbta.TransactionIsolationLevel = ta.IsolationLevel;
                    rbta.ReadOnly           = ta.ReadOnly;
                    rbta.TransactionTimeout = ta.Timeout;

#if NET_2_0
                    rbta.EnterpriseServicesInteropOption = ta.EnterpriseServicesInteropOption;
#endif
                    Type[] rbf = ta.RollbackFor;

                    IList rollBackRules = new ArrayList();

                    if (rbf != null)
                    {
                        foreach (Type t in rbf)
                        {
                            RollbackRuleAttribute rule = new RollbackRuleAttribute(t);
                            rollBackRules.Add(rule);
                        }
                    }


                    Type[] nrbfc = ta.NoRollbackFor;

                    if (nrbfc != null)
                    {
                        foreach (Type t in nrbfc)
                        {
                            NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(t);
                            rollBackRules.Add(rule);
                        }
                    }

                    rbta.RollbackRules = rollBackRules;

                    return(rbta);
                }
            }
            return(null);
        }
        public void RuleForCommitOnSubclassOfChecked()
        {
            IList list = new LinkedList();

            list.Add(new RollbackRuleAttribute("System.Data.DataException"));
            list.Add(new NoRollbackRuleAttribute("Spring.Transaction.TransactionSystemException"));
            RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionPropagation.Required, list);

            Assert.IsTrue(rta.RollbackOn(new SystemException()));
            Assert.IsFalse(rta.RollbackOn(new TransactionSystemException()));
        }
			public void RuleForRollbackOnApplicationException()
		{
			IList list = new LinkedList();
			list.Add(new RollbackRuleAttribute("Spring.Transaction.TransactionSystemException"));
			RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute( TransactionPropagation.Required, list );

			Assert.IsTrue( rta.RollbackOn(new SystemException()));
            //mlp 3/17 changed rollback to rollback on all exceptions.
			Assert.IsTrue( rta.RollbackOn(new ApplicationException()));
			Assert.IsTrue(( rta.RollbackOn( new TransactionSystemException())));
		}
			public void RollbackNever()
		{
			IList list = new LinkedList();
			list.Add( new NoRollbackRuleAttribute("System.Exception"));
			RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute( TransactionPropagation.Required, list );

			Assert.IsFalse(rta.RollbackOn(new SystemException()));
			Assert.IsFalse(rta.RollbackOn(new DataException()));
			Assert.IsFalse(rta.RollbackOn(new ApplicationException()));
			Assert.IsFalse(rta.RollbackOn(new TransactionSystemException()));
		}
        public void RollbackNever()
        {
            IList list = new LinkedList();

            list.Add(new NoRollbackRuleAttribute("System.Exception"));
            RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionPropagation.Required, list);

            Assert.IsFalse(rta.RollbackOn(new SystemException()));
            Assert.IsFalse(rta.RollbackOn(new DataException()));
            Assert.IsFalse(rta.RollbackOn(new ApplicationException()));
            Assert.IsFalse(rta.RollbackOn(new TransactionSystemException()));
        }
        public void RuleForRollbackOnApplicationException()
        {
            IList list = new LinkedList();

            list.Add(new RollbackRuleAttribute("Spring.Transaction.TransactionSystemException"));
            RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionPropagation.Required, list);

            Assert.IsTrue(rta.RollbackOn(new SystemException()));
            //mlp 3/17 changed rollback to rollback on all exceptions.
            Assert.IsTrue(rta.RollbackOn(new ApplicationException()));
            Assert.IsTrue((rta.RollbackOn(new TransactionSystemException())));
        }
        public void RuleForCommitOnUnchecked()
        {
            var list = new List <RollbackRuleAttribute>();

            list.Add(new NoRollbackRuleAttribute("System.SystemException"));
            list.Add(new RollbackRuleAttribute("Spring.Transaction.TransactionSystemException"));

            var rta = new RuleBasedTransactionAttribute(TransactionPropagation.Required, list);

            Assert.IsFalse(rta.RollbackOn(new SystemException()));
            Assert.IsTrue(rta.RollbackOn(new TransactionSystemException()));
        }
Пример #19
0
 /// <summary>
 /// Parses the input properties string into a valid <see cref="Spring.Transaction.Interceptor.ITransactionAttribute"/>
 /// instance
 /// </summary>
 /// <param name="transactionProperties">
 /// The string defining the transactional properties.
 /// </param>
 public void SetAsText(string transactionProperties)
 {
     if (!StringUtils.HasText(transactionProperties))
     {
         _attribute = null;
     }
     else
     {
         string[] tokens = StringUtils.CommaDelimitedListToStringArray(transactionProperties);
         RuleBasedTransactionAttribute attribute = new RuleBasedTransactionAttribute();
         for (int i = 0; i < tokens.Length; i++)
         {
             string token = tokens[i].Trim();
             if (token.StartsWith(DefaultTransactionAttribute.PROPAGATION_CONSTANT_PREFIX))
             {
                 attribute.PropagationBehavior = convertPropagationValue(token);
             }
             else if (token.StartsWith(DefaultTransactionAttribute.ISOLATION_CONSTANT_PREFIX))
             {
                 attribute.TransactionIsolationLevel = convertIsolationValue(token);
             }
             else if (token.StartsWith(DefaultTransactionAttribute.TIMEOUT_PREFIX))
             {
                 string value = token.Substring(DefaultTransactionAttribute.TIMEOUT_PREFIX.Length);
                 attribute.TransactionTimeout = Convert.ToInt32(value);
             }
             else if (token.StartsWith(DefaultTransactionAttribute.READ_ONLY_MARKER))
             {
                 attribute.ReadOnly = true;
             }
             else if (token.StartsWith(DefaultTransactionAttribute.COMMIT_RULE_PREFIX))
             {
                 attribute.AddRollbackRule(new NoRollbackRuleAttribute(token.Substring(1)));
             }
             else if (token.StartsWith(DefaultTransactionAttribute.ROLLBACK_RULE_PREFIX))
             {
                 attribute.AddRollbackRule(new RollbackRuleAttribute(token.Substring(1)));
             }
             else
             {
                 throw new ArgumentException("Illegal transaction attribute token: [" + token + "]");
             }
         }
         _attribute = attribute;
     }
 }
		/// <summary>
		/// Parses the input properties string into a valid <see cref="Spring.Transaction.Interceptor.ITransactionAttribute"/>
		/// instance
		/// </summary>
		/// <param name="transactionProperties">
		/// The string defining the transactional properties.
		/// </param>
		public void SetAsText( string transactionProperties )
		{
			if (!StringUtils.HasText(transactionProperties))
			{
				_attribute = null;
			}
			else
			{
				string[] tokens = StringUtils.CommaDelimitedListToStringArray( transactionProperties );
				RuleBasedTransactionAttribute attribute = new RuleBasedTransactionAttribute();
				for ( int i = 0; i < tokens.Length; i++ )
				{
					string token = tokens[i].Trim();
					if ( token.StartsWith(DefaultTransactionAttribute.PROPAGATION_CONSTANT_PREFIX))
					{
						attribute.PropagationBehavior = convertPropagationValue( token );
					} else if ( token.StartsWith(DefaultTransactionAttribute.ISOLATION_CONSTANT_PREFIX ) )
					{
						attribute.TransactionIsolationLevel = convertIsolationValue( token );
					} else if ( token.StartsWith(DefaultTransactionAttribute.TIMEOUT_PREFIX ) )
					{
						string value = token.Substring(DefaultTransactionAttribute.TIMEOUT_PREFIX.Length);
						attribute.TransactionTimeout = Convert.ToInt32( value );
					} else if ( token.StartsWith( DefaultTransactionAttribute.READ_ONLY_MARKER ) )
					{
						attribute.ReadOnly = true;
					} else if ( token.StartsWith( DefaultTransactionAttribute.COMMIT_RULE_PREFIX ) )
					{
						attribute.AddRollbackRule( new NoRollbackRuleAttribute(token.Substring( 1 ) ) );
					} else if ( token.StartsWith( DefaultTransactionAttribute.ROLLBACK_RULE_PREFIX ) )
					{
						attribute.AddRollbackRule( new RollbackRuleAttribute( token.Substring( 1 ) ) );
					} else
					{
						throw new ArgumentException("Illegal transaction attribute token: [" + token + "]");
					}		
				}
				_attribute = attribute;
			}
		}
        private AbstractObjectDefinition ParseAttributeSource(XmlElement element, ParserContext parserContext)
        {
            XmlNodeList methods = element.SelectNodes("*[local-name()='method' and namespace-uri()='" + element.NamespaceURI + "']");
            ManagedDictionary transactionAttributeMap = new ManagedDictionary();
            foreach (XmlElement methodElement in methods)
            {
                string name = GetAttributeValue(methodElement, "name");
                TypedStringValue nameHolder = new TypedStringValue(name);
                
                RuleBasedTransactionAttribute attribute = new RuleBasedTransactionAttribute();
                string propagation = GetAttributeValue(methodElement, PROPAGATION);
                string isolation = GetAttributeValue(methodElement, ISOLATION);
                string timeout = GetAttributeValue(methodElement, TIMEOUT);
                string readOnly = GetAttributeValue(methodElement, READ_ONLY);
                if (StringUtils.HasText(propagation))
                {
                    attribute.PropagationBehavior = (TransactionPropagation) Enum.Parse(typeof (TransactionPropagation), propagation, true);
                }
                if (StringUtils.HasText(isolation))
                {
                    attribute.TransactionIsolationLevel =
                        (IsolationLevel) Enum.Parse(typeof (IsolationLevel), isolation, true);
                }
                if (StringUtils.HasText(timeout))
                {
                    try
                    {
                        attribute.TransactionTimeout = Int32.Parse(timeout);
                    }
                    catch (FormatException ex)
                    {
                        parserContext.ReaderContext.ReportException(methodElement,"tx advice","timeout must be an integer value: [" + timeout + "]", ex);
                    }
                }
                if (StringUtils.HasText(readOnly))
                {
                    attribute.ReadOnly = Boolean.Parse(GetAttributeValue(methodElement, READ_ONLY));
                }
                IList rollbackRules = new LinkedList();
                if (methodElement.HasAttribute(ROLLBACK_FOR))
                {
                    string rollbackForValue = GetAttributeValue(methodElement, ROLLBACK_FOR);
                    AddRollbackRuleAttributesTo(rollbackRules, rollbackForValue);
                }
                if (methodElement.HasAttribute(NO_ROLLBACK_FOR))
                {
                    string noRollbackForValue = GetAttributeValue(methodElement, NO_ROLLBACK_FOR);
                    AddNoRollbackRuleAttributesTo(rollbackRules, noRollbackForValue);
                }
                attribute.RollbackRules = rollbackRules;

                transactionAttributeMap[nameHolder] = attribute;
            }

            ObjectDefinitionBuilder builder = parserContext
                                                .ParserHelper
                                                .CreateRootObjectDefinitionBuilder(typeof (NameMatchTransactionAttributeSource));
            builder.AddPropertyValue(NAME_MAP, transactionAttributeMap);
            return builder.ObjectDefinition;

        }
 private void ruleForSelectionRollbackOnChecked(RuleBasedTransactionAttribute rta)
 {
     Assert.IsTrue(rta.RollbackOn(new SystemException()));
     Assert.IsTrue(rta.RollbackOn(new TransactionSystemException()));
 }
		private void ruleForSelectionRollbackOnChecked( RuleBasedTransactionAttribute rta )
		{
			Assert.IsTrue(rta.RollbackOn(new SystemException()));
			Assert.IsTrue( rta.RollbackOn(new TransactionSystemException()));
		}