Skip to content

Commit 0baacb9

Browse files
author
JesusGuzmanJr
committed
fix database connection issues
1 parent 6533e71 commit 0baacb9

File tree

6 files changed

+59
-34
lines changed

6 files changed

+59
-34
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Rust earned the top spot as the “most-loved” programming language for the fi
2020
| ------------------ | ------------------------------------------------------ | -------------------------------------------- |
2121
| domain name | java-car-rentals.marzipan.club | rust-car-rentals.marzipan.club |
2222
| health endpoint | https://java-car-rentals.marzipan.club/health | https://rust-car-rental.marzipan.club/health |
23-
| language | [Java SE 8] ([OpenJDK 8 8u292-b10]) | [Rust 2018] ([1.53.0]) |
24-
| compiler | java-1.8.0-openjdk-devel | rustc 1.53.0 (53cb7b09b 2021-06-17) |
23+
| language | [Java SE 8] ([OpenJDK 8 8u292-b10]) | [Rust 2018] ([1.54.0]) |
24+
| compiler | java-1.8.0-openjdk-devel | rustc 1.54.0 (a178d0322 2021-07-26) |
2525
| compilation target | java bytecode 52.0 | x86_64-unknown-linux-musl |
2626
| runtime | [OpenJDK] ([java-1.8.0-openjdk]) | [Tokio] ([0.2.25]) |
2727
| web framework | [Apache Tomcat] ([8.5.69]) and [Spring Boot] ([2.5.2]) | [Actix Web] ([3.3.2]) |

java/src/main/resources/application.properties

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
# logging
2+
logging.level=INFO
3+
logging.level.club.marzipan.javacarrentals=TRACE
4+
5+
# http
16
server.address=localhost
27
server.port=8080
8+
server.tomcat.max-http-form-post-size=4KB
9+
server.shutdown=graceful
10+
spring.lifecycle.timeout-per-shutdown-phase=3s
11+
12+
# database
13+
spring.datasource.url=jdbc:postgresql://hattie.db.elephantsql.com:5432/emmfovaj?stringtype=unspecified&sslmode=require&user=emmfovaj&password=XVnuN0br7UusH_El55gxWHEnkxZ-ZFfq
14+
spring.datasource.hikari.maximumPoolSize=2
15+
spring.datasource.hikari.minimumIdle=2
16+
spring.datasource.hikari.maxLifetime=600
17+
spring.datasource.hikari.idleTimeout=3000
18+
spring.datasource.data-source-class-name=org.postgresql.ds.PGSimpleDataSource
19+
20+
# miscellaneous
321
management.info.git.mode=full
4-
spring.datasource.driver-class-name=org.postgresql.Driver
5-
spring.datasource.url=jdbc:postgresql://localhost:5432/car_rentals?stringtype=unspecified
6-
logging.level.org.springframework=OFF

rust/Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/config.ron

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
(
2+
// logging
3+
logging_directive: "info,rust-car-service=trace",
4+
5+
// http
26
address: "localhost",
37
port: 9090,
4-
logging_directive: "info,rust-car-service=trace,actix_web=debug",
5-
graceful_shutdown_timeout_sec: 3,
68
max_payload_size_bytes: 4096,
7-
postgres_connection_pool_size: 1,
8-
postgres_connection_timeout_sec: 1,
9-
postgres_connection_uri: "postgres://localhost:5432/car_rentals",
9+
graceful_shutdown_timeout_sec: 3,
10+
11+
// database
12+
postgres_uri: "postgres://hattie.db.elephantsql.com/emmfovaj?sslmode=require&user=emmfovaj&password=XVnuN0br7UusH_El55gxWHEnkxZ-ZFfq",
13+
postgres_max_pool_size: 2,
14+
postgres_min_pool_size: 2,
15+
postgres_max_life_time_minutes: 10,
16+
postgres_timeout_sec: 3,
17+
1018
)

rust/src/config.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,37 @@ static CONFIG: OnceCell<Config> = OnceCell::new();
99

1010
#[derive(Deserialize)]
1111
pub struct Config {
12+
/// Enable custom logging; acceptable directive must follow the following
13+
/// form https://docs.rs/env_logger/0.8.3/env_logger/index.html#enabling-logging.
14+
pub logging_directive: String,
15+
1216
/// The IPv4 address to listen to.
1317
pub address: String,
1418

1519
/// The tcp port to bind to.
1620
pub port: u16,
1721

18-
/// Enable custom logging; acceptable directive must follow the following
19-
/// form https://docs.rs/env_logger/0.8.3/env_logger/index.html#enabling-logging.
20-
pub logging_directive: String,
22+
/// The maximum size of an acceptable http request payload before responding
23+
/// with an error.
24+
pub max_payload_size_bytes: usize,
2125

2226
/// The number of seconds for workers to shutdown gracefully.
2327
pub graceful_shutdown_timeout_sec: u64,
2428

25-
/// The maximum size of an acceptable http request payload before responding
26-
/// with an error.
27-
pub max_payload_size_bytes: usize,
29+
/// The Postgres connection uri.
30+
pub postgres_uri: String,
2831

2932
/// The maximum number of active postgres connections to pool.
30-
pub postgres_connection_pool_size: u32,
33+
pub postgres_max_pool_size: u32,
3134

32-
/// The postgres connection timeout in seconds.
33-
pub postgres_connection_timeout_sec: u64,
35+
/// The minimum number of active postgres connections to maintain in the pool.
36+
pub postgres_min_pool_size: u32,
3437

35-
/// The Postgres connection uri.
36-
pub postgres_connection_uri: String,
38+
// The maximum lifetime of a connection.
39+
pub postgres_max_life_time_minutes: u64,
40+
41+
/// The Postgres connection timeout in seconds.
42+
pub postgres_timeout_sec: u64,
3743
}
3844

3945
fn init() -> Config {

rust/src/persistance.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,24 @@ static POOL: OnceCell<Pool> = OnceCell::new();
66

77
pub async fn init() {
88
let config = crate::config::app_config();
9-
let pool_size = config.postgres_connection_pool_size;
109

11-
let connection_timeout = std::time::Duration::new(config.postgres_connection_timeout_sec, 0);
10+
let connection_timeout = std::time::Duration::new(config.postgres_timeout_sec, 0);
11+
let max_lifetime = std::time::Duration::new(config.postgres_max_life_time_minutes * 60, 0);
1212

1313
// panic if connection pool can't be created
1414
let pool = sqlx::postgres::PgPoolOptions::new()
15-
.max_connections(pool_size)
15+
.max_connections(config.postgres_max_pool_size)
16+
.min_connections(config.postgres_min_pool_size)
1617
.connect_timeout(connection_timeout)
17-
.connect(&config.postgres_connection_uri)
18+
.max_lifetime(max_lifetime)
19+
.connect(&config.postgres_uri)
1820
.await
1921
.expect("Couldn't create postgres connection pool.");
2022

2123
pool.acquire().await.expect("Couldn't connect to postgres.");
2224

2325
POOL.set(pool)
2426
.expect("Postgres connection pool is already initialized.");
25-
26-
log::info!(
27-
"Started {} connection{} to Postgres.",
28-
pool_size,
29-
if pool_size == 1 { "" } else { "s" }
30-
);
3127
}
3228

3329
/// Returns a reference to the database connection pool.

0 commit comments

Comments
 (0)