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)
Javascript module
JavaScript Modules (ES6) let you split code into multiple files and reuse code cleanly. Main keywords: export → make something available...
-
Constructor is the blueprint to create object .Here person is the constructor from which we can create objects. jane,mark and john are the...
-
Lets see a practical example- POJO: public class Student{ private String firstName; private String lastName; public Student(String firstName...
-
Function expression: Expression will return a result immediately . ex:2+3 will return 5.so this is a expression. similarly , assume a funct...