<commit_before>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2014-2015 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;

namespace Zongsoft.Transactions
{
	public class Transaction : IDisposable, IEquatable<Transaction>
	{
		#region 常量定义
		internal const int OPERATION_NONE = 0;
		internal const int OPERATION_ROLLBACK = 1;
		internal const int OPERATION_ABORT = 2;
		#endregion

		#region 静态字段
		[ThreadStatic]
		private static Transaction _current;
		#endregion

		#region 私有变量
		private int _operation;
		private Queue<IEnlistment> _enlistments;
		#endregion

		#region 成员字段
		private int _isCompleted;
		private Transaction _parent;
		private TransactionBehavior _behavior;
		private IsolationLevel _isolationLevel;
		private TransactionStatus _status;
		private TransactionInformation _information;
		#endregion

		#region 构造函数
		public Transaction() : this(TransactionBehavior.Required, IsolationLevel.ReadCommitted)
		{
		}

		public Transaction(IsolationLevel isolationLevel) : this(TransactionBehavior.Required, isolationLevel)
		{
		}

		public Transaction(TransactionBehavior behavior, IsolationLevel isolationLevel)
		{
			_status = TransactionStatus.Active;
			_behavior = behavior;
			_isolationLevel = isolationLevel;

			switch(behavior)
			{
				case TransactionBehavior.Followed:
				case TransactionBehavior.Required:
					//如果当前环境事务为空，则将当前事务置为环境事务
					if(_current == null)
						_current = this;
					else
						_parent = _current;

					break;
				case TransactionBehavior.RequiresNew:<commit_msg>:package:  增加了一个构造函数重载，并修改参数的默认值。 :goat:<commit_after>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2014-2015 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.CoreLibrary.
 *
 * Zongsoft.CoreLibrary is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.CoreLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.CoreLibrary; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;

namespace Zongsoft.Transactions
{
	public class Transaction : IDisposable, IEquatable<Transaction>
	{
		#region 常量定义
		internal const int OPERATION_NONE = 0;
		internal const int OPERATION_ROLLBACK = 1;
		internal const int OPERATION_ABORT = 2;
		#endregion

		#region 静态字段
		[ThreadStatic]
		private static Transaction _current;
		#endregion

		#region 私有变量
		private int _operation;
		private Queue<IEnlistment> _enlistments;
		#endregion

		#region 成员字段
		private int _isCompleted;
		private Transaction _parent;
		private TransactionBehavior _behavior;
		private IsolationLevel _isolationLevel;
		private TransactionStatus _status;
		private TransactionInformation _information;
		#endregion

		#region 构造函数
		public Transaction() : this(TransactionBehavior.Followed, IsolationLevel.ReadCommitted)
		{
		}

		public Transaction(IsolationLevel isolationLevel) : this(TransactionBehavior.Followed, isolationLevel)
		{
		}

		public Transaction(TransactionBehavior behavior) : this(behavior, IsolationLevel.ReadCommitted)
		{
		}

		public Transaction(TransactionBehavior behavior, IsolationLevel isolationLevel)
		{
			_status = TransactionStatus.Active;
			_behavior = behavior;
			_isolationLevel = isolationLevel;

			switch(behavior)
			{
				case TransactionBehavior.Followed:
				case TransactionBehavior.Required:
					//如果当前环境事务为空，则将当前事务置为环境事务
					if(_current == null)
						_current = this;
					else
						_parent = _current;

					break;
				case TransactionBehavior.RequiresNew:
{'fraction_non_alphanumeric': 0.06737987987987988, 'fraction_numerical': 0.009009009009009009, 'mean_word_length': 4.297216699801193, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4017360', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1543, 'n_words': 522}