SQLServer 的樹狀結構寫法

參考網址

1. 建立樹狀結構範例 TABLE

CREATE TABLE tblDepartments(
DepartmentID int,
DepartmentName varchar(50),
ParentDepartmentID int,
constraint tblDepartments_P0 primary key (DepartmentID)
)


2. 建立測試資料
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(1, 'HR', NULL);
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(2, 'IT', NULL);
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(3, 'Networking', 2);
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(4, 'Development', 2);
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(5, 'ASP.NET Development', 4);
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(6, 'J2EE Development', 4);
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(7, 'C#.NET', 5);
INSERT INTO tblDepartments(DepartmentID, DepartmentName, ParentDepartmentID) VALUES(8, 'VB.NET', 5);



3. 寫出樹狀結構 SQL (若 Branch 為 'Y' 表示是別人的父親(分支))
WITH Hierarchy(DepartmentID, DepartmentName, ParentDepartmentID, HLevel)
AS
(
SELECT DepartmentID
,DepartmentName
,ParentDepartmentID
,0 HLevel
FROM tblDepartments
WHERE ParentDepartmentID is Null
UNION ALL
SELECT SubDepartment.DepartmentID
,SubDepartment.DepartmentName
,SubDepartment.ParentDepartmentID
,HLevel + 1
FROM tblDepartments SubDepartment INNER JOIN Hierarchy ParentDepartment
ON SubDepartment.ParentDepartmentID = ParentDepartment.DepartmentID
)
SELECT M.DepartmentID
,DepartmentName = Replicate('.', M.HLevel) + M.DepartmentName
,CASE
WHEN D.ParentDepartmentID IS NULL THEN 'N'
ELSE 'Y'
END Branch
,M.HLevel
,M.ParentDepartmentID
FROM Hierarchy M Left Outer Join
(SELECT DISTINCT ParentDepartmentID FROM Hierarchy WHERE ParentDepartmentID is Not Null) D
ON M.DepartmentID = D.ParentDepartmentID

在 IE 執行剪貼簿功能

原文網址




如果你想讓使用者按下一個按鈕就把整段文字的內容填入剪貼簿,那麼你或許會想要使用如下的 JavaScript 指令:

