Overcomplicated oracle jdbc BLOB handling

前端 未结 9 1011
予麋鹿
予麋鹿 2020-12-04 18:20

When I search the web for inserting BLOBs into Oracle database with jdbc thin driver, most of the webpages suggest a 3-step approach:

  1. insert empty_blob()
9条回答
  •  日久生厌
    2020-12-04 18:29

    If size of inserting BLOB is greater than blob.getBufferSize(), transaction is commited as soon as first chunk is written to db as default value of autoCommit property of jdbc connection is true and further chunks writes fail as db treats them as new transactions. It is suggested as follows:
    a) Set jdbc connection autoCommit property to false.

    conn.setAutoCommit(false);
    

    b) Explicitely commit the transaction after uploading the whole BLOB.

    while ((bytesRead = messageInputStream.read(buffer)) != -1) {
         cumBytes += bytesRead;
         blobOutputStream.write(buffer, 0, bytesRead);
        }
    conn.commit();
    

提交回复
热议问题