Advertisement
NLinker

Failed attempt to make batch upserts with Postgresql

Oct 5th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1.     public List<CrawlerTask> pushAll(
  2.         Connection connection,
  3.         Instant now,
  4.         Provider provider,
  5.         List<CrawlerTask> ts
  6.     ) {
  7.         Query q = connection.createQuery(
  8.             "INSERT INTO crawler_task (\n" +
  9.             "  provider_id,\n" +
  10.             "  entity_type,\n" +
  11.             "  external_id,\n" +
  12.             "  status,\n" +
  13.             "  fire_time,\n" +
  14.             "  created_at,\n" +
  15.             "  updated_at\n" +
  16.             ") VALUES (\n" +
  17.             "  :provider,\n" +
  18.             "  :entityType,\n" +
  19.             "  :externalId,\n" +
  20.             "  :status,\n" +
  21.             "  :fireTime,\n" +
  22.             "  :createdAt,\n" +
  23.             "  :updatedAt\n" +
  24.             ") ON CONFLICT DO NOTHING;",
  25.             true);
  26.         ts.forEach(t -> {
  27.             t.setProvider(provider);
  28.             // t.setEntityType(); -- should be set by the caller
  29.             // t.setExternalId(); -- should be set by the caller
  30.             t.setStatus(CrawlerTaskStatus.New);
  31.             t.setFireTime(now);
  32.             t.setCreatedAt(now);
  33.             t.setUpdatedAt(now);
  34.             q.bind(t)
  35.                 .addToBatch();
  36.  
  37.         });
  38.         Connection c = q.executeBatch();
  39.         // int[] x = connection.getBatchResult();
  40.         // Postgres does not support retrieving generated keys in batch mode.
  41.         //
  42.  
  43.         return ts;
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement