PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Menangani BatchUpdateException menggunakan withBatch

Saya dapat menyelesaikan ini dengan posting yang disebutkan dalam komentar Alexandros. Solusinya sekarang terlihat seperti ini:

sql.withTransaction {
    try {
        sql.withBatch(1000, 'insert into category (id, version, name, parent_id) ' +
        'select :id, :version, :name, :parent_id ' +
        'where not exists (select name, parent_id from category where name = :name and parent_id = :parent_id);') { stmt ->
            categoryInserts.each {
                try {
                    stmt.addBatch([id: it.id, version: 0, name: it.name, parent_id: it.parent?.id])
                } catch (SQLException e) {
                  log.error("Category ${it.name} with parent ${it.parent?.id} could not be inserted.")
                }
            }
        }
    } catch (BatchUpdateException e) {
        log.error("Categories could not be inserted.", e)
    }

    sql.commit()
}

Ketahuilah bahwa ini diselesaikan dengan dialek SQL postgresql. Untuk DBMS lain mungkin merupakan pendekatan yang berguna untuk menggunakan prosedur SQL dalam metode withBatch.

Jika seseorang mengetahui cara melakukan ini dengan SQL standar, tolong beri saya petunjuk.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. PostgreSQL Buat Basis Data

  2. apakah pernyataan sql memastikan atomisitas di postgres

  3. Cara menggunakan fungsi jendela SQL untuk menghitung persentase agregat

  4. Hasil ekspor PostgreSQL sebagai CSV dari server jarak jauh

  5. SQL beberapa kolom berbeda