You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(spring-boot): modernize Couchbase configuration and enhance error handling (#65)
- Updated Maven commands for building and running the application.
- Improved the `CouchbaseConfig` class with better error handling, connection timeout management, and enhanced logging.
- Revised application properties for Spring Boot 3.5+ compatibility, including new connection string formats and timeout settings.
- Added environment variable setup instructions for secure credential management.
- Enhanced integration test setup to ensure reliable execution and visibility into test results.
- Updated GitHub Actions workflow for multi-version testing and improved CI/CD reliability.
Files:
- tutorial/markdown/java/spring-boot/spring-boot.md
- tutorial/markdown/java/spring-data/spring-data.md
> Note: Maven automatically restores packages when building the project. in IntelliJ IDEA or Eclipse depending on IDE configuration.
54
+
> Note: Maven automatically restores packages when building the project in IntelliJ IDEA or Eclipse depending on IDE configuration.
55
55
56
56
### Database Server Configuration
57
57
58
-
-The `CouchbaseConfig` class is a Spring configuration class responsible for setting up the connection to a Couchbase database in a Spring Boot application. It defines two beans:
58
+
The `CouchbaseConfig` class is a Spring configuration class responsible for setting up the connection to a Couchbase database in a Spring Boot application. It has been modernized with improved error handling and resilience:
59
59
60
-
-`getCouchbaseCluster()`: This bean creates and configures a connection to the Couchbase cluster using the provided hostname, username, and password.
60
+
-`getCouchbaseCluster()`: Creates and configures a connection to the Couchbase cluster with:
61
+
- Proper variable scoping to handle connection timeouts gracefully
62
+
- 30-second connection timeout with partial connectivity fallback
63
+
- Enhanced error logging and exception handling
61
64
62
-
-`getCouchbaseBucket(Cluster cluster)`: This bean retrieves a Couchbase bucket from a cluster, ensuring it exists and is ready within a timeout, throwing exceptions if not found or if connection times out.
65
+
-`getCouchbaseBucket(Cluster cluster)`: Retrieves a Couchbase bucket from the cluster with:
66
+
- Bucket existence validation before connection attempts
67
+
- 30-second ready timeout for improved reliability
68
+
- Comprehensive error handling for various failure scenarios
69
+
70
+
**Key Improvements**: The configuration now handles connection timeouts more gracefully and provides better error diagnostics for troubleshooting connection issues.
63
71
64
72
### Application Properties
65
73
66
74
You need to configure the connection details to your Couchbase Server in the `application.properties` file located in the `src/main/resources` directory.
67
75
68
-
In the connection string, replace `DB_CONN_STR` with the connection string of your Couchbase cluster. Replace `DB_USERNAME` and `DB_PASSWORD` with the username and password of a Couchbase user with access to the bucket.
69
-
70
-
The connection string should be in the following format:
76
+
The modern Spring Boot 3.5+ configuration uses updated property names:
The application uses the `dotenv-java` dependency to automatically load these environment variables.
106
+
86
107
For more information on the spring boot connection string, see [Common Application Properties](https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html).
87
108
88
109
## Running The Application
@@ -92,7 +113,7 @@ For more information on the spring boot connection string, see [Common Applicati
92
113
At this point the application is ready, and you can run it via your IDE or from the terminal:
93
114
94
115
```shell
95
-
mvn spring-boot:run -e -X
116
+
mvn spring-boot:run
96
117
```
97
118
98
119
> Note: Either the Couchbase Server must be installed and running on localhost or the connection string must be updated in the `application.properties` file.
docker run -d --name springboot-container -p 8080:8080 \
133
+
-e DB_CONN_STR=<connection_string>\
134
+
-e DB_USERNAME=<username>\
135
+
-e DB_PASSWORD=<password>\
136
+
java-springboot-quickstart
112
137
```
113
138
114
139
Note: The `application.properties` file has the connection information to connect to your Capella cluster. You can also pass the connection information as environment variables to the Docker container.
@@ -401,12 +426,44 @@ Once the query is executed, the `AirlineController` constructs an HTTP response
401
426
402
427
## Running The Tests
403
428
404
-
This command will execute all the test cases in your project.
429
+
### Integration Tests
430
+
431
+
This project uses Maven with proper integration test configuration. The tests connect to a real Couchbase instance and validate actual functionality:
405
432
406
433
```sh
407
434
mvn test
408
435
```
409
436
437
+
**Important**: This project was modernized to ensure integration tests actually execute (no false positives). The Maven surefire plugin configuration was updated to include integration tests that were previously excluded.
438
+
439
+
### Test Validation
440
+
441
+
To verify tests are actually running:
442
+
1. Tests should show connection logs to Couchbase
443
+
2. Test count should show actual execution (e.g., "Tests run: 18")
444
+
3. Any test failures will show real assertion errors
445
+
446
+
## Continuous Integration
447
+
448
+
### GitHub Actions
449
+
450
+
The project includes a modern GitHub Actions workflow (`.github/workflows/tests.yaml`) with:
451
+
452
+
-**Multiple Java Versions**: Tests on Java 17 and 21
453
+
-**Manual Triggering**: `workflow_dispatch` allows manual test runs
454
+
-**Reliability Improvements**: 15-minute timeouts, fail-fast disabled for complete results
455
+
-**Test Artifacts**: Automatic upload of test reports for debugging
456
+
-**Simplified Commands**: Uses `./mvnw clean test` for focused testing
457
+
458
+
### Running in CI/CD
459
+
460
+
The workflow automatically:
461
+
1. Sets up Java with Temurin distribution
462
+
2. Caches Maven dependencies for faster builds
463
+
3. Executes integration tests against Couchbase
464
+
4. Uploads test results as artifacts
465
+
5. Sends failure notifications to Slack (if configured)
466
+
410
467
### Run Individual Tests:
411
468
412
469
Additionally, you can run individual test classes or methods using the following commands:
@@ -431,9 +488,20 @@ mvn test -Dtest=org.couchbase.quickstart.springboot.controllers.RouteIntegration
431
488
432
489
## Project Setup Notes
433
490
434
-
This project was based on the standard [Spring Boot project](https://spring.io/guides/gs/rest-service/).
491
+
This project was based on the standard [Spring Boot project](https://spring.io/guides/gs/rest-service/) with the following specifications:
492
+
493
+
-**Spring Boot Version**: 3.5.5 (modernized from earlier versions)
494
+
-**Java Version**: 17+ (updated from Java 8)
495
+
-**Build Tool**: Maven with wrapper (`mvnw`)
496
+
-**Key Dependencies**:
497
+
- Spring Boot Starter Web
498
+
- Couchbase Java SDK 3.7.9
499
+
- Spring Boot Validation
500
+
- SpringDoc OpenAPI (Swagger)
501
+
- Dotenv Java for environment variable management
502
+
- Project Lombok for reduced boilerplate
435
503
436
-
A full list of packages are referenced in the `pom.xml` file.
504
+
A full list of packages and versions are referenced in the `pom.xml` file.
Spring Data couchbase connector can be configured by providing a `@Configuration`[bean](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-definition) that extends [`AbstractCouchbaseConfiguration`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.html).
65
-
The sample application provides a configuration bean that uses default couchbase login and password:
64
+
Spring Data Couchbase connector can be configured by providing a `@Configuration`[bean](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-definition) that extends [`AbstractCouchbaseConfiguration`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.html).
65
+
66
+
The sample application provides a modernized configuration bean with improved error handling and environment variable support:
66
67
67
68
```java
68
69
@Slf4j
@@ -152,23 +153,36 @@ Please refer to [Managing Connections using the Java SDK with Couchbase Server](
152
153
153
154
You need to configure the connection details to your Couchbase Server in the application.properties file located in the src/main/resources directory.s
154
155
156
+
**Modern Spring Boot 3.5+ Configuration:**
157
+
155
158
```properties
156
-
spring.couchbase.bootstrap-hosts=DB_CONN_STR
157
-
spring.couchbase.bucket.user=DB_USERNAME
158
-
spring.couchbase.bucket.password=DB_PASSWORD
159
+
# Modern Couchbase configuration
160
+
spring.couchbase.connection-string=${DB_CONN_STR}
161
+
spring.couchbase.username=${DB_USERNAME}
162
+
spring.couchbase.password=${DB_PASSWORD}
163
+
spring.couchbase.bucket.name=travel-sample
164
+
165
+
# Connection optimizations
166
+
spring.couchbase.env.timeouts.query=30000ms
167
+
spring.couchbase.env.timeouts.key-value=5000ms
168
+
spring.couchbase.env.timeouts.connect=10000ms
159
169
```
160
170
161
-
In the connection string, replace `DB_CONN_STR` with the connection string of your Couchbase cluster. Replace `DB_USERNAME` and `DB_PASSWORD` with the username and password of a Couchbase user with access to the bucket.
171
+
### Environment Variables Setup
162
172
163
-
The connection string should be in the following format:
173
+
For security, use a `.env` file in your project root:
The couchbases protocol is used for secure connections. If you are using Couchbase Server 6.5 or earlier, you should use the couchbase protocol instead.
The `couchbases://` protocol is used for secure TLS connections (required for Couchbase Capella). The `couchbase://` protocol is for non-secure local connections.
172
186
173
187
## Running the Application
174
188
@@ -177,13 +191,13 @@ The couchbases protocol is used for secure connections. If you are using Couchba
177
191
At this point, we have installed the dependencies, loaded the travel-sample data and configured the application with the credentials. The application is now ready and you can run it.
178
192
179
193
```sh
180
-
gradle bootRun
194
+
./gradlew bootRun
181
195
```
182
196
183
-
Note: If you're using Windows, you can run the application using the `gradle.bat` executable.
197
+
Note: If you're using Windows, you can run the application using the Gradle wrapper batch file:
184
198
185
199
```sh
186
-
./gradew.bat bootRun
200
+
.\gradlew.bat bootRun
187
201
```
188
202
189
203
### Using Docker
@@ -362,22 +376,64 @@ For more information, see the [Couchbase Java SDK documentation](https://docs.co
362
376
363
377
## Running The Tests
364
378
365
-
To run the tests, execute `./gradlew test` (`./gradlew.bat test` on Windows).
379
+
### Integration Tests
380
+
381
+
This project uses Gradle with comprehensive integration test setup. The tests connect to a real Couchbase instance:
366
382
367
383
```sh
368
384
./gradlew test
369
385
```
370
386
387
+
**Windows users:**
388
+
```sh
389
+
.\gradlew.bat test
390
+
```
391
+
392
+
### Test Reliability
393
+
394
+
This project was modernized to ensure reliable test execution:
395
+
- Tests properly connect to Couchbase and validate real data
396
+
- Integration tests are included in the standard test run
397
+
- Test logging provides clear visibility into execution
398
+
399
+
## Continuous Integration
400
+
401
+
### GitHub Actions
402
+
403
+
The project includes a modern GitHub Actions workflow with:
404
+
405
+
-**Multi-Version Testing**: Java 17 and 21 support
406
+
-**Manual Triggering**: `workflow_dispatch` for on-demand runs
-**Test Artifacts**: Automatic test report uploads
409
+
-**Optimized Commands**: Uses `./gradlew clean test --stacktrace`
410
+
411
+
### CI/CD Features
412
+
413
+
The workflow provides:
414
+
1. Temurin JDK setup with Gradle caching
415
+
2. Real integration test execution against Couchbase
416
+
3. Test result artifact collection for debugging
417
+
4. Slack notification integration (configurable)
418
+
371
419
## Project Setup Notes
372
420
373
-
This project was created using the [Spring Initializr](https://start.spring.io/) with the following options:
421
+
This project was created using the [Spring Initializr](https://start.spring.io/) and modernized with:
422
+
423
+
-**Project**: Gradle Project with Wrapper
424
+
-**Language**: Java
425
+
-**Spring Boot**: 3.5.5 (upgraded from 2.7.18)
426
+
-**Packaging**: Jar
427
+
-**Java Version**: 17+ (upgraded from Java 8)
428
+
-**Key Dependencies**:
429
+
- Spring Boot Starter Web
430
+
- Spring Boot Starter Data Couchbase
431
+
- SpringDoc OpenAPI Starter WebMVC UI
432
+
- Spring Boot Starter Validation
433
+
- Project Lombok
434
+
- JUnit 5 (Jupiter)
374
435
375
-
- Project: Gradle Project
376
-
- Language: Java
377
-
- Spring Boot: 2.7.18
378
-
- Packaging: Jar
379
-
- Java: 8
380
-
- Dependencies: Spring Web, Spring Data Couchbase, Springdoc OpenAPI UI
436
+
**Performance Optimizations**: The project uses `Slice` instead of `Page` for better memory efficiency and eliminates expensive `COUNT` queries in pagination scenarios.
381
437
382
438
## Contributing
383
439
@@ -399,7 +455,7 @@ If you would like to add another entity to the APIs, these are the steps to foll
399
455
400
456
If you are running this quickstart with a self managed Couchbase cluster, you need to [load](https://docs.couchbase.com/server/current/manage/manage-settings/install-sample-buckets.html) the travel-sample data bucket in your cluster and generate the credentials for the bucket.
401
457
402
-
You need to update the connection string and the credentials in the [`src/main/resources/application.properties`](https://github.com/couchbase-examples/java-springboot-quickstart/blob/main/src/main/resources/application.properties) file.
458
+
You need to update the connection string and the credentials in the [`src/main/resources/application.properties`](https://github.com/couchbase-examples/java-springdata-quickstart/blob/main/src/main/resources/application.properties) file.
403
459
404
460
> **NOTE:** Couchbase must be installed and running prior to running the Spring Boot app.
0 commit comments