> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Connection Issues

> You can detect database connection issues in Apinizer, optimize connection pool settings, resolve network connection issues, and manage DNS resolution issues.

## Problem Symptoms

<Warning>
  Database connection issues usually manifest themselves with the following symptoms:
</Warning>

* Timeout errors in API requests
* `Connection refused` or `Connection timeout` errors
* Database queries failing
* High number of connection pool errors
* Pods not being able to connect to database

## Problem Causes

Database connection issues can usually be caused by the following factors:

* **Network Connection Issues**: Pods not being able to access database
* **Connection Pool Exhaustion**: Reaching maximum connection count
* **Database Server Issues**: Database server not responding
* **Wrong Connection Settings**: Incorrect host, port, or credentials
* **Firewall Rules**: Network security rules blocking connection
* **DNS Resolution Issues**: Database host name not being resolved

## Detection Methods

### 1. Log Analysis

<Steps>
  <Step title="Check Log Files">
    Search for database connection errors in log files:

    ```bash theme={null}
    kubectl logs <pod-name> | grep -i "connection"
    kubectl logs <pod-name> | grep -i "database"
    kubectl logs <pod-name> | grep -i "timeout"
    ```
  </Step>
</Steps>

### 2. Connection Test

<Steps>
  <Step title="Connect to Pod">
    Test database connection from inside pod:

    ```bash theme={null}
    # Connecting to pod
    kubectl exec -it <pod-name> -- /bin/bash

    # Testing database connection
    telnet <database-host> <database-port>
    ```
  </Step>
</Steps>

### 3. Connection Pool Metrics

<Info>
  Monitor connection pool metrics:
</Info>

* Active connection count
* Pending connection count
* Connection timeout count
* Connection pool usage percentage

## Solution Recommendations

### 1. Checking Connection Settings

Check database connection settings:

* **Host**: Is database server address correct?
* **Port**: Is port number correct?
* **Database Name**: Is database name correct?
* **Username/Password**: Are credentials correct?
* **Connection String**: Is connection string format correct?

### 2. Optimizing Connection Pool Settings

<Info>
  Optimize connection pool settings:
</Info>

```yaml theme={null}
# application.yml or environment variables
spring:
  datasource:
    hikari:
      maximum-pool-size: 20
      minimum-idle: 5
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000
```

<Note>
  **Recommendations:**

  * `maximum-pool-size`: Set maximum connection count
  * `minimum-idle`: Determine minimum idle connection count
  * `connection-timeout`: Set connection timeout duration
  * `idle-timeout`: Determine idle connections closing time
</Note>

### 3. Checking Network Connection

Check network connection:

```bash theme={null}
# DNS resolution test
nslookup <database-host>

# Port accessibility test
nc -zv <database-host> <database-port>

# Network policy check
kubectl get networkpolicies
```

### 4. Checking Database Server Status

Check database server status:

* Is database server running?
* Are there sufficient resources (CPU, RAM)?
* Has maximum connection limit been reached?
* Are there errors in database logs?

### 5. Checking Firewall Rules

Check firewall rules:

* Do pods have access to database port?
* Are network policies blocking connection?
* Are security group rules correct?

### 6. Resolving DNS Resolution Issues

Resolve DNS resolution issues:

* Are CoreDNS pods running?
* Are DNS settings correct?
* Is service discovery working properly?

## Preventive Measures

### 1. Connection Pool Monitoring

* Regularly monitor connection pool metrics
* Get early warning by setting up alerts
* Optimize connection pool usage

### 2. Health Checks

* Configure database health checks
* Set up liveness and readiness probes
* Set up automatic failover mechanisms

### 3. Connection Retry Mechanism

* Add connection retry mechanism
* Use exponential backoff
* Determine maximum retry count

## Related Resources

* [Connection Pooling](/en/operations/administrator-guides/pod-thread-count-periodic-monitoring)
* [Database Query Optimization](/en/operations/administrator-guides/pod-thread-count-periodic-monitoring)
* [Infrastructure Issues](/en/operations/troubleshooting/kubernetes-docker-containerd-troubleshooting)
