Revision 41759
Added by Claudio Atzori about 8 years ago
DatabaseUtils.java | ||
---|---|---|
10 | 10 |
import java.util.concurrent.BlockingQueue; |
11 | 11 |
import java.util.concurrent.Executors; |
12 | 12 |
import java.util.concurrent.TimeUnit; |
13 |
import java.util.concurrent.atomic.AtomicReference; |
|
13 | 14 |
import java.util.regex.Pattern; |
14 | 15 |
import javax.sql.DataSource; |
15 | 16 |
|
... | ... | |
363 | 364 |
final JdbcTemplate jdbcTemplate = jdbcTemplateFactory.createJdbcTemplate(dataSource); |
364 | 365 |
final TransactionTemplate transactionTemplate = transactionTemplateFactory.createTransactionTemplate(dataSource); |
365 | 366 |
|
366 |
int counterDone = 0; |
|
367 | 367 |
int counterTotal = 0; |
368 | 368 |
|
369 | 369 |
long start = DateUtils.now(); |
... | ... | |
373 | 373 |
rows.addAll(obtainListOfRows(prof)); |
374 | 374 |
if (rows.size() > numbersOfRecordsForTransaction) { |
375 | 375 |
counterTotal += rows.size(); |
376 |
counterDone += importTransaction(jdbcTemplate, transactionTemplate, rows);
|
|
376 |
importTransaction(jdbcTemplate, transactionTemplate, rows); |
|
377 | 377 |
rows.clear(); |
378 | 378 |
} |
379 | 379 |
} |
380 | 380 |
counterTotal += rows.size(); |
381 |
counterDone += importTransaction(jdbcTemplate, transactionTemplate, rows);
|
|
381 |
importTransaction(jdbcTemplate, transactionTemplate, rows); |
|
382 | 382 |
|
383 | 383 |
long end = DateUtils.now(); |
384 | 384 |
|
385 | 385 |
log.info("**********************************************************"); |
386 |
log.info("Processed " + counterDone + "/" + counterTotal + " rows in " + ((end - start) / 1000) + " seconds");
|
|
386 |
log.info("Processed " + counterTotal + " rows in " + ((end - start) / 1000) + " seconds"); |
|
387 | 387 |
log.info("**********************************************************"); |
388 | 388 |
} |
389 | 389 |
|
390 |
private int importTransaction(final JdbcTemplate jdbcTemplate, final TransactionTemplate transactionTemplate, List<GenericRow> rows) { |
|
391 |
if (rows == null) return 0; |
|
392 |
|
|
393 |
int res = rows.size(); |
|
394 |
|
|
395 |
while (!rows.isEmpty()) { |
|
396 |
List<GenericRow> ok = importTransactionInternal(jdbcTemplate, transactionTemplate, rows); |
|
397 |
|
|
398 |
if (ok.size() < rows.size()) { |
|
399 |
importTransactionInternal(jdbcTemplate, transactionTemplate, ok); |
|
400 |
res--; |
|
401 |
if ((ok.size() + 1) < rows.size()) { |
|
402 |
rows = rows.subList(ok.size() + 1, rows.size()); |
|
403 |
} else { |
|
404 |
rows.clear(); |
|
405 |
} |
|
406 |
} else { |
|
407 |
rows.clear(); |
|
408 |
} |
|
390 |
private void importTransaction(final JdbcTemplate jdbcTemplate, final TransactionTemplate transactionTemplate, List<GenericRow> rows) |
|
391 |
throws DatabaseException { |
|
392 |
if (rows != null && rows.size() > 0) { |
|
393 |
importTransactionInternal(jdbcTemplate, transactionTemplate, rows); |
|
409 | 394 |
} |
410 |
return res; |
|
411 | 395 |
} |
412 | 396 |
|
413 | 397 |
private List<GenericRow> importTransactionInternal(final JdbcTemplate jdbcTemplate, |
414 | 398 |
final TransactionTemplate transactionTemplate, |
415 |
final List<GenericRow> rows) { |
|
416 |
return transactionTemplate.execute(new TransactionCallback<List<GenericRow>>() { |
|
399 |
final List<GenericRow> rows) throws DatabaseException { |
|
417 | 400 |
|
418 |
@Override |
|
419 |
public List<GenericRow> doInTransaction(final TransactionStatus status) { |
|
420 |
final List<GenericRow> ok = Lists.newArrayList(); |
|
421 |
try { |
|
422 |
for (GenericRow row : rows) { |
|
423 |
if (row.isToDelete()) { |
|
424 |
deleteRow(jdbcTemplate, row.getTable(), row.getFields()); |
|
425 |
} else { |
|
426 |
addOrUpdateRow(jdbcTemplate, row.getTable(), row.getFields()); |
|
401 |
final AtomicReference<DatabaseException> error = new AtomicReference<DatabaseException>(); |
|
402 |
|
|
403 |
try { |
|
404 |
return transactionTemplate.execute(new TransactionCallback<List<GenericRow>>() { |
|
405 |
|
|
406 |
@Override |
|
407 |
public List<GenericRow> doInTransaction(final TransactionStatus status) { |
|
408 |
final List<GenericRow> ok = Lists.newArrayList(); |
|
409 |
try { |
|
410 |
for (GenericRow row : rows) { |
|
411 |
if (row.isToDelete()) { |
|
412 |
deleteRow(jdbcTemplate, row.getTable(), row.getFields()); |
|
413 |
} else { |
|
414 |
addOrUpdateRow(jdbcTemplate, row.getTable(), row.getFields()); |
|
415 |
} |
|
416 |
ok.add(row); |
|
427 | 417 |
} |
428 |
ok.add(row); |
|
418 |
} catch (DatabaseException e) { |
|
419 |
log.warn("Transaction failed", e); |
|
420 |
status.setRollbackOnly(); |
|
421 |
error.set(e); |
|
429 | 422 |
} |
430 |
} catch (DatabaseException e) { |
|
431 |
log.warn("Transaction failed", e); |
|
432 |
status.setRollbackOnly(); |
|
423 |
return ok; |
|
433 | 424 |
} |
434 |
return ok; |
|
425 |
}); |
|
426 |
} finally { |
|
427 |
if (error.get() != null) { |
|
428 |
throw error.get(); |
|
435 | 429 |
} |
436 |
});
|
|
430 |
} |
|
437 | 431 |
} |
438 | 432 |
|
439 | 433 |
protected void addOrUpdateRow(final JdbcTemplate jdbcTemplate, final String table, final Map<String, Object> rowFields) throws DatabaseException { |
Also available in: Unified diff
eventually we got to propagate exceptions.... YAY