Skip to content

Commit 982997d

Browse files
authored
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
1 parent 4bf7c9f commit 982997d

File tree

2 files changed

+169
-45
lines changed

2 files changed

+169
-45
lines changed

tutorial/markdown/java/spring-boot/spring-boot.md

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,62 @@ git clone https://github.com/couchbase-examples/java-springboot-quickstart.git
4848
### Install Dependencies
4949

5050
```shell
51-
mvn package
51+
mvn clean compile
5252
```
5353

54-
> 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.
5555
5656
### Database Server Configuration
5757

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:
5959

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
6164

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.
6371

6472
### Application Properties
6573

6674
You need to configure the connection details to your Couchbase Server in the `application.properties` file located in the `src/main/resources` directory.
6775

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:
7177

7278
```properties
73-
spring.couchbase.bootstrap-hosts=couchbases://xyz.cloud.couchbase.com
74-
OR
75-
spring.couchbase.bootstrap-hosts=localhost
79+
# Modern Couchbase configuration (Spring Boot 3.5+)
80+
spring.couchbase.connection-string=${DB_CONN_STR}
81+
spring.couchbase.username=${DB_USERNAME}
82+
spring.couchbase.password=${DB_PASSWORD}
83+
spring.couchbase.bucket.name=travel-sample
84+
85+
# Connection optimizations
86+
spring.couchbase.env.timeouts.query=30000ms
87+
spring.couchbase.env.timeouts.key-value=5000ms
88+
spring.couchbase.env.timeouts.connect=10000ms
7689
```
7790

78-
The couchbases protocol is used for secure connections.
91+
### Environment Variables Setup
92+
93+
For security, use a `.env` file in your project root with your actual credentials:
7994

8095
```properties
81-
spring.couchbase.bootstrap-hosts=DB_CONN_STR
82-
spring.couchbase.bucket.user=DB_USERNAME
83-
spring.couchbase.bucket.password=DB_PASSWORD
96+
DB_CONN_STR=couchbases://xyz.cloud.couchbase.com
97+
DB_USERNAME=your_username
98+
DB_PASSWORD=your_password
8499
```
85100

101+
The connection string should be in the following format:
102+
- **Couchbase Capella**: `couchbases://xyz.cloud.couchbase.com` (secure)
103+
- **Local Couchbase**: `couchbase://localhost` (non-secure)
104+
105+
The application uses the `dotenv-java` dependency to automatically load these environment variables.
106+
86107
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).
87108

88109
## Running The Application
@@ -92,7 +113,7 @@ For more information on the spring boot connection string, see [Common Applicati
92113
At this point the application is ready, and you can run it via your IDE or from the terminal:
93114

94115
```shell
95-
mvn spring-boot:run -e -X
116+
mvn spring-boot:run
96117
```
97118

98119
> Note: Either the Couchbase Server must be installed and running on localhost or the connection string must be updated in the `application.properties` file.
@@ -108,7 +129,11 @@ docker build -t java-springboot-quickstart .
108129
Run the Docker image
109130

110131
```sh
111-
docker run -d --name springboot-container -p 8080:8080 java-springboot-quickstart -e DB_CONN_STR=<connection_string> -e DB_USERNAME=<username> -e DB_PASSWORD=<password>
132+
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
112137
```
113138

114139
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
401426

402427
## Running The Tests
403428

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:
405432

406433
```sh
407434
mvn test
408435
```
409436

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+
410467
### Run Individual Tests:
411468

412469
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
431488

432489
## Project Setup Notes
433490

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
435503

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.
437505

438506
## Contributing
439507

tutorial/markdown/java/spring-data/spring-data.md

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ length: 30 Mins
2424

2525
### Prerequisites
2626

27-
To run this prebuild project, you will need:
27+
To run this prebuilt project, you will need:
2828

2929
- [Couchbase Capella](https://www.couchbase.com/products/capella/) cluster with [travel-sample](https://docs.couchbase.com/dotnet-sdk/current/ref/travel-app-data-model.html) bucket loaded.
3030
- To run this tutorial using a self managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster).
@@ -35,7 +35,7 @@ To run this prebuild project, you will need:
3535

3636
### Source Code
3737

38-
The sample source code used in this tutorial is [published on GitHub](https://github.com/couchbase-examples/java-springboot-quickstart).
38+
The sample source code used in this tutorial is [published on GitHub](https://github.com/couchbase-examples/java-springdata-quickstart).
3939
To obtain it, clone the git repository with your IDE or execute the following command:
4040

4141
```shell
@@ -61,8 +61,9 @@ implementation 'org.springdoc:springdoc-openapi-ui:1.6.6'
6161

6262
### Database Server Configuration
6363

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-
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:
6667

6768
```java
6869
@Slf4j
@@ -152,23 +153,36 @@ Please refer to [Managing Connections using the Java SDK with Couchbase Server](
152153

153154
You need to configure the connection details to your Couchbase Server in the application.properties file located in the src/main/resources directory.s
154155

156+
**Modern Spring Boot 3.5+ Configuration:**
157+
155158
```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
159169
```
160170

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
162172

163-
The connection string should be in the following format:
173+
For security, use a `.env` file in your project root:
164174

165175
```properties
166-
spring.couchbase.bootstrap-hosts=couchbases://xyz.cloud.couchbase.com
167-
OR
168-
spring.couchbase.bootstrap-hosts=localhost
176+
DB_CONN_STR=couchbases://xyz.cloud.couchbase.com
177+
DB_USERNAME=your_username
178+
DB_PASSWORD=your_password
169179
```
170180

171-
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.
181+
The connection string formats:
182+
- **Couchbase Capella**: `couchbases://xyz.cloud.couchbase.com` (secure)
183+
- **Local Couchbase**: `couchbase://localhost` (non-secure)
184+
185+
The `couchbases://` protocol is used for secure TLS connections (required for Couchbase Capella). The `couchbase://` protocol is for non-secure local connections.
172186

173187
## Running the Application
174188

@@ -177,13 +191,13 @@ The couchbases protocol is used for secure connections. If you are using Couchba
177191
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.
178192

179193
```sh
180-
gradle bootRun
194+
./gradlew bootRun
181195
```
182196

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:
184198

185199
```sh
186-
./gradew.bat bootRun
200+
.\gradlew.bat bootRun
187201
```
188202

189203
### Using Docker
@@ -362,22 +376,64 @@ For more information, see the [Couchbase Java SDK documentation](https://docs.co
362376

363377
## Running The Tests
364378

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:
366382

367383
```sh
368384
./gradlew test
369385
```
370386

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
407+
- **Enhanced Reliability**: 15-minute timeouts, non-blocking failure handling
408+
- **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+
371419
## Project Setup Notes
372420

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)
374435

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.
381437

382438
## Contributing
383439

@@ -399,7 +455,7 @@ If you would like to add another entity to the APIs, these are the steps to foll
399455

400456
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.
401457

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.
403459

404460
> **NOTE:** Couchbase must be installed and running prior to running the Spring Boot app.
405461

0 commit comments

Comments
 (0)