|
| 1 | +# Guide: Create a DataSource Pool |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +This guide provides step-by-step instructions for creating and configuring DataSource pools for different deployment scenarios. Follow the steps for the scenario that matches your use case. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- A Java project with Maven |
| 12 | +- `io.ebean:ebean-datasource` dependency already added to `pom.xml` |
| 13 | +- Database connection details (URL, username, password) |
| 14 | +- Understanding of your deployment environment (standard server, Kubernetes, AWS Lambda, etc.) |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Scenario 1: Basic DataSource Pool (Standard Server/VM) |
| 19 | + |
| 20 | +Use this configuration for traditional server deployments. |
| 21 | + |
| 22 | +### Step 1 — Create the pool builder |
| 23 | + |
| 24 | +```java |
| 25 | +DataSourcePool pool = DataSourcePool.builder() |
| 26 | + .name("mypool") |
| 27 | + .url("jdbc:postgresql://localhost:5432/myapp") |
| 28 | + .username("app_user") |
| 29 | + .password("password") |
| 30 | + .minConnections(5) |
| 31 | + .maxConnections(50) |
| 32 | + .build(); |
| 33 | +``` |
| 34 | + |
| 35 | +### Step 2 — Use the pool in your application |
| 36 | + |
| 37 | +```java |
| 38 | +try (Connection connection = pool.getConnection()) { |
| 39 | + try (PreparedStatement stmt = connection.prepareStatement("SELECT * FROM users WHERE id = ?")) { |
| 40 | + stmt.setInt(1, 123); |
| 41 | + try (ResultSet rs = stmt.executeQuery()) { |
| 42 | + while (rs.next()) { |
| 43 | + // Process results |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Scenario 2: Read-Only Pool |
| 53 | + |
| 54 | +Use this configuration for read-only workloads (analytics, reporting, caching layers). |
| 55 | +This adds readOnly(true) and autoCommit(true) options when building the pool. |
| 56 | + |
| 57 | +### Step 1 — Create a read-only pool |
| 58 | + |
| 59 | +```java |
| 60 | +DataSourcePool readOnlyPool = DataSourcePool.builder() |
| 61 | + .name("mypool-readonly") |
| 62 | + .url("jdbc:postgresql://read-replica.example.com:5432/myapp") |
| 63 | + .username("readonly_user") |
| 64 | + .password("password") |
| 65 | + .readOnly(true) // Signal no writes will occur |
| 66 | + .autoCommit(true) // Skip transaction overhead |
| 67 | + .minConnections(5) |
| 68 | + .maxConnections(30) |
| 69 | + .build(); |
| 70 | +``` |
| 71 | + |
| 72 | +### Step 2 — Use the read-only pool |
| 73 | + |
| 74 | +```java |
| 75 | +try (Connection connection = readOnlyPool.getConnection()) { |
| 76 | + try (PreparedStatement stmt = connection.prepareStatement("SELECT * FROM large_table")) { |
| 77 | + try (ResultSet rs = stmt.executeQuery()) { |
| 78 | + while (rs.next()) { |
| 79 | + // Process results - queries will be faster with read-only optimization |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## Scenario 3: Kubernetes Deployment |
| 89 | + |
| 90 | +Use this configuration when deploying to Kubernetes clusters. |
| 91 | + |
| 92 | +### Step 1 — Create a pool with warm-up configuration |
| 93 | + |
| 94 | +When pods start, they immediately receive production traffic. Start with `initialConnections` |
| 95 | +set higher than `minConnections` to avoid cold-start connection creation overhead. |
| 96 | + |
| 97 | +```java |
| 98 | +DataSourcePool pool = DataSourcePool.builder() |
| 99 | + .name("mypool") |
| 100 | + .url("jdbc:postgresql://postgres.default.svc.cluster.local:5432/myapp") |
| 101 | + .username("app_user") |
| 102 | + .password(System.getenv("DB_PASSWORD")) // Use environment variables |
| 103 | + .minConnections(5) |
| 104 | + .initialConnections(20) // Start with 20 connections ready |
| 105 | + .maxConnections(50) |
| 106 | + .build(); |
| 107 | +``` |
| 108 | + |
| 109 | +### Step 2 — Use environment variables for configuration |
| 110 | + |
| 111 | +For Kubernetes deployments, externalize database credentials: |
| 112 | + |
| 113 | +```java |
| 114 | +String dbUrl = System.getenv("DATABASE_URL"); |
| 115 | +String dbUser = System.getenv("DATABASE_USER"); |
| 116 | +String dbPass = System.getenv("DATABASE_PASSWORD"); |
| 117 | + |
| 118 | +DataSourcePool pool = DataSourcePool.builder() |
| 119 | + .name("mypool") |
| 120 | + .url(dbUrl) |
| 121 | + .username(dbUser) |
| 122 | + .password(dbPass) |
| 123 | + .minConnections(5) |
| 124 | + .initialConnections(20) |
| 125 | + .maxConnections(50) |
| 126 | + .build(); |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | +--- |
| 131 | + |
| 132 | +## Scenario 4: AWS Lambda |
| 133 | + |
| 134 | +Use this configuration for AWS Lambda functions. |
| 135 | + |
| 136 | +### Step 1 — Create a minimal pool |
| 137 | + |
| 138 | +The ebean datasource automatically detects when it is running in Lambda via the `LAMBDA_TASK_ROOT` environment variable |
| 139 | +and disables background heartbeat validation to optimize for cost and lambda suspend behaviour. |
| 140 | + |
| 141 | +```java |
| 142 | +DataSourcePool pool = DataSourcePool.builder() |
| 143 | + .name("mypool") |
| 144 | + .url("jdbc:postgresql://mydb.region.rds.amazonaws.com:5432/myapp") |
| 145 | + .username("app_user") |
| 146 | + .password(System.getenv("DB_PASSWORD")) |
| 147 | + .minConnections(1) |
| 148 | + .initialConnections(2) |
| 149 | + .maxConnections(10) |
| 150 | + .build(); |
| 151 | +``` |
| 152 | + |
| 153 | +### Step 2 — Understanding Lambda connection pooling |
| 154 | + |
| 155 | +**Important:** Connection pooling in Lambda is **per-Lambda instance**, not per-invocation: |
| 156 | + |
| 157 | +- A single Lambda container can be reused across many warm invocations |
| 158 | +- Connection pool persists across warm invocations |
| 159 | +- Cold starts get a fresh pool |
| 160 | +- Connections are trimmed automatically if unused |
| 161 | + |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +## Configuration Reference |
| 166 | + |
| 167 | +### Common Settings |
| 168 | + |
| 169 | +| Setting | Default | Purpose | |
| 170 | +|---------|---------|---------| |
| 171 | +| `minConnections` | 2 | Minimum connections to maintain in the pool | |
| 172 | +| `initialConnections` | Same as minConnections | Connections to create when pool starts (useful for Kubernetes/warm-up) | |
| 173 | +| `maxConnections` | 200 | Maximum connections to allow | |
| 174 | +| `readOnly` | false | Set to true for read-only workloads | |
| 175 | +| `autoCommit` | false | Set to true to skip transaction boundaries | |
| 176 | +| `validateOnHeartbeat` | true (false in Lambda) | Enable background connection validation | |
| 177 | +| `heartbeatFreqSecs` | 30 | How often to validate connections (seconds) | |
| 178 | + |
| 179 | +### Typical Sizing |
| 180 | + |
| 181 | +**Development/Testing:** |
| 182 | +```java |
| 183 | +.minConnections(1) |
| 184 | +.initialConnections(1) |
| 185 | +.maxConnections(5) |
| 186 | +``` |
| 187 | + |
| 188 | +**Production - Standard Server:** |
| 189 | +```java |
| 190 | +.minConnections(5) |
| 191 | +.initialConnections(10) |
| 192 | +.maxConnections(50) |
| 193 | +``` |
| 194 | + |
| 195 | +**Production - High Traffic:** |
| 196 | +```java |
| 197 | +.minConnections(10) |
| 198 | +.initialConnections(40) |
| 199 | +.maxConnections(100) |
| 200 | +``` |
| 201 | + |
| 202 | +**Lambda - Standard:** |
| 203 | +```java |
| 204 | +.minConnections(1) |
| 205 | +.initialConnections(2) |
| 206 | +.maxConnections(10) |
| 207 | +``` |
| 208 | + |
| 209 | +**Lambda - Provisioned Concurrency:** |
| 210 | +```java |
| 211 | +.minConnections(2) |
| 212 | +.initialConnections(10) |
| 213 | +.maxConnections(20) |
| 214 | +``` |
| 215 | + |
| 216 | +--- |
| 217 | + |
| 218 | +## Next Steps |
| 219 | + |
| 220 | +- Read the [README](../../README.md) for more information about the connection pool |
| 221 | +- Check the [ebean-datasource GitHub repository](https://github.com/ebean-orm/ebean-datasource) for latest updates |
| 222 | +- Consult the [DataSourceBuilder API documentation](https://javadoc.io/doc/io.ebean/ebean-datasource) for all available configuration options |
0 commit comments