Spaces:
Sleeping
Sleeping
File size: 9,037 Bytes
03549e5 ec4d503 03549e5 ec4d503 03549e5 ec4d503 03549e5 ec4d503 03549e5 ec4d503 03549e5 ec4d503 03549e5 ec4d503 03549e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
package com.cs102.attendance.controller;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.HikariPoolMXBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.sql.DataSource;
import java.lang.management.ManagementFactory;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/health")
public class HealthController {
@Autowired
private DataSource dataSource;
@GetMapping("/database")
public ResponseEntity<Map<String, Object>> checkDatabaseConnection() {
Map<String, Object> response = new HashMap<>();
try (Connection connection = dataSource.getConnection()) {
// Test the connection
boolean isValid = connection.isValid(5); // 5 second timeout
if (isValid) {
response.put("status", "UP");
response.put("database", "Connected");
response.put("url", connection.getMetaData().getURL());
response.put("driver", connection.getMetaData().getDriverName());
response.put("version", connection.getMetaData().getDatabaseProductVersion());
return ResponseEntity.ok(response);
} else {
response.put("status", "DOWN");
response.put("database", "Connection invalid");
return ResponseEntity.status(503).body(response);
}
} catch (SQLException e) {
response.put("status", "DOWN");
response.put("database", "Connection failed");
response.put("error", e.getMessage());
return ResponseEntity.status(503).body(response);
}
}
@GetMapping("/connection-pool")
public ResponseEntity<Map<String, Object>> checkConnectionPool() {
Map<String, Object> response = new HashMap<>();
try {
if (dataSource instanceof HikariDataSource) {
HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
response.put("status", "UP");
response.put("poolName", "AttendanceHikariCP");
// Basic configuration details (always available)
response.put("maximumPoolSize", hikariDataSource.getMaximumPoolSize());
response.put("minimumIdle", hikariDataSource.getMinimumIdle());
response.put("connectionTimeout", hikariDataSource.getConnectionTimeout());
response.put("idleTimeout", hikariDataSource.getIdleTimeout());
response.put("maxLifetime", hikariDataSource.getMaxLifetime());
response.put("leakDetectionThreshold", hikariDataSource.getLeakDetectionThreshold());
// Try to get HikariCP MBean for detailed metrics
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (AttendanceHikariCP)");
if (server.isRegistered(poolName)) {
HikariPoolMXBean poolProxy = JMX.newMXBeanProxy(server, poolName, HikariPoolMXBean.class);
response.put("activeConnections", poolProxy.getActiveConnections());
response.put("idleConnections", poolProxy.getIdleConnections());
response.put("totalConnections", poolProxy.getTotalConnections());
response.put("threadsAwaitingConnection", poolProxy.getThreadsAwaitingConnection());
// Health indicators
int activeConnections = poolProxy.getActiveConnections();
int totalConnections = poolProxy.getTotalConnections();
int maxPoolSize = hikariDataSource.getMaximumPoolSize();
response.put("poolUtilization", String.format("%.2f%%",
(double) totalConnections / maxPoolSize * 100));
response.put("activeUtilization", String.format("%.2f%%",
(double) activeConnections / maxPoolSize * 100));
// Warning thresholds
if (totalConnections >= maxPoolSize * 0.9) {
response.put("warning", "Connection pool is near maximum capacity");
}
if (poolProxy.getThreadsAwaitingConnection() > 0) {
response.put("warning", "Threads are waiting for connections - possible bottleneck");
}
} else {
response.put("mbeanStatus", "MBean not registered - pool metrics unavailable");
response.put("note", "Pool configuration is available, but runtime metrics require MBean access");
}
} catch (Exception mbeanException) {
response.put("mbeanError", "Cannot access pool MBean: " + mbeanException.getMessage());
response.put("note", "Pool configuration is available, but runtime metrics are not accessible");
}
return ResponseEntity.ok(response);
} else {
response.put("status", "UNKNOWN");
response.put("message", "DataSource is not HikariCP");
return ResponseEntity.ok(response);
}
} catch (Exception e) {
response.put("status", "ERROR");
response.put("error", "Failed to get connection pool info: " + e.getMessage());
return ResponseEntity.status(500).body(response);
}
}
@GetMapping("/connection-test")
public ResponseEntity<Map<String, Object>> testConnectionLeak() {
Map<String, Object> response = new HashMap<>();
try {
// Test multiple rapid connections to check for leaks
for (int i = 0; i < 3; i++) {
try (Connection connection = dataSource.getConnection()) {
connection.isValid(1);
}
}
response.put("status", "UP");
response.put("message", "Connection leak test passed");
response.put("testConnections", 3);
return ResponseEntity.ok(response);
} catch (SQLException e) {
response.put("status", "DOWN");
response.put("error", "Connection leak test failed: " + e.getMessage());
return ResponseEntity.status(503).body(response);
}
}
@GetMapping("/pool-simple")
public ResponseEntity<Map<String, Object>> getSimplePoolInfo() {
Map<String, Object> response = new HashMap<>();
try {
if (dataSource instanceof HikariDataSource) {
HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
response.put("status", "UP");
response.put("dataSourceType", "HikariCP");
response.put("poolName", hikariDataSource.getPoolName());
response.put("jdbcUrl", hikariDataSource.getJdbcUrl());
response.put("maximumPoolSize", hikariDataSource.getMaximumPoolSize());
response.put("minimumIdle", hikariDataSource.getMinimumIdle());
response.put("connectionTimeout", hikariDataSource.getConnectionTimeout() + "ms");
response.put("idleTimeout", hikariDataSource.getIdleTimeout() + "ms");
response.put("maxLifetime", hikariDataSource.getMaxLifetime() + "ms");
response.put("leakDetectionThreshold", hikariDataSource.getLeakDetectionThreshold() + "ms");
response.put("isRunning", !hikariDataSource.isClosed());
return ResponseEntity.ok(response);
} else {
response.put("status", "UNKNOWN");
response.put("dataSourceType", dataSource.getClass().getSimpleName());
response.put("message", "Not using HikariCP");
return ResponseEntity.ok(response);
}
} catch (Exception e) {
response.put("status", "ERROR");
response.put("error", "Failed to get pool info: " + e.getMessage());
return ResponseEntity.status(500).body(response);
}
}
} |