Exemplo n.º 1
0
        public HashGroup QueryByNameSync(string name)
        {
            HashGroup group = null;

            //获取只读连接字符串
            var strConn = _hashConnectionFactory.CreateReadForHash();


            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, strConn, (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = string.Format(@"select {0},{1} from HashGroup as g join HashGroupStrategy as s 
                                                  on g.strategyid=s.id
                                                  where g.[name]=@name", StoreHelper.GetHashGroupSelectFields("g"), StoreHelper.GetHashGroupStrategySelectFields("s"))
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.VarChar, 100)
                    {
                        Value = name
                    };
                    commond.Parameters.Add(parameter);

                    commond.Prepare();

                    SqlDataReader reader = null;

                    using (reader = commond.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            group = new HashGroup();
                            StoreHelper.SetHashGroupSelectFields(group, reader, "g");
                            group.Strategy = new HashGroupStrategy();
                            StoreHelper.SetHashGroupStrategySelectFields(group.Strategy, reader, "s");
                        }

                        reader.Close();
                    }
                }
            });

            return(group);
        }
Exemplo n.º 2
0
        public async Task <ClaimContextGenerator> QueryByName(string name)
        {
            ClaimContextGenerator generator = null;

            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, _contextConnectionFactory.CreateReadForContext(), async(conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"select {0} from ClaimContextGenerator where [name]=@name", StoreHelper.GetClaimContextGeneratorSelectFields(string.Empty)),
                    Transaction = sqlTran
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.VarChar, 150)
                    {
                        Value = name
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();


                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            generator = new ClaimContextGenerator();
                            StoreHelper.SetClaimContextGeneratorSelectFields(generator, reader, string.Empty);
                        }

                        await reader.CloseAsync();
                    }
                }
            });


            return(generator);
        }
Exemplo n.º 3
0
        public SystemConfiguration QueryByName(string name)
        {
            SystemConfiguration configuration = null;

            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, _dbConnectionMainFactory.CreateReadForSystemConfiguration(), (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"select {0} from SystemConfiguration where [name]=@name", StoreHelper.GetSystemConfigurationSelectFields(string.Empty)),
                    Transaction = sqlTran
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.VarChar, 100)
                    {
                        Value = name
                    };
                    commond.Parameters.Add(parameter);

                    commond.Prepare();


                    SqlDataReader reader = null;

                    using (reader = commond.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            configuration = new SystemConfiguration();
                            StoreHelper.SetSystemConfigurationSelectFields(configuration, reader, string.Empty);
                        }

                        reader.Close();
                    }
                }
            });


            return(configuration);
        }
        public async Task <EnvironmentClaimGenerator> QueryByID(Guid id)
        {
            EnvironmentClaimGenerator generator = null;

            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, _contextConnectionFactory.CreateReadForContext(), async(conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"select {0} from EnvironmentClaimGenerator where [id]=@id", StoreHelper.GetEnvironmentClaimGeneratorSelectFields(string.Empty)),
                    Transaction = sqlTran
                })
                {
                    var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                    {
                        Value = id
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();


                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            generator = new EnvironmentClaimGenerator();
                            StoreHelper.SetEnvironmentClaimGeneratorSelectFields(generator, reader, string.Empty);
                        }

                        await reader.CloseAsync();
                    }
                }
            });


            return(generator);
        }
Exemplo n.º 5
0
        public RedisClientFactory QueryByNameSync(string name)
        {
            RedisClientFactory factory = null;

            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, _redisConnectionFactory.CreateReadForRedisClientFactory(), (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand command = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"SELECT {0} FROM [RedisClientFactory] WHERE name=@name;", StoreHelper.GetRedisClientFactoryStoreSelectFields(string.Empty)),
                    Transaction = sqlTran
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.VarChar, 150)
                    {
                        Value = name
                    };
                    command.Parameters.Add(parameter);
                    command.Prepare();
                    SqlDataReader reader = null;

                    using (reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            factory = new RedisClientFactory();
                            StoreHelper.SetRedisClientFactoryStoreSelectFields(factory, reader, string.Empty);
                        }
                        reader.Close();
                    }
                }
            });
            return(factory);
        }
Exemplo n.º 6
0
        public ApplicationLock QueryByNameSync(string name)
        {
            ApplicationLock aLock = null;

            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, _distributeConnectionFactory.CreateReadForDistributeCoordinator(), (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand command = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"SELECT {0} FROM [ApplicationLock] WHERE name=@name;", StoreHelper.GetApplicationLockStoreSelectFields(string.Empty)),
                    Transaction = sqlTran
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.VarChar, 150)
                    {
                        Value = name
                    };
                    command.Parameters.Add(parameter);
                    command.Prepare();
                    SqlDataReader reader = null;

                    using (reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            aLock = new ApplicationLock();
                            StoreHelper.SetApplicationLockStoreSelectFields(aLock, reader, string.Empty);
                        }
                        reader.Close();
                    }
                }
            });
            return(aLock);
        }
