|
| 1 | +package com.demo |
| 2 | + |
| 3 | +import org.springframework.boot.autoconfigure.SpringBootApplication |
| 4 | +import org.springframework.boot.runApplication |
| 5 | +import org.springframework.jdbc.core.JdbcTemplate |
| 6 | +import org.springframework.web.bind.annotation.GetMapping |
| 7 | +import org.springframework.web.bind.annotation.RequestParam |
| 8 | +import org.springframework.web.bind.annotation.RestController |
| 9 | +import javax.sql.DataSource |
| 10 | +import com.zaxxer.hikari.HikariDataSource |
| 11 | + |
| 12 | +@SpringBootApplication |
| 13 | +class App |
| 14 | + |
| 15 | +fun main(args: Array<String>) { |
| 16 | + runApplication<App>(*args) |
| 17 | +} |
| 18 | + |
| 19 | +data class Account( |
| 20 | + val id: Int, |
| 21 | + val memberId: Int, |
| 22 | + val name: String, |
| 23 | + val balance: Int |
| 24 | +) |
| 25 | + |
| 26 | +@RestController |
| 27 | +class AccountController(private val jdbc: JdbcTemplate, private val dataSource: DataSource) { |
| 28 | + |
| 29 | + @GetMapping("/health") |
| 30 | + fun health() = mapOf("status" to "ok") |
| 31 | + |
| 32 | + /** |
| 33 | + * /account?member=N queries the travel_account table. |
| 34 | + * |
| 35 | + * JDBC PS caching (prepareThreshold=1): |
| 36 | + * - 1st call on a fresh connection: Parse(query="SELECT ...") + Bind + Describe + Execute |
| 37 | + * - 2nd+ calls on same connection: Bind(ps="S_1") + Execute only (cached PS) |
| 38 | + * |
| 39 | + * The /evict endpoint forces HikariCP to evict all connections, so the |
| 40 | + * NEXT /account call gets a fresh connection with cold PS cache. |
| 41 | + */ |
| 42 | + @GetMapping("/account") |
| 43 | + fun getAccount(@RequestParam("member") memberId: Int): Any { |
| 44 | + return jdbc.execute { conn: java.sql.Connection -> |
| 45 | + conn.autoCommit = false |
| 46 | + try { |
| 47 | + val ps = conn.prepareStatement( |
| 48 | + """SELECT id, member_id, name, balance |
| 49 | + FROM travelcard.travel_account |
| 50 | + WHERE member_id = ?""" |
| 51 | + ) |
| 52 | + ps.setInt(1, memberId) |
| 53 | + val rs = ps.executeQuery() |
| 54 | + |
| 55 | + val result = if (rs.next()) { |
| 56 | + Account( |
| 57 | + id = rs.getInt("id"), |
| 58 | + memberId = rs.getInt("member_id"), |
| 59 | + name = rs.getString("name"), |
| 60 | + balance = rs.getInt("balance") |
| 61 | + ) |
| 62 | + } else { |
| 63 | + mapOf("error" to "not found", "member_id" to memberId) |
| 64 | + } |
| 65 | + |
| 66 | + rs.close() |
| 67 | + ps.close() |
| 68 | + conn.commit() |
| 69 | + result |
| 70 | + } catch (e: Exception) { |
| 71 | + conn.rollback() |
| 72 | + throw e |
| 73 | + } |
| 74 | + }!! |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * /evict forces HikariCP to evict all idle connections. |
| 79 | + * Next request gets a FRESH PG connection → cold PS cache. |
| 80 | + * This simulates what happens in production when connections cycle. |
| 81 | + */ |
| 82 | + @GetMapping("/evict") |
| 83 | + fun evict(): Map<String, Any> { |
| 84 | + val hikari = dataSource as HikariDataSource |
| 85 | + hikari.hikariPoolMXBean?.softEvictConnections() |
| 86 | + // Also wait briefly for eviction |
| 87 | + Thread.sleep(200) |
| 88 | + return mapOf("evicted" to true, "active" to (hikari.hikariPoolMXBean?.activeConnections ?: 0), |
| 89 | + "idle" to (hikari.hikariPoolMXBean?.idleConnections ?: 0)) |
| 90 | + } |
| 91 | +} |
0 commit comments