public Optional<List<UUID>> addInBatch(List<ApiActionLogs> apiActionLogs) {
if (apiActionLogs == null || apiActionLogs.isEmpty()) {
return Optional.ofNullable(Collections.emptyList());
}
List<UUID> insertedUUIDList = new ArrayList<>();
ByteArrayToUUIDConverter byteArrayToUUIDConverter = new ByteArrayToUUIDConverter();
try (Connection connection = ConnectionUtil.getWriteConnection()) {
connection.setAutoCommit(false);
String sql = String.format(
"INSERT INTO %s (uuid, agency_id, user_id, `action`, api_bulk_action_queues_uuid, message_log, "
+ "status, response_code, request_data, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", "api_action_logs ");
try (PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
int counter = 0;
int batchSize = 100;
List<UUID> batchRequests = new ArrayList<>();
for (ApiActionLogs apiActionLog : apiActionLogs) {
if (apiActionLog == null
|| apiActionLog.getAgencyId() == null
|| apiActionLog.getUserId() == null
|| apiActionLog.getApiBulkActionQueuesUuid() == null) {
continue;
}
statement.clearParameters();
apiActionLog.setUuid(UUID.randomUUID());
statement.setBytes(1, byteArrayToUUIDConverter.to(apiActionLog.getUuid()));
statement.setString(2, apiActionLog.getAgencyId().toString());
statement.setString(3, apiActionLog.getUserId().toString());
statement.setString(4, apiActionLog.getAction() == null ? null : apiActionLog.getAction().name());
statement.setBytes(5, byteArrayToUUIDConverter.to(apiActionLog.getApiBulkActionQueuesUuid()));
statement.setString(6, apiActionLog.getMessageLog());
statement.setString(7, apiActionLog.getStatus() == null ? null : apiActionLog.getStatus().name());
statement.setString(8, apiActionLog.getResponseCode());
statement.setString(9, apiActionLog.getRequestData() == null ? null : apiActionLog.getRequestData().toString());
statement.setTimestamp(10, Timestamp.valueOf(LocalDateTime.now()));
statement.setTimestamp(11, Timestamp.valueOf(LocalDateTime.now()));
statement.addBatch();
batchRequests.add(apiActionLog.getUuid());
if ((counter + 1) % batchSize == 0 || (counter + 1) == apiActionLogs.size()) {
try {
statement.executeBatch(); // Execute Batch Operations
insertedUUIDList.addAll(batchRequests);
batchRequests.clear();
} catch (Exception e) {
log.error("Error :{}", e);
System.out.println("Error "+e);
if (!connection.isClosed()) {
connection.rollback();
}
batchRequests.clear();
}
statement.clearBatch();
}
counter++;
}
} catch (Exception e) {
log.error("Error :{}", e);
System.out.println("Error2 "+e);
e.printStackTrace();
}
connection.commit(); // execute prepare statement
if(!insertedUUIDList.isEmpty()) {
return Optional.ofNullable(insertedUUIDList);
}
} catch (Exception e) {
log.error("Error :{}", e);
System.out.println("Error3 "+e);
e.printStackTrace();
}
return Optional.ofNullable(Collections.emptyList());
}
Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts
Wednesday, December 13, 2023
Bulk data insert using prepared statement
Monday, October 25, 2021
What is the difference between date and instant?
Instant and LocalDateTime are two entirely different animals: One represents a moment, the other does not. Instant represents a moment, a specific point in the timeline. LocalDateTime represents a date and a time-of-day.
Subscribe to:
Posts (Atom)
Element of a good table (Ref: Database design mere mortals by Michael J. Hernandez)
Elements of the Ideal Table: It represents a single subject, which can be an object or event that reduces the risk of potential data integ...
-
If any event fired on a DOM element it will also fired on all of its parent element which is called the event bubbling. So, the element eve...
-
N.B- we can iterate an array using foreach and map in javascript like below- var arrays = [1,2,3,4,5]; //foreach arrays.forEach(function(cu...
-
slice()- returns a shallow copy of a portion of given array . ex: var a=['j','u','r','g','e','n...