Skip to content

Commit 7afc6bc

Browse files
committed
Docs: Add docs / guides for AI agents
1 parent d90bf88 commit 7afc6bc

3 files changed

Lines changed: 307 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A robust and fast SQL DataSource implementation
88

99
## Table of Contents
1010

11+
- [Documentation & Guides](#documentation--guides)
1112
- [Example use](#example-use)
1213
- [Robust and fast](#robust-and-fast)
1314
- [Read-Only Connection Pools](#read-only-connection-pools)
@@ -16,6 +17,16 @@ A robust and fast SQL DataSource implementation
1617
- [Mature](#mature)
1718
- [Java modules use](#java-modules-use)
1819

20+
### Documentation & Guides
21+
22+
This project includes step-by-step guides for common datasource configuration scenarios.
23+
24+
**See [docs/guides/README.md](docs/guides/README.md)** for:
25+
- [Creating DataSource pools](docs/guides/create-datasource-pool.md) — basic pools, read-only pools, Kubernetes deployment, AWS Lambda, and configuration reference
26+
- Instructions for AI agents to discover and follow these guides
27+
28+
These guides are designed for both developers and AI coding agents (Copilot, Claude, Cursor, etc.) to follow step-by-step instructions for common tasks.
29+
1930
### Example use:
2031

2132
```java

docs/guides/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Guides
2+
3+
Step-by-step guides written as instructions for AI agents and developers.
4+
5+
## Creating DataSource Pools
6+
7+
Step-by-step guide covering common scenarios for creating and configuring connection pools.
8+
9+
| Guide | Description |
10+
|-------|-------------|
11+
| [Create a DataSource Pool](create-datasource-pool.md) | Basic pool creation, read-only pools, Kubernetes configuration, AWS Lambda setup, and connection validation settings |
12+
13+
---
14+
15+
## Helping AI agents find these guides
16+
17+
AI coding agents can only follow these guides if they know they exist. Below are
18+
copy-paste snippets for the most common AI tooling configurations. Add whichever
19+
applies to your project.
20+
21+
### Project `README.md` (universal — works with all AI tools)
22+
23+
Add a section near the top of your project README:
24+
25+
```markdown
26+
## Documentation & Guides
27+
28+
This project includes step-by-step guides for common tasks with the ebean-datasource connection pool:
29+
30+
**https://github.com/ebean-orm/ebean-datasource/tree/main/docs/guides/**
31+
32+
AI agents should fetch and follow the relevant guide before performing datasource-related tasks.
33+
```
34+
35+
### Why README is important
36+
37+
Many AI agents (Copilot, Claude, Cursor, Aider) read the project `README.md`
38+
automatically as part of understanding the codebase. A visible link in README is the
39+
most universally effective way to ensure any agent working on your project will
40+
discover the guides without requiring tool-specific configuration files.
41+
42+
------
43+
44+
### GitHub Copilot — `.github/copilot-instructions.md`
45+
46+
```markdown
47+
## ebean-datasource
48+
49+
This project uses ebean-datasource for connection pooling. Step-by-step guides for common
50+
tasks are at: https://github.com/ebean-orm/ebean-datasource/tree/main/docs/guides/
51+
52+
Key guides (fetch and follow when performing the relevant task):
53+
- Create a DataSource Pool: https://raw.githubusercontent.com/ebean-orm/ebean-datasource/main/docs/guides/create-datasource-pool.md
54+
```
55+
56+
### Claude Code — `CLAUDE.md`
57+
58+
Same content as above — Claude Code reads `CLAUDE.md` at the project root.
59+
60+
### Cursor — `.cursor/rules/ebean-datasource.mdc`
61+
62+
```markdown
63+
---
64+
description: ebean-datasource connection pool guidance
65+
globs: ["**/*.java", "**/pom.xml"]
66+
alwaysApply: false
67+
---
68+
69+
## ebean-datasource
70+
71+
This project uses ebean-datasource for connection pooling. Before performing any datasource configuration task, fetch and
72+
follow the relevant step-by-step guide from:
73+
https://github.com/ebean-orm/ebean-datasource/tree/main/docs/guides/
74+
```
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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

Comments
 (0)