<< All versions

Skill v1.0.0

currentAutomated scan96/100
taipt1504/agent-skills/database-patterns
──Details
PublishedOctober 3, 2026 at 01:38 PM
Content Hashsha256:95269fa0c163f91d...
Git SHA
──Files
Files (1 file, 5.3 KB)
SKILL.md5.3 KBactive
SKILL.md · 102 lines · 5.3 KB

version: "1.0.0" name: database-patterns description: > Database patterns for Java Spring — R2DBC (reactive), JPA/Hibernate (servlet), PostgreSQL, MySQL, connection pooling (HikariCP/r2dbc-pool), Flyway migrations, and query optimization. Use when writing @Entity or R2DBC models, creating Repository interfaces, writing @Query methods, planning Flyway migrations, tuning connection pools, or reviewing SQL performance. Includes scripts/validate-migration.sh for migration file validation. triggers: natural: ["db migration", "jpa entity", "connection pool", "query optimization", "r2dbc"] code: ["Repository.java", "Entity.java", "*.sql", "@Query"] applicability: always: false triggers: files_match: ["/*Repository.java", "/*Entity.java", "/db/migration//*.sql", "**/*Migration.java"] code_patterns: ["@Entity", "@Repository", "JpaRepository", "ReactiveCrudRepository", "R2dbcRepository", "@Table", "@Column"] task_keywords: ["entity", "repository", "JPA", "R2DBC", "Hibernate", "schema", "migration", "Flyway", "Liquibase", "N+1", "HikariCP"] related_rules:

  • rules/java/security.md
  • rules/java/observability.md

relevance_assessment: | HIGH 80%+: new @Entity OR new repository OR new query OR schema migration MEDIUM 40-79%: existing repo modification (e.g., add finder method) OR connection pool config LOW 1-39%: service that uses repository, no DB code change ZERO: project has no JPA / R2DBC (verify build.gradle)


Database Patterns

Stack Decision

CriterionJPA / HikariCPR2DBC / R2DBC Pool
RuntimeSpring MVC (blocking)Spring WebFlux (reactive)
RepositoryJpaRepositoryReactiveCrudRepository
Transaction@Transactional@Transactional + TransactionalOperator
PG driverorg.postgresql:postgresqlorg.postgresql:r2dbc-postgresql
MySQL drivercom.mysql.cj.jdbc.Driverio.asyncer:r2dbc-mysql

Engine Differences

AspectPostgreSQLMySQL
PK strategyGENERATED ALWAYS AS IDENTITY / SEQUENCEBIGINT UNSIGNED AUTO_INCREMENT
JPA @GeneratedValueSEQUENCE + @SequenceGeneratorIDENTITY
Stringstext (no storage diff vs varchar)VARCHAR(n) + utf8mb4 required
Timestampstimestamptz (always TZ-aware)DATETIME(3) (store in UTC)
Covering indexINCLUDE (col)List all cols in key (no INCLUDE)
Partial indexWHERE clause on indexNot supported (use composite)
Concurrent indexCREATE INDEX CONCURRENTLYALGORITHM=INPLACE, LOCK=NONE
Transactional DDLYesNo (implicit commit)
Default isolationREAD_COMMITTEDREPEATABLE_READ (gap locks)

Critical Rules (Both Engines)

  1. `@SQLRestriction` not `@Where` — Hibernate 6+
  2. `FetchType.LAZY` always — JOIN FETCH / @EntityGraph at call site
  3. No `.block()` in reactive — use R2DBC on WebFlux
  4. `open-in-view: false` — always disable
  5. Parameterized queries only — never concatenate user input
  6. Index all FK columns
  7. Pool size = `vCPU * 2 + 1` (SSD); smaller pools outperform oversized
  8. DTO projections for read-only — skip entity lifecycle overhead

Migration Safety (Expand-Contract)

  1. Expand — add nullable column; code writes both old+new
  2. Migrate — backfill in batches (1000 rows), not single UPDATE
  3. Contract — add NOT NULL / drop old column in separate deploy

Always safe: add nullable column, index (CONCURRENTLY/INPLACE), add table. Never in prod: DROP without expand-contract, ALTER to smaller type, TRUNCATE.

Verification Checklist

  • [ ] No @Data on entities; use @Getter + @NoArgsConstructor(PROTECTED) + builder
  • [ ] @EntityGraph / JOIN FETCH for known N+1 paths
  • [ ] DTO projections for read-only queries
  • [ ] @Transactional(readOnly = true) on query methods
  • [ ] HikariCP leak detection enabled in non-prod
  • [ ] Migrations follow V{n}__{description}.sql naming
  • [ ] Large table changes use online DDL or batch strategy
  • [ ] Migration tested with Testcontainers

References

FileContents
postgresql.mdSchema, indexing (B-tree/GIN/GiST/BRIN/covering/partial), UPSERT, keyset pagination, SKIP LOCKED, RLS, JPA config, R2DBC config, pool sizing, Flyway
mysql.mdSchema, indexing (covering without INCLUDE, invisible, generated columns), utf8mb4, JPA config, R2DBC asyncer driver, pool sizing, Flyway
jpa-hibernate.mdEntity design, N+1 prevention (EntityGraph/JOIN FETCH/DTO projections), HikariCP config, pagination (offset + cursor), batch writes, specifications
r2dbc.mdReactiveCrudRepository, R2dbcEntityTemplate, DatabaseClient, reactive transactions, pool config, Testcontainers testing
migrations.mdFlyway naming, expand-contract, safety rules, large table strategies (PG vs MySQL), Testcontainers validation

Related Skills

  • summer-data — R2DBC converters, audit_log/outbox_events DDL, AuditTableValidator
  • spring-webflux-patterns — JPA (MVC) or R2DBC (WebFlux) stack selection
  • redis-patterns — Cache-aside to reduce DB load
  • testing-workflow — Testcontainers for DB integration tests, @DataR2dbcTest/@DataJpaTest
All versions