Parameters, row mapping, single/optional/list, generated keys, batch via NamedParameterJdbcTemplate (JdbcClient has none), exception translation, transactions, an unclosed stream() exhausting a 2-connection pool, withMaxRows truncating silently, and spring.jdbc.template.* reaching the auto-configured JdbcClient. JdbcClient API diffed across 6.1.0, 6.2.19 and 7.0.0. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5n3PRbPkUsiJybTeg
34 lines
1.1 KiB
Java
34 lines
1.1 KiB
Java
package com.ankurm.jdbcclient;
|
|
|
|
import org.springframework.jdbc.core.simple.JdbcClient;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
/** Moves stock between two products. Two UPDATEs that must succeed or fail together. */
|
|
@Service
|
|
public class StockService {
|
|
|
|
private final JdbcClient jdbc;
|
|
|
|
public StockService(JdbcClient jdbc) {
|
|
this.jdbc = jdbc;
|
|
}
|
|
|
|
@Transactional
|
|
public void moveStockAtomic(long from, long to, int qty) {
|
|
move(from, to, qty);
|
|
}
|
|
|
|
/** Same body, no @Transactional: every statement auto-commits on its own. */
|
|
public void moveStockNoTx(long from, long to, int qty) {
|
|
move(from, to, qty);
|
|
}
|
|
|
|
private void move(long from, long to, int qty) {
|
|
jdbc.sql("UPDATE product SET stock_count = stock_count + :q WHERE id = :id")
|
|
.param("q", qty).param("id", to).update(); // first UPDATE
|
|
jdbc.sql("UPDATE product SET stock_count = stock_count - :q WHERE id = :id")
|
|
.param("q", qty).param("id", from).update(); // CHECK (stock_count >= 0) may fail here
|
|
}
|
|
}
|