|
| 1 | +# Guide: Configuration Reference |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +A complete reference of every `DataSourceBuilder` setting, its default value, and the equivalent |
| 6 | +property key for file-based / external configuration. Use this alongside the task-focused guides |
| 7 | +(pool creation, read-only pools, validation, Aurora) when you need to know exactly what a setting |
| 8 | +does or what it defaults to. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Two ways to configure |
| 13 | + |
| 14 | +### 1. Programmatic (builder) |
| 15 | + |
| 16 | +```java |
| 17 | +DataSourcePool pool = DataSourcePool.builder() |
| 18 | + .name("mypool") |
| 19 | + .url("jdbc:postgresql://localhost:5432/myapp") |
| 20 | + .username("app_user") |
| 21 | + .password("password") |
| 22 | + .minConnections(5) |
| 23 | + .maxConnections(50) |
| 24 | + .build(); |
| 25 | +``` |
| 26 | + |
| 27 | +### 2. Properties (external configuration) |
| 28 | + |
| 29 | +The builder can load settings from `java.util.Properties`. There are three entry points: |
| 30 | + |
| 31 | +```java |
| 32 | +Properties props = ...; // loaded from file, env, avaje-config, Spring, etc. |
| 33 | + |
| 34 | +// (a) no prefix: keys are "username", "url", ... |
| 35 | +DataSourcePool pool = DataSourcePool.builder().load(props).build(); |
| 36 | + |
| 37 | +// (b) custom prefix: keys are "my-db.username", "my-db.url", ... |
| 38 | +DataSourcePool pool = DataSourcePool.builder().load(props, "my-db").build(); |
| 39 | + |
| 40 | +// (c) "datasource.<poolName>." prefix: keys are "datasource.hr.username", ... |
| 41 | +DataSourcePool pool = DataSourcePool.builder().loadSettings(props, "hr").build(); |
| 42 | +``` |
| 43 | + |
| 44 | +Example properties file using the `loadSettings` convention with pool name `hr`: |
| 45 | + |
| 46 | +```properties |
| 47 | +datasource.hr.username=app_user |
| 48 | +datasource.hr.password=password |
| 49 | +datasource.hr.url=jdbc:postgresql://localhost:5432/myapp |
| 50 | +datasource.hr.minConnections=5 |
| 51 | +datasource.hr.maxConnections=50 |
| 52 | +datasource.hr.leakTimeMinutes=30 |
| 53 | +``` |
| 54 | + |
| 55 | +> When used through **Ebean ORM**, these `datasource.<name>.*` properties are typically placed in |
| 56 | +> `application.yaml` / `application.properties` and loaded for you via avaje-config. |
| 57 | +
|
| 58 | +Property keys are matched case-insensitively. |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Connection settings |
| 63 | + |
| 64 | +| Builder method | Property key | Default | Description | |
| 65 | +|----------------|--------------|---------|-------------| |
| 66 | +| `url(String)` | `url` (or `databaseUrl`) | – | JDBC URL. | |
| 67 | +| `username(String)` | `username` | – | Database username. | |
| 68 | +| `password(String)` | `password` | – | Database password. | |
| 69 | +| `readOnlyUrl(String)` | `readOnlyUrl` | – | Optional separate URL for read-only connections. | |
| 70 | +| `driver(...)` / `driver(String)` | `driver` (or `databaseDriver`) | auto from URL | JDBC driver class / instance. | |
| 71 | +| `schema(String)` | `schema` | driver default | Default schema applied to connections. | |
| 72 | +| `catalog(String)` | `catalog` | driver default | Default catalog applied to connections. | |
| 73 | +| `isolationLevel(int)` | `isolationLevel` | `READ_COMMITTED` | Transaction isolation level. Property accepts names e.g. `READ_COMMITTED`. | |
| 74 | +| `autoCommit(boolean)` | `autoCommit` | `false` | Auto-commit mode for pooled connections. | |
| 75 | +| `readOnly(boolean)` | `readOnly` | `false` | Mark connections read-only (optimises read workloads). | |
| 76 | +| `applicationName(String)` | `applicationName` | – | Application name reported to the driver where supported. | |
| 77 | +| `clientInfo(Properties)` | `clientInfo` | – | Client info properties (semicolon separated `key=value` in properties form). | |
| 78 | +| `customProperties(Map)` / `addProperty(...)` | `customProperties` | – | Extra JDBC driver connection properties (semicolon separated `key=value` in properties form). | |
| 79 | +| `initSql(List<String>)` | `initSql` | – | SQL run on each new connection (semicolon separated statements in properties form). See per-connection init below. | |
| 80 | + |
| 81 | +## Pool sizing |
| 82 | + |
| 83 | +| Builder method | Property key | Default | Description | |
| 84 | +|----------------|--------------|---------|-------------| |
| 85 | +| `minConnections(int)` | `minConnections` | `2` | Minimum connections maintained in the pool. | |
| 86 | +| `initialConnections(int)` | `initialConnections` | = `minConnections` | Connections created on startup. Set higher than min for smooth warm-up (Kubernetes). | |
| 87 | +| `maxConnections(int)` | `maxConnections` | `200` | Maximum connections. Threads block (up to `waitTimeout`) when this is reached. | |
| 88 | + |
| 89 | +## Timeouts, trimming and ageing |
| 90 | + |
| 91 | +| Builder method | Property key | Default | Description | |
| 92 | +|----------------|--------------|---------|-------------| |
| 93 | +| `waitTimeoutMillis(int)` | `waitTimeout` | `1000` | Millis a thread waits for a free connection once the pool is at max before throwing `ConnectionPoolExhaustedException`. | |
| 94 | +| `maxInactiveTimeSecs(int)` | `maxInactiveTimeSecs` | `300` | Idle seconds after which a free connection can be trimmed back towards `minConnections`. | |
| 95 | +| `maxAgeMinutes(int)` | `maxAgeMinutes` | `0` (unlimited) | Maximum age of a connection before it is trimmed regardless of activity. | |
| 96 | +| `trimPoolFreqSecs(int)` | `trimPoolFreqSecs` | `59` | How often the background trim check runs. | |
| 97 | + |
| 98 | +## Health checks / heartbeat |
| 99 | + |
| 100 | +| Builder method | Property key | Default | Description | |
| 101 | +|----------------|--------------|---------|-------------| |
| 102 | +| `validateOnHeartbeat(boolean)` | `validateOnHeartbeat` | `true` (`false` in AWS Lambda) | Enable the background heartbeat that validates the pool. | |
| 103 | +| `heartbeatFreqSecs(int)` | *(builder only)* | `30` | How often the heartbeat runs. | |
| 104 | +| `heartbeatTimeoutSeconds(int)` | `heartbeatTimeoutSeconds` | `30` | Query timeout for the heartbeat validation. | |
| 105 | +| `heartbeatSql(String)` | `heartbeatSql` | `Connection.isValid()` / platform default | Explicit validation SQL. Rarely needed — see the validation guide. | |
| 106 | +| `heartbeatMaxPoolExhaustedCount(int)` | *(builder only)* | `10` | Consecutive heartbeat pool-exhaustion detections before the pool is reset (leak recovery). | |
| 107 | + |
| 108 | +See [Connection Validation Best Practices](connection-validation-best-practices.md) for details. |
| 109 | + |
| 110 | +## Leak detection / diagnostics |
| 111 | + |
| 112 | +| Builder method | Property key | Default | Description | |
| 113 | +|----------------|--------------|---------|-------------| |
| 114 | +| `leakTimeMinutes(int)` | `leakTimeMinutes` | `30` | A busy (checked-out) connection older than this is treated as a leak and force-closed during a pool reset. | |
| 115 | +| `captureStackTrace(boolean)` | `captureStackTrace` | `false` | Capture the stack trace when a connection is obtained, to locate leaks. Has a performance cost. | |
| 116 | +| `maxStackTraceSize(int)` | `maxStackTraceSize` | `5` | Number of stack frames reported for busy connections. | |
| 117 | + |
| 118 | +See [Troubleshooting Connection Leaks & Pool Exhaustion](troubleshooting-connection-leaks.md). |
| 119 | + |
| 120 | +## Statement caching |
| 121 | + |
| 122 | +| Builder method | Property key | Default | Description | |
| 123 | +|----------------|--------------|---------|-------------| |
| 124 | +| `pstmtCacheSize(int)` | `pstmtCacheSize` | `300` | `PreparedStatement` cache size, per connection. | |
| 125 | +| `cstmtCacheSize(int)` | `cstmtCacheSize` | `20` | `CallableStatement` cache size, per connection. | |
| 126 | + |
| 127 | +## Lifecycle / startup |
| 128 | + |
| 129 | +| Builder method | Property key | Default | Description | |
| 130 | +|----------------|--------------|---------|-------------| |
| 131 | +| `failOnStart(boolean)` | `failOnStart` | `true` | When `false`, the pool starts even if the database is unavailable (it recovers later via heartbeat). | |
| 132 | +| `offline(boolean)` | `offline` | `false` | Start the pool offline (no connections created until `online()`). | |
| 133 | +| `shutdownOnJvmExit(boolean)` | `shutdownOnJvmExit` | `false` | Register a JVM shutdown hook to close the pool on exit. | |
| 134 | +| `enforceCleanClose(boolean)` | `enforceCleanClose` | `false` | Throw if a dirty (uncommitted) connection is closed. Recommended in tests. See [issue #116](https://github.com/ebean-orm/ebean-datasource/issues/116). | |
| 135 | + |
| 136 | +## Hooks and extension points |
| 137 | + |
| 138 | +| Builder method | Property key | Description | |
| 139 | +|----------------|--------------|-------------| |
| 140 | +| `connectionInitializer(NewConnectionInitializer)` | *(builder only)* | Hook called when each new connection is created (`preInitialize` / `postInitialize`). | |
| 141 | +| `defaultConnectionInitializer(NewConnectionInitializer)` | *(builder only)* | Fallback initializer used only if one is not otherwise set. | |
| 142 | +| `listener(DataSourcePoolListener)` | *(builder only)* | Callbacks on borrow (`onAfterBorrowConnection`) and return (`onBeforeReturnConnection`). | |
| 143 | +| `poolListener(String)` | `poolListener` | Class name of a `DataSourcePoolListener` to instantiate. | |
| 144 | +| `alert(DataSourceAlert)` | *(builder only)* | Callbacks for `dataSourceUp` / `dataSourceDown` (outage alerting). See [Monitoring](monitoring-pool-metrics.md). | |
| 145 | + |
| 146 | +## Per-connection initialization |
| 147 | + |
| 148 | +Use `initSql` for simple statements, or a `NewConnectionInitializer` for programmatic control. This |
| 149 | +is the right place to set things like a Postgres `search_path` or a per-session `statement_timeout`. |
| 150 | + |
| 151 | +```java |
| 152 | +DataSourcePool pool = DataSourcePool.builder() |
| 153 | + .name("mypool") |
| 154 | + .url("jdbc:postgresql://localhost:5432/myapp") |
| 155 | + .username("app_user") |
| 156 | + .password("password") |
| 157 | + .initSql(List.of("set search_path to app, public")) |
| 158 | + .connectionInitializer(new NewConnectionInitializer() { |
| 159 | + @Override |
| 160 | + public void postInitialize(Connection connection) { |
| 161 | + try (Statement st = connection.createStatement()) { |
| 162 | + st.execute("set statement_timeout to '30s'"); |
| 163 | + } catch (SQLException e) { |
| 164 | + throw new IllegalStateException(e); |
| 165 | + } |
| 166 | + } |
| 167 | + }) |
| 168 | + .build(); |
| 169 | +``` |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +## Deprecated `setXxx` methods |
| 174 | + |
| 175 | +Many settings historically used a `setXxx` name (e.g. `setMinConnections`). These remain for |
| 176 | +backwards compatibility but are deprecated — prefer the fluent forms shown above |
| 177 | +(`minConnections`, `maxConnections`, `heartbeatFreqSecs`, etc.). |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## Next Steps |
| 182 | + |
| 183 | +- [Create a DataSource Pool](create-datasource-pool.md) |
| 184 | +- [Connection Validation Best Practices](connection-validation-best-practices.md) |
| 185 | +- [Troubleshooting Connection Leaks & Pool Exhaustion](troubleshooting-connection-leaks.md) |
| 186 | +- [Monitoring Pool Metrics](monitoring-pool-metrics.md) |
| 187 | +- Full Javadoc: [io.ebean:ebean-datasource](https://javadoc.io/doc/io.ebean/ebean-datasource) |
0 commit comments