Skip to content

database management

Shelson Ferrari edited this page Jul 23, 2024 · 5 revisions

Database Management

The project uses the H2 database for development and testing purposes. Below are strategies and practices for efficient management of the database.

H2 Configuration

The H2 database is an embedded in-memory database that is easy to configure and use. It allows for quick testing and development without needing an external database server.

Settings in application.properties

# H2 Console
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.h2.console.settings.web-allow-others=true

# Datasource
spring.datasource.url=jdbc:h2:mem:java_base;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

Accessing the H2 Console

The H2 console is a web interface that allows you to execute queries and manage the in-memory database.

Accessing the Console via Web

Start the application.

Open the browser and go to http://localhost:8080/h2-console.

Connection Settings

Use the following settings to connect to the H2 database:

  • JDBC URL: jdbc:h2:mem:java_base
  • User Name: sa
  • Password: (leave blank)

Executing Queries in H2

After connecting to the H2 console, you can execute SQL queries to view and manipulate the data.

Query Example

SELECT * FROM CURRENCY_CONVERSIONS;

Data Persistence

The in-memory H2 database is volatile, and all data will be lost upon restarting the application. To persist data, you need to configure a persistent database, such as PostgreSQL or MySQL.

Configuring a Persistent Database

To configure a persistent database, you should change the datasource properties in application.properties to point to the desired database. For example, to use PostgreSQL:

spring.datasource.url=jdbc:postgresql://localhost:5432/java_base
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=your-username
spring.datasource.password=your-password
spring.jpa.hibernate.ddl-auto=update

Ways to Access the H2 Database

Access via Command Terminal

Using CURL

curl -s -X GET "http://localhost:8080/api/v1/conversions/convert?source=BRL&target=EUR&amount=33" -H "Accept: application/json" | jq

Access via Postman

Import the file postman_collection.json in Postman to use the application.

Java Swing

mvn clean compile
mvn exec:java -Dexec.mainClass="com.sognisport.swing.CurrencyConverterSwingApp"

Final Considerations

The H2 database is an excellent tool for development and testing due to its simplicity and ease of configuration. However, for production environments, it is recommended to use a more robust and persistent relational database, such as PostgreSQL or MySQL.

Benefits of Using H2

  • Easy configuration and use
  • Ideal for quick development and testing
  • No need for additional software installation

Limitations of Using H2

  • Data does not persist between restarts
  • Not recommended for production environments

Conclusion

The H2 database is a powerful tool for development, providing a quick and efficient way to test the application and manage temporary data. By following these instructions, you can configure, access, and use H2 effectively in your project.


Wiki Menu

Wiki Main Page

1. Introduction to the Project

  • Overview: Presentation of the project, highlighting its purpose and the context in which it is embedded.
  • Project Objectives: Enumeration of the main objectives that the project aims to achieve.
  • Scope and Functionalities: Description of the main functionalities offered by the project and its scope of operation.

2. Configuration and Installation

3. Project Structure

  • Folder Structure: Description of the organization of the project directories.
  • Project Architecture: Explanation of the architecture used, including design patterns and technical decisions.

4. Development

  • Development Flow: Description of the development process adopted, including planning, coding, and review stages.
  • Apache Camel Integration: Guide on integrating Apache Camel into the project, including configuration and usage.
  • Contributors and Authors: Recognition of the contributors to the project.
  • Contributions: Guidelines on how to contribute to the project, including code standards and pull request requirements, tips and best practices.
  • Code of Conduct: Behavioral guidelines expected for the project community.

5. API and Documentation

6. Endpoints and Database

  • Endpoint Description: Details of the available API endpoints, including methods, parameters, and usage examples.
  • Database Management.

7. Testing

  • Testing Strategies: Approach and methods used to test the software, including unit, integration, and E2E tests.
  • Testing Tools: Description of the testing tools used in the project and how to configure them.

8. CI/CD and Automations

  • CI/CD Pipeline: Explanation of the continuous integration and delivery pipeline, detailing each stage and its function.
  • Automations and Artifact Generation: Description of the automations incorporated into the CI/CD, including documentation generation and build artifacts.

9. Configuration Files

10. Best Practices

11. Legal and Licensing

  • Licensing: Information about the rights and restrictions associated with the use of the software.
  • Terms of Use: Information about the terms and conditions for using the software.

12. Projections and Innovations

  • Future Plans: Discussion on functionalities and improvements considered for future versions of the project.
  • Improvement Proposals: Space for the community to suggest and debate improvements and innovations.

13. Attachments and Useful Links

14. Security

  • Security Policy: Details on the supported versions, reporting vulnerabilities, and general security practices.

Clone this wiki locally