2019年10月16日 星期三

deadlock 相關蒐集

開始之前先補充一下,查詢專案sql使用狀況,可以用SSMS的活動監控器







接下來就可以觀察最近使用次數最多或是最耗時的sql, 進而觀察table的使用情況


---


最近上完非同步後,認識後deadlock, 多發生於呼叫非同步方法後,結果尚未回傳,, 就又用同步的方式去要結果導致 (ex. .Result)

這次在專案log發現deadlock, 檢查後發現,卻是sql 查詢時, datareader 查詢較久導致, 所以找一些相關文獻如下, 目前推測的狀況應該就是sql查詢使用到平行查詢,才導致deadlock情況

做個紀錄

目前看到幾個解決方式 :

1.修正SQL
2.不使用平行執行計畫。
3.maxdop=1。
4.用try catch(Error 1205 is a deadlock)預防處理死結。
等等


參考內容 :
[SQL Server][Deadlock]Intra-Query Parallel Thread Deadlocks初體驗
https://dotblogs.com.tw/stanley14/2017/07/22/intra-queryparallelthreaddeadlocks

[SQL Server][DeakLock]觀察死結的工具(四)擴充事件(Extended events)
https://dotblogs.com.tw/stanley14/2017/01/28/191015


限定資料表僅能用 Table Lock
https://byronhu.wordpress.com/2011/05/18/%e9%99%90%e5%ae%9a%e8%b3%87%e6%96%99%e8%a1%a8%e5%83%85%e8%83%bd%e7%94%a8-table-lock/

[SQL SERVER]內部平行查詢死結特性
https://dotblogs.com.tw/ricochen/2016/08/25/191618

2019年9月25日 星期三

[angularJs] popup 使用 : angular-ui

使用 : angular-ui

預覽參考 : https://plnkr.co/edit/?p=preview

1. 加入ui-bootstrap-tpls-2.5.0.min.js
2. module include ui.bootstrap
3. 加入$uibModal
4. 建立fn  openMyModalView
5. 建立template controller

<script type="text/ng-template" id="DetailView.html">
    <div class="modal-header">
        <h3 class="modal-title" id="modal-title">I'm a modal!</h3>
    </div>
    <div class="modal-body" id="modal-body">
        <ul>
            <li ng-repeat="item in ctrl.items">
                <a href="#" ng-click="$event.preventDefault(); $ctrl.selected.item = item">{{ item }}</a>
            </li>
        </ul>
        Selected: <b>{{ ctrl.selected.item }}</b>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="ctrl.ok()">OK</button>
        <button class="btn btn-warning" type="button" ng-click="ctrl.cancel()">Cancel</button>
    </div>