Exemplo n.º 7
0
        public void UnLockSync(DBConnectionNames connNames, string lockName)
        {
            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateAllForApplicationLock(connNames, lockName),
                                                   (conn, transaction) =>
            {
                string strSql = string.Format(@"declare @result int,@resource nvarchar(300),@message nvarchar(300)
                                      set @result = 0;
		                              set @resource='{0}';
                                      begin

		                                begin try
			                                EXEC @result = sp_releaseapplock @Resource = @resource
		                                end try
		                                begin catch
		                                end catch

                                        if @result<0
		                                    begin
			                                    set @message=N'applock解锁失败,失败码:'+convert(nvarchar(20),@result)+N',详细信息:'+ERROR_MESSAGE();
			                                    throw 50001,@message,1
		                                    end
                                       end
                            
                        ", lockName);

                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand command = new SqlCommand())
                {
                    command.CommandTimeout = 300;
                    command.Connection     = (SqlConnection)conn;
                    command.CommandType    = CommandType.Text;
                    command.CommandText    = strSql;
                    command.Transaction    = sqlTran;
                    command.Prepare();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch (SqlException ex)
                    {
                        if (ex.Number == 50001)
                        {
                            var fragment = new TextFragment()
                            {
                                Code = TextCodes.ExistLicationLock,
                                DefaultFormatting = "当前请求{0}已被锁定",
                                ReplaceParameters = new List <object>()
                                {
                                    lockName
                                }
                            };

                            throw new UtilityException((int)Errors.ExistLicationLock, fragment);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
                                                   );
        }
Exemplo n.º 8
0
        public void LockSync(DBConnectionNames connNames, string lockName, int timeout)
        {
            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateAllForApplicationLock(connNames, lockName),
                                                   (conn, transaction) =>
            {
                string strTimeout;
                if (timeout == -1)
                {
                    strTimeout = "null";
                }
                else
                {
                    strTimeout = timeout.ToString();
                }
                string strSql = string.Format(@"declare @result int,@resource nvarchar(300),@timeout int,@message nvarchar(300)
                                      set @result = 0;
		                              set @resource='{0}';
                                      set @timeout={1}
                                      begin
			                            if @@TRANCOUNT>0
		                                    begin
			                                    if @timeout is null
				                                    EXEC @result = sp_getapplock @Resource = @resource, 
					                                @LockMode = 'Exclusive'
			                                    else
					                                EXEC @result = sp_getapplock @Resource = @resource, 
					                                @LockMode = 'Exclusive',@LockTimeout=@timeout			
		                                    end
                                        else
		                                    begin
			                                    if @timeout is null
				                                    EXEC @result = sp_getapplock @Resource = @resource, 
					                                @LockMode = 'Exclusive',@LockOwner='Session'
			                                    else
					                                EXEC @result = sp_getapplock @Resource = @resource, 
					                                @LockMode = 'Exclusive',@LockOwner='Session',@LockTimeout=@timeout			
		                                    end	

                                        if @result<0
		                                    begin
			                                    set @message=N'applock加锁失败,失败码:'+convert(nvarchar(20),@result)+',详细信息:'+ERROR_MESSAGE();
			                                    throw 50001,@message,1
		                                    end
                                       end
                            
                        ", lockName, strTimeout);

                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand command = new SqlCommand())
                {
                    command.CommandTimeout = 300;
                    command.Connection     = (SqlConnection)conn;
                    command.CommandType    = CommandType.Text;
                    command.CommandText    = strSql;
                    command.Transaction    = sqlTran;
                    command.Prepare();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    catch (SqlException ex)
                    {
                        if (ex.Number == 50001)
                        {
                            var fragment = new TextFragment()
                            {
                                Code = TextCodes.ExistLicationLock,
                                DefaultFormatting = "当前请求{0}已被锁定",
                                ReplaceParameters = new List <object>()
                                {
                                    lockName
                                }
                            };

                            throw new UtilityException((int)Errors.ExistLicationLock, fragment);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
                                                   );
        }
Exemplo n.º 9
0
        public QueryResult <EntityAttributeMapConfigurationDetail> QueryByPageSync(Guid configurationId, int page, int pageSize)
        {
            QueryResult <EntityAttributeMapConfigurationDetail> result = new QueryResult <EntityAttributeMapConfigurationDetail>();

            DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForEntityAttributeMapConfiguration(), (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = @"set @currentpage=@page
		                                select @count= count(*) from [dbo].[EntityAttributeMapConfigurationDetail] where configurationid=@configurationid
		                                if @pagesize*@page>=@count
			                                begin
				                                set @currentpage= @count/@pagesize
				                                if @count%@pagesize<>0
					                                begin
						                                set @currentpage=@currentpage+1
					                                end
				                                if @currentpage=0
					                                set @currentpage=1
			                                end
		                                else if @page<1 
			                                begin 
				                                set @currentpage=1
			                                end 

		                                declare @columns nvarchar(500),@prefix nvarchar(20)
		                                set @prefix=''
		                                set @columns=dbo.core_GetEntityAttributeMapConfigurationDetailQueryColumns(@prefix)

		                                select @prefix as prefix

		                                declare @execsql nvarchar(1000)
		                                set @execsql='select '+@columns+' from [dbo].[EntityAttributeMapConfigurationDetail]WITH (SNAPSHOT) where configurationid=@configurationid '
		                                +'order by [createtime] desc '
		                                +'offset  ( @pagesize * ( @currentpage - 1 )) rows '
		                                +'fetch next @pagesize rows only;'
		
		                                exec sp_executesql @execsql, N'@configurationid uniqueidentifier,@pagesize int,@currentpage int',@configurationid, @pagesize,@currentpage   "            ,
                    Transaction = sqlTran
                })
                {
                    var parameter = new SqlParameter("@page", SqlDbType.Int)
                    {
                        Value = page
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@pagesize", SqlDbType.Int)
                    {
                        Value = pageSize
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@configurationid", SqlDbType.UniqueIdentifier)
                    {
                        Value = configurationId
                    };
                    commond.Parameters.Add(parameter);


                    parameter = new SqlParameter("@count", SqlDbType.Int)
                    {
                        Direction = ParameterDirection.Output
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@currentpage", SqlDbType.Int)
                    {
                        Direction = ParameterDirection.Output
                    };
                    commond.Parameters.Add(parameter);

                    commond.Prepare();

                    SqlDataReader reader = null;

                    using (reader = commond.ExecuteReader())
                    {
                        string prefix = string.Empty;
                        if (reader.Read())
                        {
                            prefix = reader["prefix"].ToString();
                        }

                        if (reader.NextResult())
                        {
                            while (reader.Read())
                            {
                                var detail = new EntityAttributeMapConfigurationDetail();
                                StoreHelper.SetEntityAttributeMapConfigurationDetailSelectFields(detail, reader, prefix);

                                result.Results.Add(detail);
                            }
                        }

                        reader.Close();

                        result.TotalCount  = (int)commond.Parameters["@count"].Value;
                        result.CurrentPage = (int)commond.Parameters["@currentpage"].Value;
                    }
                }
            });

            return(result);
        }
Exemplo n.º 10
0
        public async Task QueryByType(string type, Func <HashGroup, Task> action)
        {
            List <HashGroup> result = new List <HashGroup>();
            int? index = null;
            long count = 500;

            //获取只读连接字符串
            var strConn = _hashConnectionFactory.CreateReadForHash();

            while (true)
            {
                result.Clear();


                DBTransactionHelper.SqlTransactionWork(DBTypes.SqlServer, true, false, strConn, (conn, transaction) =>
                {
                    SqlTransaction sqlTran = null;
                    if (transaction != null)
                    {
                        sqlTran = (SqlTransaction)transaction;
                    }

                    string strSql = string.Format(@"select top(@top) {0},{1} from HashGroup as g join HashGroupStrategy as s 
                                                  on g.strategyid=s.id
                                                  where g.[type]=@type order by g.[sequence]", StoreHelper.GetHashGroupSelectFields("g"), StoreHelper.GetHashGroupStrategySelectFields("s"));
                    if (index != null)
                    {
                        strSql = string.Format(@"select top(@top) {0},{1} from HashGroup as g join HashGroupStrategy as s 
                                                  on g.strategyid=s.id
                                                  where g.[type]=@type and g.[sequence]>@index order by g.[sequence]", StoreHelper.GetHashGroupSelectFields("g"), StoreHelper.GetHashGroupStrategySelectFields("s"));
                    }

                    using (SqlCommand commond = new SqlCommand()
                    {
                        Connection = (SqlConnection)conn,
                        CommandType = CommandType.Text,
                        Transaction = sqlTran,
                        CommandText = strSql
                    })
                    {
                        var parameter = new SqlParameter("@top", SqlDbType.Int)
                        {
                            Value = count
                        };
                        commond.Parameters.Add(parameter);

                        parameter = new SqlParameter("@type", SqlDbType.VarChar, 100)
                        {
                            Value = type
                        };
                        commond.Parameters.Add(parameter);

                        if (index != null)
                        {
                            parameter = new SqlParameter("@index", SqlDbType.BigInt)
                            {
                                Value = index
                            };
                            commond.Parameters.Add(parameter);
                        }

                        commond.Prepare();

                        SqlDataReader reader = null;

                        using (reader = commond.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                var group = new HashGroup();
                                StoreHelper.SetHashGroupSelectFields(group, reader, "g");
                                group.Strategy = new HashGroupStrategy();
                                StoreHelper.SetHashGroupStrategySelectFields(group.Strategy, reader, "s");
                                result.Add(group);
                            }

                            reader.Close();
                        }
                    }
                });



                foreach (var item in result)
                {
                    await action(item);
                }

                if (result.Count < count)
                {
                    break;
                }
            }
        }