# Fine-Tuning Tomcat 8 JDBC Connection Pool

## 🔧 1. Right-Size [`maxActive`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

**Description:** Maximum number of active connections in the pool.

> 🛑 **Issue**: Too low = connection starvation. Too high = DB overload.

### ✅ Recommendation:

```xml
maxActive="100"
```

📎 [Tomcat 8 Docs - maxActive](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

---

## ⏱ 2. Set a Sensible [`maxWait`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

**Description:** How long (in ms) to wait for a connection before throwing an exception.

> 🛑 **Issue**: Low value = frequent timeouts under load.

### ✅ Recommendation:

```xml
maxWait="10000"
```

📎 [Tomcat 8 Docs - maxWait](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

---

## ✅ 3. Avoid Stale Connections with [`validationQuery`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#JDBC_Interceptors), [`testOnBorrow`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes), and [`validationInterval`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

**Description:** Checks that a DB connection is valid before returning it to the app.

> 🛑 **Issue**: Stale connections lead to failed transactions.

### ✅ Recommendation:

```xml
testOnBorrow="true"
validationQuery="SELECT 1"
validationInterval="30000"
```

📎 [Tomcat 8 Docs - validationQuery & testOnBorrow](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

---

## 💤 4. Manage Idle Connections with [`minIdle`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes), [`maxIdle`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes), [`timeBetweenEvictionRunsMillis`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes), [`minEvictableIdleTimeMillis`](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

**Description:** Controls idle connection cleanup to avoid leaks and unnecessary DB usage.

### ✅ Recommendation:

```xml
minIdle="10"
maxIdle="30"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"
```

📎 [Tomcat 8 Docs - Idle Connection Settings](https://tomcat.apache.org/tomcat-8.5-doc/jdbc-pool.html#Common_Attributes)

---

---