</script>
    <script>
    var DefaultApp = angular.module("Default", ['ngTable','ui.bootstrap']);
    DefaultApp.controller('RegistrationController',
        ['$scope', '$http', 'NgTableParams', '$uibModal', function ($scope, $http, NgTableParams, $uibModal) {
            console.log("test12")
 
            var self = this;
            self.List = @Html.Raw(ViewBag.Registration);
            console.log(self.List);
 
            //初始化
            self.Init = function()
            {
                self.tableParams = new NgTableParams({ count: 50 }, { dataset: self.List });
            }
            self.Init();
 
             //方法
            // ref : https://angular-ui.github.io/bootstrap
            self.openMyModalView = function (item) {
                var modalInstance = $uibModal.open({
                    templateUrl: 'DetailView.html',
                    controller: 'UserDetailCtrl as ctrl',
                    resolve: {
                        item: function () {
                            return item;
                        }
                    }
                });
 
                modalInstance.result.then(function (obj) {
                    console.log(obj)
                }, function () {
                    $log.info('modal-component dismissed at: ' + new Date());
                });
            };
 
            self.SaveHandWrite = function () {
                console.log(self.Edit);
            }
    }]
);
    //popup 事件
    angular.module('Default').controller('UserDetailCtrl', function ($uibModalInstance, item) {
        console.log(item);
        var $ctrl = this;
 
        item.Name = "test";
 
        $ctrl.ok = function () {
            $uibModalInstance.close(item);
        };
 
        $ctrl.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
    </script>
 
 

2019年9月12日 星期四

[C#] SYSTEM.IO 常用到的檔案存取

以下列出一些平常會用到的操作資料方法

git : https://raw.githubusercontent.com/todomato/CSharp_IO/master/Program.cs

       static void Main(string[] args)
    {
 
        //取得Bin資料夾
        var bin = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("file:///", "");
        var path = Path.Combine(Path.GetDirectoryName(bin),"Demo");
        //判斷資料夾位置
        if (!Directory.Exists(path))
        {
            //建立資料夾
            Directory.CreateDirectory(path);
        }
 
        #region 文件相關
        Console.WriteLine(Path.GetDirectoryName(path));     //取得資料夾稱, D:\TestDir
        Console.WriteLine(Path.Combine(new string[] { @"D:\", "BaseDir", "SubDir", "TestFile.txt" })); //產生路徑
        #endregion
 
        //判斷檔案位置
        var filename = "test.txt";
 
        //結合路徑
        var filePath = Path.Combine(path, filename);
 
        //判斷資料夾位置
        if (!File.Exists(filePath))
        {
            //建立檔案
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                AddText(fs, "This is some text");
                AddText(fs, "This is some more text,");
                AddText(fs, "\r\nand this is on a new line");
                AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");
            }
        }
 

2019年8月26日 星期一

[Rgx] 推薦好用且簡單的正規表示 (.+)

平常會有很不少例行重複的工作

例如.

public string AA {get;set;}
public string BBB {get;set;}
public string CCCCC {get;set;}
....

假設你需要複製類似上述,但又遇到欄位長度不一致, 可以試試正規表示式,事半功倍

範例: 有一堆網址,我想要全部加入html 的<a>標籤,且名稱也是網址名稱

該如何處理?

AA.com
BBB.com
CCCC.com
DD.com
E.com
FF.com
GGGGG.com

完成後











2019年8月7日 星期三

[C#] console 實現全域變數



簡單的方式就是實做一個class, 裡面宣告一個靜態(static)的欄位即可達到共用變數效果

public class Global
{
    public static string _content = "";
}

2019年8月5日 星期一

[PDF] Itextsharp rotate 轉置角度


最近遇到的問題 : 合併多筆PDF文件,但內容裡面有轉90度跟270度怎麼辦?


第一種,使用pdfReader直接轉, 但如果後續還要進行頁碼等處理則不建議

主要就這句 page.Put(PdfName.ROTATE, new PdfNumber(rotation));

提供rotation 要旋轉的角度

而我有一個參數rotations只是有事先記錄該pdf本身選轉的角度

可以直接使用int rotation = rotate == null ? 90 : (rotate.IntValue + 90) % 360;

替代即可

public void RotatePDFFiles_bak(string fileList, string outMergeFile, List<int> rotations)
{
     PdfReader reader = new PdfReader(fileList);
    int pagesCount = reader.NumberOfPages;
 
    for (int n = 1; n <= pagesCount; n++)
    {
         PdfDictionary page = reader.GetPageN(n);
         PdfNumber rotate = page.GetAsNumber(PdfName.ROTATE);
        //int rotation = rotate == null ? 90 : (rotate.IntValue + 90) % 360;
        int rotation = 0;
        if (rotations[n - 1] == 90)
        {
            rotation = 180; //(90 + 90) % 360; //向左橫置的pdf逆轉90
        }
        else if (rotations[n - 1] == 270)
        {
            rotation = 0; // (270 + 90) % 360;
        }
        page.Put(PdfName.ROTATE, new PdfNumber(rotation));
    }
 
    PdfStamper stamper = new PdfStamper(reader, new FileStream(outMergeFile, FileMode.Create));
    stamper.Close();
    reader.Close();
}


2019年8月2日 星期五

[C#] MVC 下載檔案

利用session 暫存檔案 然後讓html 呼叫
注意這是有缺點的,不建議拿來存放大檔案會出現OOM
建議還是直接使用<a  href="" downloads> 直接下載


action1

var pdfFile = "xxx.pdf";
FileContentResult file = File(pdfFile, "application/vnd.ms-excel");
Session["pdfFile"] = file; // 將檔案儲存在 Session