CopiedTxt = document.getElementById('myText").createRange();
CopiedTxt.execCommand(”Copy”);

很不幸的,在 IE 瀏覽器中可能不能執行這段指令,它會指出 createRange 這道指令「物件不支援此屬性或方法」。事實上,你光從 Visual Studio 2008 中 JavaScript 的 Intellisense 下面已經找不到物件的 select()、createRange() 或 execCommand() 等方法了,這表示上述方法都並不受到支援。

對於 IE 瀏覽器,你應該使用 JavaScript 的 window.clipboardData.setData() 指令,如下例:

 

<script type="text/javascript" language="javascript">
        function CopyToClipboard() {
            var ite = document.getElementById('myGridView’);
            ite.focus();
            window.clipboardData.setData('Text', ite.innerHTML);
        }
</script>
...
<input name="btnCopyToClipboard" type="button" value="複製" onclick="CopyToClipboard();" />
<div id="myGridView">
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" ShowHeader="true"
        ...
</div>


在這個範例中,當使用者按下「複製」按鈕後,程式會把 GridView 控制項內容完整的複製到剪貼簿裡面去,以方便他/她再貼到 Word 或 Excel 裡。

那麼,如果你是想把剪貼簿裡的資料取出來,那麼可以使用 JavaScript 的 window.clipboardData.getData('Text') 指令。

Windows XP 鎖定電腦的功能那哪裡?

原文網址

Windows XP 若要使用「鎖定電腦」的功能,登入方式就需改為傳統的登入方式(即同時按 CTRL、ALT、DEL)
若使用 XP新創的「歡迎畫面」這一種登入模式的話, 同時按 CTRL、ALT、DEL 就沒有鎖定電腦這一種功能,而是直接叫出工作管理員。

如何變更登入模式?如何取消「使用歡迎畫面」?
* 按「開始」,到「控制台」
*「使用者帳戶」,進入「變更使用者登入或登出的方式」
* 不勾「使用歡迎畫面」,按套用選項。

關閉使用者帳戶視窗、關閉控制台。
現在同時按下 CTRL、ALT、DEL 三鍵,這時就會蹦出如同 Windows 2000 下的 Windows 安全視窗,按「鎖住電腦」鈕,就可以鎖定電腦。

中文的免費還原光碟軟體

本篇文章不知道出處,知道的人請告訴偶,偶再補上企..

分享一個製作中文免費版還原光碟的系統~

Clonezilla - 再生龍還原系統
免費開放原始碼的還原系統,可製作免費還原光碟哦(內建有中文介面)

http://drbl.nchc.org.tw/clonezilla/

大量部署作業系統的時候(不管是微軟Windows或是Linux),用 Norton Ghost
製作成印象擋再複製到每台用戶端是現行最常使用的方法。然而適合用來做大量複製的
Symantec Ghost Corporate
Edition 需要不少的費用,而且電腦越多,費用越高。此外,因為它不是自由軟體,所以靈活度不高,不能自己客製化修改。若是使用
Norton Ghost 又必須一台台的做, 頗為曠時費日。
再生龍Clonezilla提供您一個完整的環境,這是一個整合的系統,包含還原程式以及作業系統。搭配網路開機功能,你可以忘了軟碟或是光碟...
以國家高速網路與計算中心C教室為例,共有41台機器同時進行還原的動作,還原一個5.6
GBytes的微軟Windows XP
image到41台客戶端電腦,以群播(multicast)的方式處理,只要10分鐘左右(若採用unicast的話,約需50分鐘)。

使用Clonezilla Live的理由
提到備份還原軟體,最有名的是賽門鐵克的GHOST,就連許多品牌電腦的還原光碟也是用GHOST來製作的,很多人可能想,我會用GHOST就好了,為什麼要用Clonezilla
Live呢?

我知道GHOST是目前大多數人的選擇,寫病毒的人也知道,去年在大陸肆虐的熊貓燒香病毒就會把硬碟中的GHOST備份檔刪除,許多受害者中毒了系統掛掉,要還原時找不到檔案欲哭無淚…

一、再生龍特點:
自由軟體,國網中心所開發。
可還原再生多種作業系統,包含Linux (ext2, ext3, reiserfs, xfs, jfs), 微軟Windows
(fat, ntfs)
支援GNU/Linux下的LVM2 (尚未支援LVM1)
支援群播(Multicast)。配合PXE網路開機,搭配DRBL的Clonezilla可以使用播(multicast)的方式,適合用來大備份與還原。硬體設備功能足夠時(用戶端支援Wake
on LAN與PXE),可以遠端操作,人不需到現場
不同於Partimage或是ntfsclone只能處理分割區,Clonezilla包含其他程式的撰寫與整合,因此可以備份與複製分割區或是整顆硬碟
Clonezilla對於支援的檔案系統(ext2, ext3, reiserfs, xfs, jfs, fat,
ntfs),只備份有存資料的硬碟空間,因此可以節省備份時間與硬碟空間。其他不支援的檔案系統Clonezilla採用dd的方式處理。
透過本實驗室開發的另外一套自由軟體drbl-winroll,可以指定還原後微軟Windows的主機名稱、工作群組,亦可寫入隨機的SID。
另有單機版免安裝的還原系統Clonezilla Live可以使用
二、再生龍的安裝與使用:


http://drbl.nchc.org.tw/clonezilla/clonezilla-live/


Clonezilla Live對Windows系統的備份與還原

http://hc.cyc.edu.tw/exe/clonezilla_live/

免安裝的單機版還原系統
說明:適合單台電腦備份或是還原
一、版權說明
本文版權屬於全體作者所有,且以 GNU FDL 授權合約發佈。在遵守 GNU FDL
授權合約的情況下,您可以複製、散佈,以及修改本文件。唯請保持作者列表的完整,若您修改過此份文件,也請在作者列表中加入您的大名。
同時請注意本文件沒有任何擔保,若因本文件而產生任何直接或間接的損失,本文作者群均不負任何法律及道義上的責任。

二、作者列表
蕭志榥 (steven _at_ nchc org tw)
黃國連 (klhaung _at_ gmail com)
王順泰 (c00wht00 _at_ nchc org tw)
孫振凱 (ceasar _at_ nchc org tw)
王耀聰 (jazzwang _at_ nchc org tw)
蔡育欽 (thomas _at_ nchc org tw)

三、Clonezilla Live說明
搭配DRBL使用的Clonezilla乃是適合來大量部署作業系統的時候使用,不過這樣的限制是一定要有一台DRBL伺服器,而且要被備份的電腦一定要能網路開機。由於GNU/Linux
Live CD的成熟,將Clonezilla和GNU/Linux Live
CD結合,達成免安裝就能備份還原單台電腦就非常實用。因此,本實驗室結合Debian
Live與Clonezilla,另外開發一個Clonezilla Live套件,專門就是用來單台電腦的還原與備份。Clonezilla
Live可以放在光碟上,或者USB隨身碟等開機媒體。當然,Clonezilla
live這樣的環境就不適合用來大量還原一個電腦教室,而且所能存的印象檔也可能會受限於開機媒體的儲存空間的大小。不過,如果要被備份的電腦可以連接網路,透過網路磁碟機,例如sshfs或是Samba(網路芳鄰),開機媒體的儲存空間大小的限制就可以打破,我們可以直接把印象檔的存取放在網路磁碟機上。

四、如何製作Clonezilla Live
三個方法:
方法一:直接下載來燒到光碟片或是放到USB隨身碟上。
方法二:透過Clonezilla的相關程式來產生Clonezilla
live,把它放在光碟片或是USB隨身碟上,你也可以把一個已經存在Clonezilla伺服器上的image直接透過程式,塞到Clonezilla
live上,這樣,直接用那片光碟片或是隨身碟就可以還原某台機器。
方法三:自己客製化的程式運用在clonezilla live中。

下載點:光碟版和usb版
http://drbl.nchc.org.tw/clonezilla/

製作還原光碟方法:http://drbl.nchc.org.tw/clonezilla/related_article/006_Clonezilla_live_recreate/Tsai_clonezilla_live_recreate_960926.pdf

系統路徑變數

轉貼網址
這在寫批次檔時非常有用,特別是如果系統不是安裝在 C: 時,指定安裝路徑就全部靠這個了。

以下變數在 Win2000 與 WinXP 皆可適用。

系統相關:
%SystemDrive% > C:\
%HomeDrive% > C:\ (同上)
%SystemRoot% > C:\Windows\
%Windir% > C:\Windows\ (同上)
%ProgramFiles% > C:\Program Files\

使用者相關:
%HomePath% > C:\Documents and Settings\user\
%UserProfile% > C:\Documents and Settings\user\ (同上)
%AppData% > C:\Documents and Settings\user\Application Data\
%Temp% > C:\Documents and Settings\user\Local Settings\Temp\
%AllUsersProfile% > C:\Documents and Settings\All Users\

如何使用一個 Public IP ,對應內部兩台 Web Server

有人問偶這個問題,基本需求如下:
1. 只有一個 Public IP
2. Private 有兩台 Web Server ServerA:Linux+Apache ServerB:Windows+IIS
3. 有個簡單的Firewall ,可將 Public Port 對應至 Private IP
3. 不想再多增加 Public IP 或申請網路

以下是我想到的解決方法:
1. ServerA 的 Apache 啟用 VirtualHost ,VirtualHost_A 和 VirtualHost_B
2. Firewall 啟用 Port 對應,80 Port 對應 VirtualHost_A ,8080 Port 對應 VirtualHost_B
3. 在 VirtualHost_B 放個 Redirect 指令...重導至Public IP :8080

PS. 若不喜歡看到網址後面有 :8080,在 Redirect 指令用一個 Frame 包起來,不過不建議這樣做

Oracle 時間加1天/ 1分鐘

sysdate+1 加一天
sysdate+1/24 加1小時
sysdate+1/(24*60) 加1分鐘
sysdate+1/(24*60*60) 加1秒鐘

ORACLE Tips: 這事是誰做的? 快點過來自首吧!

雖然是粉久以前的文章,不過非常具有參考價值,也非常精采。
原文網址

By Tommy Wu

我想各位都應該有那種資料被改了或被砍了, 但是沒有人要承認是他做的, 或者是那個程式設
計師不小心寫錯程式造成的, 這時, 老闆就會問你, 這到底是誰做的?

以前, 為了這個問題, 我們可能會在程式中加上一堆稽核的記錄, 記下一堆又臭又長的資料,
但是真的發生問題時, 找起來又不一定找的到罪魁禍首... 我記得每次用到最後, 都會把這部
份的功能關掉, 因為實在是沒什麼作用.

現在, 利用 ORACLE 所提供的 LOGMNR 的功能, 我們可以輕易的找到兇嫌了.

ORACLE 為了要做到 crash 時, 能夠由備份資料回復到之前的狀態, 所以對於每一筆交易, 都
會有記錄. 這些會存放在所謂的 archived log 之中, 在以前, 這個 log 的檔案, 只能用來做
為 recovery 使用. 但是現在, 我們也可以透過 ORACLE 所提供的功能來查詢裡面的內容, 這
個功能在 ORACLE 中叫做 LOGMNR.

要做這件事, 我們需要保留兩種檔案, 第一種當然是 archived log, 因為所有的交易記錄都放
在這類的檔案中. 另一種就是字典檔, 因為在 log 中並不存放物件的名稱, 為了要讓產生的資
料容易讀取, 必須要有字典檔來做對應.

Archived log 會隨著系統的交易而增加, 所以你必須決定要保留多久, 這個視你的需求而定,
以我們這兒為例, 原本保留一個月, 現在改成兩個月, 當然, 只要你的硬碟空間足夠, 要留多
久就可以留多久.

字典檔就必須自己產生, 有兩種方式, 一種是存到 ORACLE 的 control 檔案中, 一種是存放
指定的檔案中. 為了日後的查詢方便, 我們採用的是存到另外的檔案. 每天會產生一個新的字
典檔, 與 archived log 一樣, 保留下來, 以便於日後查詢使用.

產生字典檔的方法如下:


# 今天的日期
TODAY=`date +%Y%m%d`

sqlplus "/ as sysdba" <execute dbms_logmnr_d.build('${ORACLE_SID}_dictionary.ora', '/db/dictionary/${TODAY}',
options => dbms_logmnr_d.store_in_flat_file);
exit
EOF

上面的指令會產生一個字典檔放在 /db/dictionary/20020101 之類的路徑之中.
請注意上面的 execute 指令, 與下面那行是同一行, 請勿換行.

有了交易記錄檔與字典檔之後, 我們就可以查詢交易的內容了. 不過... 每日的交易檔可能數量是
很可觀的, 以我們這兒來說, 所定的每個交易檔的大小為 1MB, 每天約有 1000 個上下的交易檔產
生, 要找起來實在不是一件小工程. :-(

我們以下面的例子來說明:

假定我們要查的交易是在 arch0000093355.arc 這個交易記錄檔中, 就在 sqlplus 中執行下面的指
令:

execute DBMS_LOGMNR.ADD_LOGFILE('/log/arch0000093335.arc', dbms_logmnr.NEW);

如果還有其他的交易記錄檔, 如 arch0000093336.arc, 可以用下面的指令加上:

execute DBMS_LOGMNR.ADD_LOGFILE('/log/arch0000093336.arc', dbms_logmnr.ADDFILE);

依此類推, 你可以一次加上一堆記錄檔, 我通常習慣是每天的檔案都放在一起查...

把所有的交易記錄檔都加上之後, 就執行下面的指令啟動 LOGMNR 的功能:

execute dbms_logmnr.start_logmnr(DictFileName =>'/log/ORCL_dictionary.ora');

後面那個檔案就是你存下來的字典檔.
這個指令有另外的參數可以使用, 例如我們如果只要查詢有 commit 的交易時, 可以使用:

execute dbms_logmnr.start_logmnr(DictFileName =>'/log/ORCL_dictionary.ora',
options => dbms_logmnr.COMMITTED_DATA_ONLY);

請注意上面的 execute 指令, 與下面那行是同一行, 請勿換行.
其他的參數, 可以參考 ORACLE 的文件說明.

這個動作可能會很久, 等做完之後, 系統就會有一個 v$logmnr_contents 可以查詢.

v$logmnr_contents 這個 view, 比較常用的欄位有:
scn: 系統變更代碼
timestamp: 時間
username: 使用者名稱 (ORACLE 的使用者)
row_id: 指令所異動到的資料的 ROWID.
session#: 執行指令的 session 號碼.
serial#: 執行指令的 serial 號碼.
sql_redo: 執行的指令.
sql_undo: 回復的指令.
session_info: 這個 session 的資料. (可能是空的, 或許要查更早之前的 log, 看看這個
session 什麼時候登入的, 在登入後的那幾筆交易可以由這個看出那一個機
器連上來的, 使用那個 OS 的使用者.

直接使用 select 指令查詢相關的內容, 就可以得知在何時誰做了什麼事情...
例如, 有人下了 delete 指令, 我們可以查詢

select session_info, sql_redo from v$logmnr_contents
where sql_redo like 'delete%';

這個查詢會把這些交易記錄中所有 delete 開頭的指令取出來.

查詢後執行下面的指令, 結束 LOGMNR 的運作:

execute dbms_logmnr_end_logmnr();

查詢的技巧... 自己多試試就知道了, 你也可以 create 另一個 table, 把這個 view 的資料先
存過去, 再來查詢, 當資料多的時候, 可能會比較方便一些.

最後... 要提的就是, 雖然可以知道那一個使用者在那一台機器, 那個時間做了這件事, 但是實
際上... 還是有人要說不是他做的... 說可能有人用他的密碼, 在他的機器上做, 或者他開機後
離開位子, 不曉得誰過來用他的電腦... 這些事情, 已經不是電腦可以處理的了, 應該歸於行政
管理上面的要求.

請所有的使用者, 不要用太簡單的密碼, 也不要讓別人知道你的密碼, 在離開位子的時候, 也請
將電腦的畫面鎖住... 否則, 這事就算在你的頭上.


另外, 在 ORACLE 9.0.1.x 的版本中 (到 9.0.1.3 為止), 這個功能有點問題, 可能會造成你在
select 這個 view 的時候, 造成 ORACLE hang 住, 或產生 core dump. 這個問題據說已經被修
正, 但無法提供單一的 patch, 所以到等到 9.0.1.4 版本才可以使用, 或者是使用 9.2.0.1 這
個版本也可以.

我們這兒的解決方法就是裝一套 ORACLE 9.2.0.1 專門來處理這個問題.

如何手動升級 Kernel

1. 升級 gentoo-sources
# emerge -u gentoo-sources
2. 將目前的 linux link 至最新的目錄
# cd /usr/src
# ln -sfn linux-2.6.23-gentoo-r8 linux
3. 將舊的 Kernel 設定檔複製過來,才不用全部重新設定
# cd /usr/src/linux-2.6.23-gentoo-r8
# cp ../linux-2.6.23-gentoo-r6/.config ./
4. 設定 Kernel 編譯選項,記得存檔為 .config
# cd /usr/src/linux
# make menuconfig --若沒有設定檔執行這行
# make oldconfig --若用舊的設定檔執行這行
5. 編 Kernel 並安裝相關模組
# make && make modules_install
6. 將編好的 Kernel 複製到 /boot 目錄下
# cp arch/i386/boot/bzImage /boot/kernel-2.6.23-gentoo-r8
7. 新增 grub 設定檔,設定為新 Kernel 開機,建議保留舊選單,以免開機失敗可以用舊 kernel 開機

設定 SSH 的 RSA 驗證

最近老是有許多韓國和對岸的朋友來 try 偶的 SSH 密碼,
可能看偶的流量不速粉大,提高偶的頻寬使用率..
所以就啟動了 SSH 的 RSA 驗證機制,設定方式如下:

1. 編輯 SSH Server 設定檔 /etc/ssh/ssh_config
RSAAuthentication yes #啟動 RSA 驗證
PubkeyAuthentication yes #接受 Pubkey
AuthorizedKeysFile .ssh/authorized_keys #Pubkey 的檔名
PasswordAuthentication no #只使用 RSA 驗證(不使用密碼驗證)

2. 使用登入帳號(例如 username帳號)產生 Pubkey (不建議用 root 登入),在 username 的 home 目錄執行指令 ssh-keygen -d (若沒有.ssh目錄請先建立),過程會詢問密碼,直接按 Enter 登入 SSH 時就不會問密碼
Enter file in which to save the key (/home/username/.ssh/id_dsa) : (按Enter)
Enter passphrase (empty for no passphrase): (按Enter)
Enter same passphrase again: (按Enter)

出現下面訊息表示 Key 已經建立完成( key 請妥善保存)
Your identification has been saved in /home/username/.ssh/id_dsa. (Prikey)
Your public key has been saved in /home/username/.ssh/id_dsa.pub. (Pubkey)

3. 將 Pubkey 改名為 authorized_keys,這樣 Server 端的工作就完成了。(記得重新啟動 sshd)
#mv id_dsa.pub authorized_keys

4. 將 id_dsa 複製到 Client 端,如果使用 putty 可以用 puttygen.exe 轉成 .ppk 格式,在 putty 的 Auth\Private key file for authentication 指定 ppk 檔案,即可以直接登入 Server 。

MySQL 常用指令

*授權使用者權限
Shell>mysql -u root -p
password:XXXXX

mysql>grant all privileges on *.* to username@"%" identified by 'password' with grant option;
====>可以由遠端來存取。

mysql>grant all privileges on *.* to username@localhost identified by 'password' with grant option;
====>可以由本機來存取。

ps:授權完畢必須用mysqladmin reload來使授權生效。

*變更使用者密碼

mysql> SET PASSWORD FOR username@"%" = PASSWORD('password');
mysql> SET PASSWORD FOR username@localhost = PASSWORD('password');

*備份MySQL
mysqldump --all-databases --complete-insert --add-drop-table --user=username --password=password

不小心移除 emerge 的安裝方法

假如您哪一天要知道甚麼叫做手殘,可以執行下面這個指令

emerge unmerge portage

這樣 Gentoo 的 emerge 就不能用了...
搜尋 Google 並沒有人像偶這麼白目,
所以當然不會有人回答怎麼裝回去..
經過請教高人,裝回去的方法如下:

1. 找一台沒經過手殘的 Gentoo ,用下面指令包一個 emerge 的 Package
emerge -B portage
會產生 binary Package 在 /usr/portage/packages/

2. 把產生的 Package 丟到被手殘的那台 Gentoo
解開後複製到相對的目錄下

3. emerge 就裝回來了(留下了感動的眼淚)

附註:沒事別亂玩...


經過查詢與驗證正解如下(2008/02/12):
/usr/portage/sys-apps/portage/files/README.RESCUE 中說明恢復 portage 的網址為
http://www.gentoo.org/proj/en/portage/doc/manually-fixing-portage.xml
為了以防萬一連結不見,就把整篇文章複製過來,以後查詢也方便,需要的人請自行參考

Manually fixing broken portage installations

1. Manually fixing portage

Purpose

This section will tell you how to manually update/fix your portage installation in case you can't run emerge sys-apps/portage. While not hard it is still to be done with great care, so please follow the listed steps exactly (but apply common sense when necessary).

Getting a portage tarball

The first step to do is to get the tarball of a current portage version. In the following text we will use portage-2.1.1 as an example (as this is the current stable version at the time of this writing), please replace that with a version present in the tree if possible.

Warning: If your currently installed version of python reported by python -V is less than 2.4 then you must choose a version of portage that is compatible with it. If you have at least python 2.3 then use portage-2.1.1.tar.bz2. If you have python 2.2 then use portage-2.0.51.22.tar.bz2.

Depending on the exact reason portage doesn't work for you anymore it may still be possible to use it to fetch the tarball for you, so as a first step please try to run emerge --fetchonly sys-apps/portage, only if that doesn't work you have to manually fetch the tarball with:

Code Listing 1.1: Fetching portage tarball with wget

# wget -P /usr/portage/distfiles http://distfiles.gentoo.org/distfiles/portage-2.1.1.tar.bz2 

After that you should have the tarball available as /usr/portage/distfiles/portage-2.1.1.tar.bz2.

Replacing the installed version

The next step is to unpack the tarball to a temporary location, using /root/portage-recover as example the commands to do that are:

Code Listing 1.2: Unpacking portage tarball

# cd /root
# mkdir portage-recover
# cd portage-recover
# tar xfj /usr/portage/distfiles/portage-2.1.1.tar.bz2

After you have done this it's just a matter of replacing the python and bash files of your existing installation with the ones from the tarball (in most cases anyway). To do so please run:

Code Listing 1.3: Replacing installed files

# cd /root/portage-recover/portage-2.1.1
# cp -R pym bin /usr/lib/portage/

If you are not using Gentoo on FreeBSD then you should remove the sed wrapper script since it's not needed and it is known to cause problems with old versions of bash:

Code Listing 1.4: Removing the sed wrapper script

# rm -f /usr/lib/portage/bin/sed 

Note: If you accidently unmerged portage before or lost /etc/make.globals for other reasons you should also copy cnf/make.globals back into /etc, otherwise portage might behave in strange ways.

Note: If the previous version of portage was less than 2.1 then you should now run emerge --metadata before continuing to the next step. This is necessary in order to convert the ebuild metadata to the new format that is used by portage 2.1 and above. It is okay to run this command even if you are not sure what the previous version of portage was.

Now you should have a working portage install again. To ensure a consistent system state however you should now run emerge sys-apps/portage again immediately.

If you get a command not found error message when you try to run emerge you have to recreate the symlink:

Code Listing 1.5: Recreating the emerge symlink

# ln -s ../lib/portage/bin/emerge /usr/bin/emerge 

If these steps didn't work for you your problem is likely not a broken portage installation but something else beyond the scope of this document. Please recheck the list of common problems and also look in bugzilla if the problem is reported there.

壓縮指令

.tar.gz 格式

    壓縮 tar zcvf package.tar.gz files...
    解壓 tar zxvf package.tar.gz

.tar.bz2 格式
    壓縮 tar jcvf package.tar.bz2 files...
    解壓 tar jxvf package.tar.gz

關於 Oracle Lock 的說明

本文轉貼自 http://www.wisc.edu/drmt/oratips/sess004.html

Oracle Tips Session #4
2/11/98
Dave Ruhde


Table of Contents

  • Terms

  • Review Read Consistency/Concurrency in Oracle

  • Row Locks

  • Select For Update

  • Table Locks

  • Lock Failures

  • Application Usage of Locking

  • Isolation Levels

  • Examples


Terms


Concurrency

How well can multiple sessions access the same data simultaneously

Consistency

How consistent is the view of the data between and within multiple sessions, transactions or statements

Statement

A SQL command that reads/changes data in the database. Changes made may or may not be committed (made permanent).
ex. SELECT, SELECT...FOR UPDATE, INSERT, UPDATE, DELETE

Session

A connection (as a single Oracle database user) to the database by an external program. The program that opens the Oracle session may be purely interactive--SQL statements are explicitly submitted by a person, or the program could be a developed application program using any number of languages

A database user may have mulitple concurrent sessions. All sessions, whether by multiple users or the same user, are independent.


Transaction

All changes to the database are made through transactions. A transaction consists of any number of SQL statements.

A transaction ends with a commit or a rollback. If the transaction is committed, ALL CHANGES by SQL statements in the transaction are saved. If the transaction is rolled back, ALL CHANGES by SQL statements in the transaction are undone.

A new transaction begins when a session is opened and after each rollback or commit.


Commit

Makes permanent all changes made in a transaction. Changes are not made part of the database until the transaction is commited. A commit is made when:
  • a 'COMMIT' statement is issued
  • the application program ends and disconnects its Oracle session normally.

Rollback

Undo any uncommitted changes made by a transaction. The transaction is ended. A rollback occurs when:

  • a 'ROLLBACK' statement is issued
  • Oracle terminates a session because
    • a application program or database exception is not handled and the application program terminates abnormally
    • a hardware or system failure causes the application program to terminate
    • a network failure causes the network connection between the connecting program and Oracle to be interrupted.
    • Oracle fails

Review Read consistency/concurrency

Consistency

All Oracle SQL statements ALWAYS work with a image of data that is consistent to a single point-in-time. The consistent image consists of all changes and only changes COMMITTED before the point-in-time of the image.

The consistent image nevers shows changes by other sessions that have not been committed to the database. Oracle never performs a "dirty read"

SQL statements do see changes, not yet committed, made by previous statements within the transaction the statement is part of.
By default, consistency is at the statement level not the transaction level. SQL statements return data using a consistent image based on the point-in-time the STATEMENT began, not when the transaction began. Each statement in a transaction works with its own consistent image.
Session 1, Example 1
Session 1, Example 2
Session 1, Example 3

Concurrency

Readers don't block readers.

Readers don't block writers.
Note: SELECT ... FOR UDPATE behaves as a writer

Writers don't block readers.


Row Locks

Writers block writers trying to update the same row.

SELECT..FOR UPDATE, UPDATE, DELETE and INSERT statements always lock the row(s) that the statement is modifying.

Only one transaction can lock a given row at a time.

Other transactions can not update or lock a row that is locked.

Locks are NOT released when a statement completes. Locks are released when the transaction is committed or rolled-back.

Example 1


SELECT...FOR UPDATE

SYNTAX
SELECT cols FROM tables [WHERE...] FOR UPDATE [OF cols] [NOWAIT];

Locks row(s) from all table(s) that are selected by the query.

The row locks are released when the transaction that contains the SELECT...FOR UPDATE is committed or rolled-back.

OF cols: Lock only rows from tables which have a column listed in the OF clause. Rows are not locked in tables that don't have a column appearing in the OF clause. If no OF clause is listed, rows from all tables are locked.

NOWAIT: If NOWAIT is NOT specified and a row to be locked is locked by another transaction, SELECT...FOR UPDATE will wait indefinitely until the lock is released. If NOWAIT is specified and a row to be selected is locked by another transaction, the SELECT...FOR UPDATE will return immediately with a "ORA-00054: resource busy and acquire with NOWAIT specified" error.

Example 2
Example 3 - FOR UPDATE OF
Example 4 - NOWAIT


TABLE LOCKS

SYNTAX
LOCK TABLE table(s) IN EXCLUSIVE MODE [NOWAIT];

Successful completion of this statement means no other transaction can update or lock a row in the locked table(s) or lock the locked table(s) until the lock are released.

The table locks are released when the transaction that contains the LOCK TABLE is committed or rolled-back.

NOWAIT: If NOWAIT is NOT specified, the LOCK TABLE waits indefinitely until all existing row or table locks on the table(s) to be locked are released. If NOWAIT is specified, LOCK TABLE returns immediately with a "ORA-00054: resource busy and acquire with NOWAIT specified" error.

Table locks do not block readers.

Example 5


Lock Failures

"ORA-00054: resource busy and acquire with NOWAIT specified"
Occurs when you attempt to lock NOWAIT and the resource is already locked.

"ORA-00060 deadlock detected while waiting for resource"
Transaction 1 locks row A, then Transaction 2 locks row B.

Then Transaction 1 tries to lock Row B and transaction 2 tries to lock row A.

Each transaction is waiting on the other.

Oracle automatically detects deadlocks. When a deadlock is detected the last statement fails with a ORA-00060.


Application Usage of Locking

DRMT believes developers should design applications to avoid locking records explicitly.

Simple techniques like the following will not hold records locked and will likely meet true operational needs.

SELECT * INTO editrec FROM emp WHERE empno=:empnum
/* Record is not locked */
User Edits row....
UPDATE emp .... WHERE empno = :empnum


Isolation Levels

Isolation Levels control the point-in-time used for the consistent image that each statement sees.

Read Committed (Default)
Statements see changes as of the point-in-time of the start of the statement.

Read Only
Statements see changes committed as of the point-in-time of the start of the transaction. No updates allowed in transaction.

Serializable
Statements see changes committed as of the point-in-time of the start of the transaction. Updates are allowed, but:

A row that has been changed and committed by another transaction after the serialized transaction began will cause the serialized transaction to fail with a ORA-08177: can't serialize access for this transaction" error.

Serializable mode can only be used after consultation with DBA. Tables must be specially initiated to participate in a serializable transaction.

Usage

Read-only and Serializable mode could be useful for reporting tasks. Consult your DBA.

To set for a single transaction, first statement in transaction is:

SET TRANSACTION READ ONLY
SET TRANSACTION ISOLATION LEVEL READ COMMITTED (default)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

To set for all subsequent transactions within session:

ALTER SESSION ISOLATION LEVEL READ COMMITTED
ALTER SESSION ISOLATION LEVEL SERIALIZABLE



EXAMPLES

Example 1

Two transactions updating the same row
Session 2's update waits until Session 1's commit release the row lock.

Time
  |
|
|
|
|
|
|
|
V
Session 1 Session 2



-------BEGIN TRANSACTION-------
update cust set color='blue';






-----------COMMIT--------------
-------BEGIN TRANSACTION-------
select color from cust
where id=500;
color
-----
red


update cust set color=green
where id=500;

(Waiting...)

1 row updated.


select color from cust
where id=500;
color
-----
green
-----------COMMIT--------------

Example 2

SELECT FOR UPDATE
Session 2's update waits until Session 1's rollback releases the row lock.

Time
  |
|
|
|
|
|
|
|
V
Session 1 Session 2



-------BEGIN TRANSACTION-------
select color from cust where
id=500 for update;
color
-----
red



update cust set color='blue'
where id=500;



----------ROLLBACK--------------
-------BEGIN TRANSACTION-------
select color from cust
where id=500;
color
-----
red


update cust set color=green
where id=500;

(Waiting...)



(Still waiting...)



1 row updated.

-----------COMMIT--------------

Example 3

SELECT FOR UPDATE OF

Time
  |
|
|
|
|
|
|
|
V
Session 1 Session 2
-------BEGIN TRANSACTION-------
select c.color, t.typedesc
from cust c, custtype t where
cust.id=500 and c.type=t.type
for update nowait;

color typedesc
----- --------
red CASH ONLY

ROLLBACK;


select c.color, t.typedesc
from cust c, custtype t where
c.id=500 and c.type=t.type
for update of c.id nowait;

color typedesc
----- --------
red CASH ONLY
----------COMMIT--------------
-------BEGIN TRANSACTION-------

update custtype
set status='VALID';




(Waiting...)


100 rows updated.
-----------COMMIT--------------



update custtype
set status='INVALID';



100 rows updated.

-----------COMMIT--------------

Example 4

SELECT FOR UPDATE NOWAIT

Time
  |
|
|
|
|
|
|
|
V
Session 1 Session 2
-------BEGIN TRANSACTION-------
select color from cust
where id=500 for update nowait;
color
-----
red

update cust set color='blue'
where id=500;

1 row updated.
-----------COMMIT--------------
-------BEGIN TRANSACTION-------

select color from cust
where id=500 for update nowait;


ORA-00054





select color from cust
where id=500 for update nowait;

update cust set color='blue'
where id=500;

1 row updated.

-----------COMMIT--------------

Example 5

LOCK TABLE
Time
  |
|
|
|
|
|
|
|
V
Session 1 Session 2
-------BEGIN TRANSACTION-------
select color from cust
where id=500 for update nowait;
color
-----
red
-----------ROLLBACK--------------



-------BEGIN TRANSACTION-------
select color from cust
where id=500 for update nowait;

ORA-00054
-----------COMMIT--------------

-------BEGIN TRANSACTION-------

LOCK TABLE cust in
exclusive mode nowait;

ORA-00054



LOCK TABLE cust in
exclusive mode nowait;

update cust set color='blue'

100 row updated.

-----------COMMIT--------------

ORA-00060: deadlock detected while waiting for resource

本文轉貼自 http://www.dbmotive.com/oracle_error_codes.php?errcode=00060
關於 DeadLock 的發生在這篇有不錯的範例說明:

    Transactions deadlocked one another while waiting for resources.

    Once Oracle detects a conflict between transactions, it will automatically stop the transaction of one of the sessions so they will not block each other for ever.

    You will find a trace file in the user_dump_dest containing the objects involved.

    eg:
    First setup 2 tables:

    SQL> create table t1(n number);

    Table created.

    SQL> create table t2(n number);

    Table created.

    SQL> insert into t1 values(1);

    1 row created.

    SQL> insert into t2 values(2);

    1 row created.

    SQL> commit;

    Commit complete.

    Now you need to have 2 sessions (S1 and S2)

    Timestamp session action

    T1 S1 update t1 set n=3;
    T2 S2 update t2 set n=4;
    T3 S2 update t1 set n=2; --> This will wait until S1 commits the action done in T1
    T4 S1 update t2 set n=4; --> This will wait until S2 commits the action done in T2

    Now S2 is waiting for S1 to commit the transaction done in T1
    and S1 is waiting for S2 to commit the transaction done in T2

    This is a deadlock situation. After a while, S2 will time out with following error:

    SQL> update t1 set n=2;
    update t1 set n=2
    *
    ERROR at line 1:
    ORA-00060: deadlock detected while waiting for resource

    Remark: Only the action done at T3 will timeout, the lock set by S2 in T2 is still there.
    S1 will still wait for the commit/rollback of the transaction done in T2

整理 portage

本文轉貼自 http://forums.gentoo.tw/viewtopic.php?p=56454

由於portage有相依性的功能,所以我們可以很輕鬆的編譯我們要的軟體
但是當我們移除軟體(emerge -C)時候,因相依而安裝進來的程式並不會一併被移除,以致檔案愈來愈多
這時們可以用

emerge --depclean

來清除這些無用的軟體
但是在某些情況下很可能別的軟體在編譯的時候有用到這些軟體而造成連結的錯誤
這時我們可以用
revdep-rebuild

來修正這種錯誤

而安裝的程式列表(不包含相依的程式)會放在 /var/libs/portage/world

CommitMonitor

本文轉貼自 http://blog.miniasp.com/category/Subversion.aspx

我們在使用 Subversion 做原始碼版本管理的時候,常常要主動去使用 Show Log 檢查到底專案有沒有別人改過什麼,但因為很多人會忘記看 log 就開始拼命寫,導致下班前才在解決衝突的問題!

今天發現一個很棒的工具,叫做 CommitMonitor,網址在 http://tools.tortoisesvn.net/CommitMonitor

這個小工具可以縮在系統匣(System Tray)中,程式本身執行的時候佔的記憶體很小不太會吃系統的資源,且可以定時去 SVN 儲存庫檢查你參與的專案是否有其他人 commit 了新東西進來,如果有就先看一下 Log 再說。

如何用 TortoiseSVN 復原錯誤的簽入動作

本文轉貼自 http://blog.miniasp.com/category/Subversion.aspx

在一個開發團隊裡,當有人將錯誤的程式碼簽入(Commit)到 Subversion 儲存庫(Repository)去,的確會造成許多人的麻煩,這時程式開發經理應該就要立即出面,將這類的問題排除。

如果目前最新的版本(Revision)是 9 的話,你可能想要將版本復原到 Revision 8,這時候有兩種方法可以做:

1. 透過 Update to revision.. 將目前的程式碼更新至第 8 版

透過 Update to revision.. 將目前的程式碼更新至第 8 版

但這種作法是錯的!

因為這方法只是將你的工作目錄更新到舊的版本(第 8 版),這時重新所做的修改其實都會失敗,因為當你要 commit 的時候,Subversion 就會告訴你必須先更新(SVN Update)過才能 commit,到時就會發生衝突!

2. 透過 Merge (合併) 將目前的程式碼合併到第 8 版

透過 Merge (合併) 將目前的程式碼合併到第 8 版

這才是正確的作法!

因為這等於是將目前的版本 ( 第 9 版 ) 直接合併到第 8 版去,現有的工作目錄還是處於第 9 版的狀態,只是所有程式碼因為執行合併的關係而變成第 8 版的內容而已,當你 Merge 成功以後,還必須 commit 一次,讓第9版修改後的程式簽入到儲存庫 ( 成為第 10 版 ),這時的第 10 版就會跟原本的第 8 版一模一樣了!此時,再請專案成員做一次 SVN Update 就可以讓大家回復到正確程式碼的狀態了!

而將目前的 Revision 合併回上一個 Revision 的方式如下,以下將以 TortoiseSVN 工具做說明:

  1. 先在工作目錄的根目錄顯選取 SVN Merge 功能

    透過 Merge (合併) 將目前的程式碼合併到第 8 版

    開啟 SVN Merge 對話方塊

  2. 在 From: 的地方選取 "HEAD Revision" ,在 To: 的地方選取 Revision 並設定到你想要還原到的版本編號 ( 此範例是 8 ),最後按下 Merge 按鈕

    在 From: 的地方選取 "HEAD Revision" ,在 To: 的地方選取 Revision 並設定到你想要還原到的版本編號 ( 此範例是 8 ),最後按下 Merge 按鈕

  3. 此時你將會看到在工作目錄下的檔案已經被異動了,最後再執行 SVN Commit 就可以復原成功了!

WinXP中讓帳號在登入時顯示或消失的方法

有的時候,我們想要讓某個帳號出現或消失,以讓電腦的安全性或便利性更好,這個時候可以透過修改機碼的方式來達到這個目的。

修改的機碼如下:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList]
"administrator"=dword:00000001
"1"=dword:00000000

00000001-->表示讓該帳號【顯示】
00000000-->表示讓該帳號【隱藏】

改變 Windows 檔案權限指令

要在 Windows 以指令模式改變檔案的權限,可以使用以下指令:

CACLS files /e /p {USERNAME}:{PERMISSION}

指令的內容為:

* /p : 設定新的權限
* /e : 編輯現有權限
* {USERNAME} : 用戶名稱
* {PERMISSION} : 權限內容:
o R - Read
o W - Write
o C - Change (write)
o F - Full control

以下指令會給予 win_user 全部控制權:

C:> CACLS files /e /p win_user:f

Screen 使用方法

screen
screen 可以在遠端建立多重終端機的套件。
第一次開始 screen,僅需要在命令列中下達: screen
讀回前一次的 screen 為 screen -r
在 screen 模式下,可使用滿多指令,以下列出較常用的一些:

ctrl - a , c : 在 screen 中,再多開啟一個 screen
ctrl - a , w : 查詢現在共開始多少 screen
ctrl - a , k : 刪除目前的這一個 screen
ctrl - a , d : detach, 離開 screen 並保留 screen 在伺服器上,下次想要使用,可以用 screen -r 讀回
ctrl - a , 數字 : 和 ctrl - a , w 搭配使用,可以切換到指定的 screen
ctrl - a , n : 切換至下一個 screen
ctrl - a , p : 切換至上一個 screen
ctrl - a , a : 切換至前一個 screen


低調部落格在 Google

一直在找適合的部落格,考慮的要點有幾個

  1. 瀏覽速度快
  2. 使用簡單
  3. 可以用很久(不會不見),老實說我也不常寫,也沒啥時間寫,所以 Blog 本身的功能變動性太大對我反而是一個困擾,相信很多人也是這麼覺得。
  4. 簡單的放多張圖,這是懶人寫 Blog 的方法,貼個圖就少打了很多字,人家說有圖有真相,也是另外一個優點,目前找到的 Blog 貼圖方式都不是讓我很滿意,有些還要修改 Html,我這種懶人是不會去查 html Code 的。