refactor: 优化变量和私有嵌套类的命名
- 将循环计数器变量从 'i' 重命名为更具描述性的 'itemIndex',提高代码可读性 - 将 JdbcExecutor 重命名为 TransactionJdbcExecutor 以更好地反映其事务处理功能
This commit is contained in:
@@ -225,19 +225,19 @@ class JdbcOperationSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final int paramsSize = params.size();
|
final int paramsSize = params.size();
|
||||||
int batchCount = (paramsSize + batchSize - 1) / batchSize;
|
final int batchCount = (paramsSize + batchSize - 1) / batchSize;
|
||||||
|
|
||||||
final BatchUpdateResult result = new BatchUpdateResult(paramsSize, batchCount, batchSize);
|
final BatchUpdateResult result = new BatchUpdateResult(paramsSize, batchCount, batchSize);
|
||||||
|
|
||||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
int i = 0;
|
int itemIndex = 0;
|
||||||
int batchIndex = 0;
|
int batchIndex = 0;
|
||||||
for (Object[] ps : params) {
|
for (Object[] ps : params) {
|
||||||
i++;
|
itemIndex++;
|
||||||
fillStatement(stmt, ps);
|
fillStatement(stmt, ps);
|
||||||
stmt.addBatch();
|
stmt.addBatch();
|
||||||
final int indexInBatch = i % batchSize;
|
final int indexInBatch = itemIndex % batchSize;
|
||||||
if (indexInBatch == 0 || i >= paramsSize) {
|
if (indexInBatch == 0 || itemIndex >= paramsSize) {
|
||||||
try {
|
try {
|
||||||
int[] updateCounts = stmt.executeBatch();
|
int[] updateCounts = stmt.executeBatch();
|
||||||
result.recordSuccessBatch(batchIndex, updateCounts);
|
result.recordSuccessBatch(batchIndex, updateCounts);
|
||||||
@@ -248,7 +248,7 @@ class JdbcOperationSupport {
|
|||||||
updateCounts = ((BatchUpdateException) e).getUpdateCounts();
|
updateCounts = ((BatchUpdateException) e).getUpdateCounts();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int n = (i >= paramsSize && indexInBatch != 0) ? indexInBatch : batchSize;
|
int n = (itemIndex >= paramsSize && indexInBatch != 0) ? indexInBatch : batchSize;
|
||||||
updateCounts = new int[n];
|
updateCounts = new int[n];
|
||||||
Arrays.fill(updateCounts, UNKNOWN_COUNT);
|
Arrays.fill(updateCounts, UNKNOWN_COUNT);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -204,7 +204,7 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
final boolean autoCommit = conn.getAutoCommit();
|
final boolean autoCommit = conn.getAutoCommit();
|
||||||
try {
|
try {
|
||||||
conn.setAutoCommit(false);
|
conn.setAutoCommit(false);
|
||||||
operations.accept(new JdbcExecutor(conn));
|
operations.accept(new TransactionJdbcExecutor(conn));
|
||||||
conn.commit();
|
conn.commit();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
@@ -240,7 +240,7 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
final boolean autoCommit = conn.getAutoCommit();
|
final boolean autoCommit = conn.getAutoCommit();
|
||||||
try {
|
try {
|
||||||
conn.setAutoCommit(false);
|
conn.setAutoCommit(false);
|
||||||
if (operations.test(new JdbcExecutor(conn))) {
|
if (operations.test(new TransactionJdbcExecutor(conn))) {
|
||||||
conn.commit();
|
conn.commit();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -264,11 +264,11 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
|||||||
|
|
||||||
// #endregion
|
// #endregion
|
||||||
|
|
||||||
private static final class JdbcExecutor implements JdbcOperations {
|
private static final class TransactionJdbcExecutor implements JdbcOperations {
|
||||||
|
|
||||||
private final Connection conn;
|
private final Connection conn;
|
||||||
|
|
||||||
private JdbcExecutor(Connection conn) {
|
private TransactionJdbcExecutor(Connection conn) {
|
||||||
this.conn = conn;
|
this.conn = conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user