![](/img/trans.png)
[英]How to insert a large xml in a Oracle table CLOB using log4j.xml
[英]Using JdbcTemplate to insert large CLOB's into Oracle fails
我通過Spring的JdbcTemplate
實用程序類與Oracle數據庫連接,我嘗試了這兩種代碼變體:
jdbcTemplate.update("INSERT INTO my_table (title, content) VALUES (?, ?)", title, content);
- 要么 -
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
OraclePreparedStatement ps = (OraclePreparedStatement)conn.prepareStatement("INSERT INTO my_table (title, content) VALUES (?, ?)");
ps.setString(1, title);
ps.setStringForClob(2, content);
return ps;
}
});
title
是傳統的VARCHAR2
, content
是CLOB
。
這些替代方案中的任何一個都適用於較小的content
值。 但是,當我有大量content
,沒有任何content
插入到CLOB
列中。
有趣的是,在這兩種情況下, title
都會更新。 就好像查詢只是忽略content
如果有太多,但從不拋出錯誤。
有誰知道我該怎么解決這個問題?
謝謝。
編輯 :
根據@GreyBeardedGeek的回答,我嘗試使用OracleLobHandler
和DefaultLobHandler
,效果相同。 事情一直有效,直到我的CLOB
達到一定的規模。
我也嘗試了以下代碼,同樣效果:
Connection conn = db.getDataSource().getConnection();
CLOB clob = CLOB.createTemporary(conn, false, CLOB.DURATION_SESSION);
clob.setString(1, myString);
OraclePreparedStatement ps = (OraclePreparedStatement)conn.prepareStatement("UPDATE my_table SET blob = ?");
ps.setCLOB(1, clob);
ps.execute();
令我感到困惑的是,為什么這些方法中的每一種都適用於較小的CLOB
,但隨后突然間會破壞大型CLOB
。 DB中是否存在某種類型的配置? 或者是代碼的問題?
好的,我覺得很傻。 事實證明,即使是這個簡單的代碼也正確地存儲了CLOB
:
jdbcTemplate.update("UPDATE my_table SET title = ?, content = ? WHERE id = ?", getTitle(), getContentString(), getId());
問題是我從數據庫中檢索CLOB
代碼。 以下是基於我的代碼(和修復)的推測:似乎較小的CLOB
被緩存在內存中,並且可以在以后讀取(即,在連接關閉后,它們仍然可以被讀取)。 但是,對於較大的CLOB
,必須在連接仍然打開時讀取它們。
對我來說,這意味着修復就像在我的對象可用時一樣讀取CLOB
的內容一樣簡單。 在我的情況下,我並不是真的擔心內存問題,因為我不希望我的CLOB
包含非常大的內容,因此立即將值讀入內存是一種可接受的方法。
在我記憶中,Oracle已經需要對BLOB和CLOB進行特殊處理。 Spring JDBC有org.springframework.jdbc.support.lob.OracleLobHandler,用於設置BLOB和CLOB的值。
在http://techdive.in/spring/spring-handling-blobclob上有一個非常好的完整示例如何使用它,但基本上,你會做,而不是ps.setStringForClob
oracleLobHandler.getLobCreator().setClobAsString(ps, 2, content);
SqlLobValue(String content)
可用於CLOB。
請點擊以下鏈接: http : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/support/SqlLobValue.html
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.