# Databases in the development process

{% hint style="info" %}

#### Databases are used to persist data

{% endhint %}

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FT9Tdsegtxv6xMYufVve4%2Fimage.png?alt=media\&token=3e668bda-d29e-4760-9fff-775853878581)

&#x20;  ▪ Users can create, read, update or delete entries in a database&#x20;

&#x20;  ▪ Also called CRUD operations

&#x20;  ▪ Developers need a database for local development&#x20;

&#x20;        ● Connect to a development database to develop the new features&#x20;

&#x20;        ● Connect to a test database to test the new feature with realistic data

## 🟢 There are 2 ways for developers to work with DB

**▪ OPTION 1**

&#x20;        ● Each developer installs DB locally&#x20;

&#x20;        ● Each developer has own DB with own test data

#### &#x20;  **▪** Advantages & Disadvantages

&#x20;        ✅ Can't mess up someone else's test&#x20;

&#x20;        ❌ DB data start from empty DB | need realistic test data&#x20;

&#x20;        ❌ Install and setup DB for EVERY developer

**▪ OPTION 2**

&#x20;        ● Shared DB hosted remotely

#### &#x20;  **▪** Advantages & Disadvantages

&#x20;        ✅ No need for local installation | start coding right away&#x20;

&#x20;        ✅ Test data available right from the start&#x20;

&#x20;        ❌ Can't play around without affecting others!

## 🟢 Configure Database Connection

### How does the application talk to the database?

&#x20;  **▪** The DB connection is configured in the application's code&#x20;

&#x20;  **▪** Each programming language has a library/module for DB connection

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2F788XNJFjfnfAtFxeTYMc%2Fimage.png?alt=media\&token=7cf05ec2-fa0e-40bf-a234-7ca9f98c4b21)

![Java Code connecting to a Mysql database](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2Fb0Ovunju3y0PQooxBN6e%2Fimage.png?alt=media\&token=3d944ea1-133c-4553-a4cd-fae90477bad4)

{% hint style="success" %}
Better: Do not hardcode database connection data in source code
{% endhint %}

{% hint style="success" %}

#### Best Practice: DON'T check in credentials in code

{% endhint %}

&#x20;        ✅ Define only variables in code&#x20;

&#x20;        ✅ Set endpoint and credentials from outside for each environment (for example dev, test, prod)&#x20;

&#x20;        ✅ Depending on environment (dev, test, prod) connects to different DB!

{% hint style="info" %}
The way to do this, is to pass environment variables on application start-up
{% endhint %}

### How to pass environment variables:

1. From Command Line
2. Configure in code editor&#x20;
3. Use properties/configuration files

{% hint style="success" %}
The best option is to pass them via a properties or configuration file
{% endhint %}

&#x20;  **▪** In a Java/Spring application, you can define the values in a Spring properties file:

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FfZ1iPvUkWbmEi5j5xZNF%2Fimage.png?alt=media\&token=f578d8df-33f6-405f-bcc9-30609d5c7fa6)

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FtmtJKkUQIA8WGa9xAtyJ%2FScreen%20Shot%202022-05-21%20at%2012.50.19.png?alt=media\&token=dc8f4c68-6e07-478c-821c-e7903acbf3af)

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2Ff2KBTTQPwyHgdqs6jJAX%2Fimage.png?alt=media\&token=75c13c91-d92c-4d37-a823-c414ab7e2605)

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2F1B9E11glm9V7jl8n1HlR%2Fimage.png?alt=media\&token=75dae775-e8a8-4c42-97f6-aa7de3323266)

## Databases in Production

&#x20;  **▪** Application connects to database&#x20;

&#x20;  **▪** This means database needs to be available, when application starts&#x20;

&#x20;  **▪** So before application is deployed, we need to install and configure database

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2F7bY28DJgTmtLHfuE03K7%2Fimage.png?alt=media\&token=48075497-4b1d-4f73-bdf1-81f765c36445)

### 🟥 <mark style="color:red;">Data is important</mark>, <mark style="color:red;">so we need to secure them:</mark>

&#x20;        ✅ Replicate database&#x20;

&#x20;        ✅ Do regular backups&#x20;

&#x20;        ✅ Make sure it performs under high load when many users accessing the application

#### This may be done by:

&#x20;        **▪** System Administrator&#x20;

&#x20;        **▪** Database Engineer&#x20;

&#x20;        **▪** DevOps Engineer

![](https://2601183865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8QpIzz6VwCOTMwaqKYa3%2Fuploads%2FY8yVzkhDXwCvAihVeQ7G%2FScreen%20Shot%202022-05-21%20at%2014.07.49.png?alt=media\&token=92faccc8-b4e2-4ecb-8988-c0e06132717e)
