1: import java.sql.*;
2: import java.io.*;
3: import oracle.sql.*;
4: import oracle.jdbc.driver.*;
5: ...
6:
7: public void insertQuery(UploadBoard up,int re_step,int re_level)
throws Exception
8: {
9: ResultSet rs = null;
10: PreparedStatement pstmt = null;
11: Connection conn = null;
12: String query = "insert into "+up.getTableName()+" (seq,re_step,
re_level,name,title,pwd,email,readnum,writeday,ip,relativeCnt,
homepage,imgInfo,content,tag)
values(?,?,?,?,?,?,?,0,sysdate,?,0,?,?,empty_clob(),?)";
오라클 명령어 empty_clob()을 이용해 공간을 확보한다. |
13: try{
14: conn = DBManager.getClobConnection();
15: conn.setAutoCommit(false);
CLOB column을 업데이트 하는동안 다른 process의 접근을 막기위해 setAutoCommit(false)를 반드시 설정해야 한다. 이부분이 가장 중요하다. |
16: pstmt = conn.prepareStatement(query);
17: pstmt.setInt(1,up.getSeq());
18: pstmt.setInt(2,re_step);
19: pstmt.setInt(3,re_level);
20: pstmt.setString(4,up.getName());
21: pstmt.setString(5,up.getTitle());
22: pstmt.setString(6,up.getPwd());
23: pstmt.setString(7,up.getEmail());
24: pstmt.setString(8,up.getIp());
25: pstmt.setString(9,up.getHomepage());
26: pstmt.setString(10,up.getImgInfo());
27: pstmt.setString(11,up.getTag());
28: pstmt.executeUpdate();
29: pstmt.close();
30: String query2 = " select /*+ index_desc("+up.getTableName()+
" "+up.getTableName()+"_indx) */ content from "+
up.getTableName()+" where seq = ? for update ";
for update를 이용해 CLOB column을 lock한다. |
31: pstmt = conn.prepareStatement(query2);
32: pstmt.setInt(1,up.getSeq());
33: rs = pstmt.executeQuery();
34: if(rs.next()) {
35: CLOB clob = ((OracleResultSet)rs).getCLOB(1);
36: Writer writer = clob.getCharacterOutputStream();
37: Reader src = new CharArrayReader(up.getContent().toCharArray());
38: char[] buffer = new char[1024];
39: int read = 0;
40: while ( (read = src.read(buffer,0,1024)) != -1) {
41: writer.write(buffer, 0, read); // write clob.
42: }
43: src.close();
44: writer.close();
45: }
46: conn.commit();
47: conn.setAutoCommit(true);
CLOB column에 데이터을 저장하였다면 commit()을 실행시키고 conn.setAutoCommit(true)로 다시 설정한다. |
48: }finally{
49: DBManager.close(rs,pstmt,conn);
50: }
51: }
|