今天有個功能需要對CLOB欄位進行操作,期間遇到幾個問題,老天眷顧,都算是解決了,記一下,免得日後重蹈覆轍。
錯誤一:
ORA-22920: 未鎖定含有 LOB 值的行
出現這個問題的原因,是由於select出LOB欄位時未加 for update,ORACLE官方文檔對該錯誤的說明:
引用文字代碼
1.ORA-22920 row containing the LOB value is not locked
2.
3.Cause: The row containing the LOB value must be locked before updating the LOB value.
4.
5.Action: Lock the row containing the LOB value before updating the LOB value.
ORA-22920 row containing the LOB value is not locked
Cause: The row containing the LOB value must be locked before updating the LOB value.
Action: Lock the row containing the LOB value before updating the LOB value.
於是加上for update再測,發現錯誤提示變了,變成下面的:
錯誤二:
ORA-01002: 讀取違反順序
出現這個問題的原因是Connection 的 autoCommit屬性值為true,改成
Sql代碼
1.conn.setAutoCommit(false);
2.
3.......
4.
5.LOB操作
6.
7......
8.
9.conn.commit();
10.
11.conn.setAutoCommit(true);
conn.setAutoCommit(false);
......
LOB操作
.....
conn.commit();
conn.setAutoCommit(true);
就可以了。
另外,注意第一步的select時,不要select出非LOB的欄位,並且,如果一張表有多個LOB欄位,操作順序也最好按照表裡的欄位順序來操作,否則也可能出錯。