Skip to content

Commit f9896e4

Browse files
authored
Adding spring-boot rules (#104)
* Adding spring-boot rules * Adding SQL reference in the tables
1 parent d7b833c commit f9896e4

11 files changed

+5350
-45
lines changed

.cursor/rules/301-frameworks-spring-boot-core.mdc

Lines changed: 1207 additions & 0 deletions
Large diffs are not rendered by default.

.cursor/rules/302-frameworks-spring-boot-rest.mdc

Lines changed: 765 additions & 0 deletions
Large diffs are not rendered by default.

.cursor/rules/303-frameworks-spring-data-jdbc.mdc

Lines changed: 728 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 396 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,396 @@
1+
---
2+
description:
3+
globs:
4+
alwaysApply: false
5+
---
6+
# Spring Boot HikariCP Connection Pool Configuration
7+
8+
HikariCP is the default connection pool for Spring Boot and is known for being the fastest, most reliable connection pool available for Java applications. This guide will help you configure HikariCP optimally for your Spring Boot applications.
9+
10+
## Implementing These Principles
11+
12+
These guidelines are built upon the following core principles:
13+
14+
- **Performance First**: Configure pool sizes based on your application's actual database concurrency needs
15+
- **Resource Efficiency**: Balance connection availability with memory and database server resources
16+
- **Monitoring & Observability**: Enable metrics and logging to understand pool behavior
17+
- **Environment-Specific**: Adjust configurations based on development, testing, and production environments
18+
- **Fail-Fast**: Configure appropriate timeouts to detect issues quickly
19+
20+
## Table of contents
21+
22+
- Rule 1: Essential Pool Sizing Configuration
23+
- Rule 2: Connection Timeout and Lifecycle Management
24+
- Rule 3: Health Check and Validation Configuration
25+
- Rule 4: Performance Monitoring and Metrics
26+
- Rule 5: Environment-Specific Configuration Strategies
27+
28+
## Rule 1: Essential Pool Sizing Configuration
29+
30+
**Title**: Right-size your connection pool based on application needs
31+
32+
**Description**: The most critical aspect of HikariCP configuration is determining the optimal pool size. Ask yourself: "How many concurrent database operations does my application actually need?" Most applications need far fewer connections than developers initially think.
33+
34+
**Key Questions to Ask:**
35+
- How many concurrent users will access my application?
36+
- How many database operations happen per user request?
37+
- What's my database server's connection limit?
38+
- Am I running multiple application instances?
39+
40+
**Good example:**
41+
42+
```yaml
43+
# application.yml
44+
spring:
45+
datasource:
46+
hikari:
47+
# Start with this formula: connections = ((core_count * 2) + effective_spindle_count)
48+
# For most web apps: 10-15 connections is often sufficient
49+
maximum-pool-size: 10
50+
minimum-idle: 5
51+
# Allow pool to shrink during low activity
52+
idle-timeout: 300000 # 5 minutes
53+
```
54+
55+
```java
56+
// For programmatic configuration
57+
@Configuration
58+
public class DatabaseConfig {
59+
60+
@Bean
61+
@ConfigurationProperties("spring.datasource.hikari")
62+
public HikariConfig hikariConfig() {
63+
HikariConfig config = new HikariConfig();
64+
// Conservative pool sizing for most applications
65+
config.setMaximumPoolSize(10);
66+
config.setMinimumIdle(5);
67+
config.setIdleTimeout(300_000);
68+
return config;
69+
}
70+
}
71+
```
72+
73+
**Bad Example:**
74+
75+
```yaml
76+
# application.yml - DON'T DO THIS
77+
spring:
78+
datasource:
79+
hikari:
80+
# Too many connections - wastes resources and can overwhelm DB
81+
maximum-pool-size: 100
82+
minimum-idle: 50
83+
# Never let connections be idle - keeps unnecessary connections
84+
idle-timeout: 0
85+
```
86+
87+
## Rule 2: Connection Timeout and Lifecycle Management
88+
89+
**Title**: Configure appropriate timeouts for reliable connection handling
90+
91+
**Description**: Proper timeout configuration ensures your application fails fast when database issues occur and doesn't hold onto stale connections. Ask yourself: "How long should my application wait for a database connection before giving up?"
92+
93+
**Key Questions to Ask:**
94+
- What's an acceptable wait time for users when the database is under load?
95+
- How quickly should I detect database connectivity issues?
96+
- What's my application's typical query execution time?
97+
98+
**Good example:**
99+
100+
```yaml
101+
# application.yml
102+
spring:
103+
datasource:
104+
hikari:
105+
# Fast failure for connection acquisition
106+
connection-timeout: 20000 # 20 seconds - adjust based on your needs
107+
# Detect stale connections quickly
108+
max-lifetime: 1800000 # 30 minutes - less than DB connection timeout
109+
# Quick validation of connections
110+
validation-timeout: 5000 # 5 seconds
111+
# Test connections when borrowed from pool
112+
connection-test-query: SELECT 1
113+
```
114+
115+
```java
116+
// Programmatic configuration with monitoring
117+
@Configuration
118+
public class DatabaseConfig {
119+
120+
@Bean
121+
@ConfigurationProperties("spring.datasource.hikari")
122+
public HikariConfig hikariConfig() {
123+
HikariConfig config = new HikariConfig();
124+
config.setConnectionTimeout(20_000);
125+
config.setMaxLifetime(1_800_000);
126+
config.setValidationTimeout(5_000);
127+
128+
// Enable connection testing
129+
config.setConnectionTestQuery("SELECT 1");
130+
return config;
131+
}
132+
}
133+
```
134+
135+
**Bad Example:**
136+
137+
```yaml
138+
# application.yml - DON'T DO THIS
139+
spring:
140+
datasource:
141+
hikari:
142+
# Too long - users will think app is frozen
143+
connection-timeout: 120000
144+
# Too long - may exceed DB server timeout
145+
max-lifetime: 7200000
146+
# No validation - stale connections may be used
147+
# connection-test-query: # missing
148+
```
149+
150+
## Rule 3: Health Check and Validation Configuration
151+
152+
**Title**: Implement robust connection health checking
153+
154+
**Description**: Configure HikariCP to validate connections and maintain pool health. Ask yourself: "How can I ensure my application always gets working database connections?"
155+
156+
**Key Questions to Ask:**
157+
- Does my database server have connection timeouts?
158+
- How can I detect network issues between app and database?
159+
- Should I validate connections proactively or reactively?
160+
161+
**Good example:**
162+
163+
```yaml
164+
# application.yml
165+
spring:
166+
datasource:
167+
hikari:
168+
# Lightweight validation query for most databases
169+
connection-test-query: SELECT 1
170+
# Validate connections when borrowed (recommended for production)
171+
validation-timeout: 5000
172+
# Remove connections that fail validation
173+
leak-detection-threshold: 60000 # 60 seconds - helps find connection leaks
174+
```
175+
176+
```java
177+
// Database-specific configuration
178+
@Configuration
179+
@Profile("production")
180+
public class ProductionDatabaseConfig {
181+
182+
@Bean
183+
@ConfigurationProperties("spring.datasource.hikari")
184+
public HikariConfig hikariConfig() {
185+
HikariConfig config = new HikariConfig();
186+
187+
// PostgreSQL-specific validation
188+
config.setConnectionTestQuery("SELECT 1");
189+
config.setValidationTimeout(5_000);
190+
config.setLeakDetectionThreshold(60_000);
191+
192+
// Additional PostgreSQL optimizations
193+
config.addDataSourceProperty("socketTimeout", "30");
194+
config.addDataSourceProperty("loginTimeout", "10");
195+
196+
return config;
197+
}
198+
}
199+
```
200+
201+
**Bad Example:**
202+
203+
```yaml
204+
# application.yml - DON'T DO THIS
205+
spring:
206+
datasource:
207+
hikari:
208+
# Heavy validation query that impacts performance
209+
connection-test-query: "SELECT COUNT(*) FROM large_table WHERE complex_condition = 'value'"
210+
# No leak detection - memory leaks may go unnoticed
211+
# leak-detection-threshold: # missing
212+
```
213+
214+
## Rule 4: Performance Monitoring and Metrics
215+
216+
**Title**: Enable comprehensive monitoring and metrics collection
217+
218+
**Description**: Configure HikariCP to provide visibility into connection pool behavior. Ask yourself: "How will I know if my connection pool is properly sized and performing well?"
219+
220+
**Key Questions to Ask:**
221+
- How can I monitor pool utilization in production?
222+
- What metrics indicate pool sizing issues?
223+
- How do I correlate application performance with database connection patterns?
224+
225+
**Good example:**
226+
227+
```yaml
228+
# application.yml
229+
spring:
230+
datasource:
231+
hikari:
232+
# Enable detailed metrics
233+
register-mbeans: true
234+
pool-name: "HikariPool-${spring.application.name}"
235+
236+
# Enable JMX metrics for monitoring tools
237+
management:
238+
endpoints:
239+
web:
240+
exposure:
241+
include: health,metrics,hikari
242+
metrics:
243+
export:
244+
prometheus:
245+
enabled: true
246+
```
247+
248+
```java
249+
// Comprehensive monitoring setup
250+
@Configuration
251+
@ConditionalOnProperty(name = "management.metrics.enabled", matchIfMissing = true)
252+
public class DatabaseMonitoringConfig {
253+
254+
@Bean
255+
@ConfigurationProperties("spring.datasource.hikari")
256+
public HikariConfig hikariConfig(MeterRegistry meterRegistry) {
257+
HikariConfig config = new HikariConfig();
258+
259+
// Enable metrics collection
260+
config.setRegisterMbeans(true);
261+
config.setPoolName("HikariPool-" + getApplicationName());
262+
config.setMetricRegistry(meterRegistry);
263+
264+
// Configure alerts for pool exhaustion
265+
config.setConnectionTimeout(20_000);
266+
config.setLeakDetectionThreshold(60_000);
267+
268+
return config;
269+
}
270+
271+
private String getApplicationName() {
272+
return System.getProperty("spring.application.name", "app");
273+
}
274+
}
275+
```
276+
277+
**Bad Example:**
278+
279+
```yaml
280+
# application.yml - DON'T DO THIS
281+
spring:
282+
datasource:
283+
hikari:
284+
# No monitoring enabled - flying blind in production
285+
register-mbeans: false
286+
# Generic pool name - hard to identify in monitoring tools
287+
pool-name: "pool"
288+
```
289+
290+
## Rule 5: Environment-Specific Configuration Strategies
291+
292+
**Title**: Adapt HikariCP configuration for different environments
293+
294+
**Description**: Configure HikariCP differently for development, testing, and production environments. Ask yourself: "What are the different requirements for each environment where my application runs?"
295+
296+
**Key Questions to Ask:**
297+
- How do my development and production database loads differ?
298+
- Should I use different pool sizes for testing vs production?
299+
- How can I make troubleshooting easier in development?
300+
301+
**Good example:**
302+
303+
```yaml
304+
# application-dev.yml - Development environment
305+
spring:
306+
datasource:
307+
hikari:
308+
maximum-pool-size: 5 # Smaller pool for dev
309+
minimum-idle: 2
310+
connection-timeout: 30000 # Longer timeout for debugging
311+
leak-detection-threshold: 30000 # Faster leak detection for dev
312+
register-mbeans: true # Enable for local monitoring
313+
314+
# application-prod.yml - Production environment
315+
spring:
316+
datasource:
317+
hikari:
318+
maximum-pool-size: 20 # Larger pool for production load
319+
minimum-idle: 10
320+
connection-timeout: 20000 # Fast failure in production
321+
idle-timeout: 300000 # Allow shrinking during low load
322+
max-lifetime: 1800000 # Refresh connections regularly
323+
leak-detection-threshold: 60000
324+
register-mbeans: true
325+
pool-name: "${spring.application.name}-prod"
326+
```
327+
328+
```java
329+
// Environment-specific configuration
330+
@Configuration
331+
public class EnvironmentSpecificDatabaseConfig {
332+
333+
@Bean
334+
@Profile("development")
335+
@ConfigurationProperties("spring.datasource.hikari")
336+
public HikariConfig devHikariConfig() {
337+
HikariConfig config = new HikariConfig();
338+
// Development: Favor debugging over performance
339+
config.setMaximumPoolSize(5);
340+
config.setConnectionTimeout(30_000);
341+
config.setLeakDetectionThreshold(30_000);
342+
config.setRegisterMbeans(true);
343+
return config;
344+
}
345+
346+
@Bean
347+
@Profile("production")
348+
@ConfigurationProperties("spring.datasource.hikari")
349+
public HikariConfig prodHikariConfig() {
350+
HikariConfig config = new HikariConfig();
351+
// Production: Favor performance and reliability
352+
config.setMaximumPoolSize(20);
353+
config.setMinimumIdle(10);
354+
config.setConnectionTimeout(20_000);
355+
config.setIdleTimeout(300_000);
356+
config.setMaxLifetime(1_800_000);
357+
config.setLeakDetectionThreshold(60_000);
358+
config.setRegisterMbeans(true);
359+
config.setPoolName(getApplicationName() + "-prod");
360+
return config;
361+
}
362+
363+
private String getApplicationName() {
364+
return System.getProperty("spring.application.name", "app");
365+
}
366+
}
367+
```
368+
369+
**Bad Example:**
370+
371+
```yaml
372+
# Same configuration for all environments - DON'T DO THIS
373+
spring:
374+
datasource:
375+
hikari:
376+
maximum-pool-size: 50 # Too many for dev, maybe wrong for prod
377+
minimum-idle: 25 # Wastes resources in all environments
378+
connection-timeout: 60000 # Too slow for production
379+
# No environment-specific tuning
380+
```
381+
382+
---
383+
384+
## Quick Configuration Checklist
385+
386+
Before deploying your HikariCP configuration, ask yourself these questions:
387+
388+
1. **Pool Sizing**: Have I calculated the right pool size based on my application's concurrency needs?
389+
2. **Timeouts**: Are my timeouts appropriate for fast failure detection without being too aggressive?
390+
3. **Monitoring**: Can I see pool utilization and performance metrics in my monitoring system?
391+
4. **Environment Differences**: Do I have different configurations for dev, test, and production?
392+
5. **Database Specifics**: Have I configured database-specific optimizations?
393+
6. **Connection Health**: Am I validating connections appropriately without impacting performance?
394+
7. **Resource Limits**: Are my pool settings within my database server's connection limits?
395+
396+
Remember: Start with conservative settings and adjust based on monitoring data from your actual production load!

0 commit comments

Comments
 (0)