24h購物| | PChome| 登入
2009-01-19 14:05:22| 人氣2,346| 回應0 | 上一篇 | 下一篇

Sequence的NEXTVAL與CURRVAL(PostgreSQL 8.3.5)

推薦 0 收藏 0 轉貼0 訂閱站台

因為要存一個java instance進postgresql,
須要先取得serial的id存進class field,
在SQL的pgAdmin run
SELECT currval('"Company_id_seq"')
Yes! It's OK.
但是在jdbc insert時會引起一個問題,
SQL state [55000]; error code [0];
ERROR: currval of sequence "Company_id_seq" is not yet defined in this session; nested exception is org.postgresql.util.PSQLException: ERROR: currval of sequence "Company_id_seq" is not yet defined in this session
後來發現在網路上發現Oracle的也會,
ORA-08002: sequence TABLE1AUTON.CURRVAL is not yet defined in this session
ORA-06512: at "some_schema.sp1", line y
ORA-06512: at line 1
08002. 00000 -  "sequence %s.CURRVAL is not yet defined in this session"
*Cause:    sequence CURRVAL has been selected before sequence NEXTVAL
*Action:   select NEXTVAL from the sequence before selecting CURRVAL

經測試透過 JDBC(版本postgresql-8.3-604.jdbc4.jar)+spring2.5.6+commons-dbcp(1.2.2),
在call NEXTVAL之前不能call CURRVAL,
單獨call也不行。
但是在pgAdmin 執行 SELECT currval('"Company_id_seq"') 是可以得到回傳結果的。
這是各家資料庫廠商為了保護資料一致性所做的。我猜。

然後發現了jdbc 3.0 有定義Statement.getGeneratedKeys,
去下載8.0.3-604回來測試,發現不work,還沒implement。
原來還在todo list上。

再用spring的DataFieldMaxValueIncrementer試試,
結果因為sequence name有大小寫,需要有双引號,
打開spring的  PostgreSQLSequenceMaxValueIncrementer.getSequenceQuery()
直接把incrementerNames拿來用,完全沒有處理符號部份。
用escape sign,拆到中間都沒用,
最後workable版本configuration如下,

在新版driver出來前,就先這樣用了。


台長: softlive
人氣(2,346) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 數位資訊(科技、網路、通訊、家電) | 個人分類: |
此分類下一篇:2008 Top 25 Open Source Project
此分類上一篇:OpenJDK6 in ibook 升級

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文