Skip to content

eclipse-ecsp/sql-dao

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

88 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

SQL DAO library

Build And Sonar scan License Compliance Deployment

sql-dao provides HikariCP pooled connections to the Postgres DB. It creates, manages and authenticates connection to Postgres DB. Retry on failure in obtaining a connection is implemented with the configurable retry count. Graceful shutdown of the connection is ensured whenever a connection is closed.

Apart from providing connections, it also provides the below functionalities to a service.

  1. Health Monitoring of Postgres DB.
  2. A JDBC template provider to execute queries using JDBC.
  3. Extensible Credentials provider to fetch credentials from Vault or from environment properties or a customized implementation.
  4. Metrics exporter to export DB connection pool metrics to Prometheus for reliability.

Table of Contents

Getting Started

To build the project in the local working directory after the project has been cloned/forked, run:

mvn clean install

from the command line interface.

Prerequisites

  1. Maven
  2. Java 11

Installation

How to set up maven

Install Java

Running the tests

mvn test

Or run a specific test

mvn test -Dtest="TheFirstUnitTest"

To run a method from within a test

mvn test -Dtest="TheSecondUnitTest#whenTestCase2_thenPrintTest2_1"

Deployment

sql-dao-library project serves as a library for the services. It is not meant to be deployed as a service in any cloud environment.

Usage

Add the following dependency in the target project

<dependency>
  <groupId>org.eclipse.ecsp</groupId>
  <artifactId>sql-dao</artifactId>
  <version>1.X.X</version>
</dependency>

Specifying Postgres DB connection properties

The Postgres DB connection properties can be specified in the environment properties as shown below:

postgres.jdbc.url=jdbc:postgresql://localhost:5432/postgres
postgres.username=********
postgres.password=********

Using the JdbcTemplate to execute queries

The JdbcTemplate can be autowired in the implementing service to execute queries as shown below:

import org.springframework.beans.factory.annotation.Qualifier;

@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;

@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public void executeQuery() {
    jdbcTemplate.query("SELECT * FROM table_name", (rs, rowNum) -> {
        // Process the result set
        return null;
    });
}

Health Monitoring

The health of the Postgres DB is monitored by the PostgresDBHealthMonitor class. To enabled health monitoring, the below configuration needs to be added in the environment properties:

health.postgresdb.monitor.enabled=true

## If graceful restart is required
health.postgresdb.monitor.restart.on.failure=true

Publishing Metrics to Prometheus server

To publish Postgres DB pool metrics to Prometheus server, the below configuration needs to be added in the environment properties:

## To enable postgres DB metrics fetching from PostgresDB connection pool 
postgresdb.metrics.enabled=true

## To enable Prometheus metrics publishing
metrics.prometheus.enabled=true

## Prometheus agent port configuration
prometheus.agent.port=9100
prometheus.agent.port.exposed=9100

Built With Dependencies

Dependency Purpose
Ignite Utils For logging & health monitoring
Prometheus HTTP server For metrics publishing
postgresql For Postgres DB connection
HikariCP For connection pooling
Spring Framework The core spring support
Spring Boot The web framework used
Maven Dependency Management
Junit Testing framework
Mockito Test Mocking framework

How to contribute

Please read CONTRIBUTING.md for details on our contribution guidelines, and the process for submitting pull requests to us.

Code of Conduct

Please read CODE_OF_CONDUCT.md for details on our code of conduct.

Authors

Kaushal Arora
Kaushal Arora

๐Ÿ“– ๐Ÿ‘€

See also the list of contributors who participated in this project.

Security Contact Information

Please read SECURITY.md to raise any security related issues.

Support

Please write to us at csp@harman.com

Troubleshooting

Please read CONTRIBUTING.md for details on how to raise an issue and submit a pull request to us.

License

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

Announcements

All updates to this library are documented in our Release Notes and releases. For the versions available, see the tags on this repository.

About

ECSP SQL DAO library

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages