Skip to content

Commit db9828a

Browse files
authored
feat: support HikariCP custom settings for pg (#208)
1 parent 0ef8237 commit db9828a

7 files changed

Lines changed: 434 additions & 8 deletions

File tree

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ java-cosmos is a client for Azure CosmosDB 's SQL API (also called documentdb fo
2323
<dependency>
2424
<groupId>com.github.thunderz99</groupId>
2525
<artifactId>java-cosmos</artifactId>
26-
<version>0.8.23</version>
26+
<version>0.8.24</version>
2727
</dependency>
2828
```
2929

@@ -502,6 +502,33 @@ db.upsert("Database1", new User("id011","Tom","Banks"), "Collection1");
502502
503503
```
504504

505+
### PostgreSQL HikariCP settings
506+
507+
```java
508+
import io.github.thunderz99.cosmos.impl.postgres.dto.PostgresHikariOptions;
509+
510+
var hikariOptions = new PostgresHikariOptions()
511+
.withMaximumPoolSize(30)
512+
.withMinimumIdle(5)
513+
.withConnectionTimeoutMs(30_000)
514+
.withIdleTimeoutMs(120_000)
515+
.withMaxLifetimeMs(600_000)
516+
.withValidationTimeoutMs(5_000)
517+
.withPoolName("java-cosmos-pool")
518+
// Optional: pass raw Hikari properties for minor/future settings
519+
.withHikariProperty("initializationFailTimeout", "-1")
520+
.withHikariProperty("leakDetectionThreshold", "30000");
521+
522+
var cosmos = new CosmosBuilder()
523+
.withDatabaseType("postgres")
524+
.withConnectionString("jdbc:postgresql://localhost:5432/postgres?user=postgres&password=postgres")
525+
// quick entrypoint for common tuning
526+
.withHikariMaximumPoolSize(30)
527+
// optional: typed + raw custom settings
528+
.withCustomHikariSettings(hikariOptions)
529+
.build();
530+
```
531+
505532
### $ELEM_MATCH queries to match fields in array type field
506533

507534
Dealing with array types in json, we can do a query like this using rawSql to find a child whose grade greater than 5 and gender is "female".

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.thunderz99</groupId>
55
<artifactId>java-cosmos</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.8.23</version>
7+
<version>0.8.24</version>
88
<name>${project.groupId}:${project.artifactId}$</name>
99
<description>A lightweight Azure CosmosDB client for Java</description>
1010
<url>https://github.com/thunderz99/java-cosmos</url>

src/main/java/io/github/thunderz99/cosmos/CosmosBuilder.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.thunderz99.cosmos.impl.cosmosdb.CosmosImpl;
66
import io.github.thunderz99.cosmos.impl.mongo.MongoImpl;
77
import io.github.thunderz99.cosmos.impl.postgres.PostgresImpl;
8+
import io.github.thunderz99.cosmos.impl.postgres.dto.PostgresHikariOptions;
89
import io.github.thunderz99.cosmos.impl.postgres.util.PGSortUtil;
910
import io.github.thunderz99.cosmos.util.Checker;
1011
import org.apache.commons.lang3.StringUtils;
@@ -49,6 +50,11 @@ public class CosmosBuilder {
4950

5051
boolean etagEnabled = false;
5152

53+
/**
54+
* Optional custom HikariCP settings for postgres.
55+
*/
56+
PostgresHikariOptions postgresHikariOptions;
57+
5258
/**
5359
* Specify the dbType( "cosmosdb" or "mongodb" or "postgres")
5460
*
@@ -138,6 +144,35 @@ public CosmosBuilder withEtagEnabled(boolean enabled) {
138144
return this;
139145
}
140146

147+
/**
148+
* Set hikari maximum pool size for postgres.
149+
*
150+
* <p>
151+
* This is a convenient entrypoint for the most common HikariCP tuning.
152+
* </p>
153+
*
154+
* @param maximumPoolSize max number of connections in the pool
155+
* @return this
156+
*/
157+
public CosmosBuilder withHikariMaximumPoolSize(int maximumPoolSize) {
158+
if (this.postgresHikariOptions == null) {
159+
this.postgresHikariOptions = new PostgresHikariOptions();
160+
}
161+
this.postgresHikariOptions.withMaximumPoolSize(maximumPoolSize);
162+
return this;
163+
}
164+
165+
/**
166+
* Set custom hikari settings for postgres.
167+
*
168+
* @param options custom hikari options
169+
* @return this
170+
*/
171+
public CosmosBuilder withCustomHikariSettings(PostgresHikariOptions options) {
172+
this.postgresHikariOptions = options;
173+
return this;
174+
}
175+
141176
/**
142177
* Build the instance representing a Cosmos instance.
143178
*
@@ -156,7 +191,7 @@ public Cosmos build() {
156191
}
157192

158193
if (StringUtils.equals(dbType, POSTGRES)) {
159-
return new PostgresImpl(connectionString, expireAtEnabled, etagEnabled, collate);
194+
return new PostgresImpl(connectionString, expireAtEnabled, etagEnabled, collate, postgresHikariOptions);
160195
}
161196

162197
throw new IllegalArgumentException("Not supported dbType: " + dbType);
@@ -165,4 +200,3 @@ public Cosmos build() {
165200

166201

167202
}
168-

src/main/java/io/github/thunderz99/cosmos/impl/postgres/PostgresImpl.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.github.thunderz99.cosmos.CosmosException;
1010
import io.github.thunderz99.cosmos.dto.CosmosContainerResponse;
1111
import io.github.thunderz99.cosmos.dto.UniqueKeyPolicy;
12+
import io.github.thunderz99.cosmos.impl.postgres.dto.PostgresHikariOptions;
1213
import io.github.thunderz99.cosmos.impl.postgres.util.PGSortUtil;
1314
import io.github.thunderz99.cosmos.impl.postgres.util.TableUtil;
1415
import io.github.thunderz99.cosmos.util.Checker;
@@ -61,8 +62,22 @@ public PostgresImpl(String connectionString) {
6162
}
6263

6364
public PostgresImpl(String connectionString, boolean expireAtEnabled, boolean etagEnabled, String collate) {
65+
this(connectionString, expireAtEnabled, etagEnabled, collate, null);
66+
}
67+
68+
/**
69+
* Build postgres cosmos client with custom hikari settings.
70+
*
71+
* @param connectionString postgres connection string
72+
* @param expireAtEnabled whether enable expireAt feature
73+
* @param etagEnabled whether enable etag feature
74+
* @param collate collation used for postgres sorting
75+
* @param hikariOptions optional custom hikari options
76+
*/
77+
public PostgresImpl(String connectionString, boolean expireAtEnabled, boolean etagEnabled,
78+
String collate, PostgresHikariOptions hikariOptions) {
6479

65-
var pair = parseToHikariConfig(connectionString);
80+
var pair = parseToHikariConfig(connectionString, hikariOptions);
6681
var config = pair.getLeft();
6782
this.account = pair.getRight();
6883

@@ -84,9 +99,21 @@ public PostgresImpl(String connectionString, boolean expireAtEnabled, boolean et
8499
* @return pair of HikariConfig and account(hostName)
85100
*/
86101
public static Pair<HikariConfig, String> parseToHikariConfig(String connectionString) {
102+
return parseToHikariConfig(connectionString, null);
103+
}
104+
105+
/**
106+
* parse connectionString("postgres://user:pass@localhost:5432/database1") to HikariConfig,
107+
* then apply optional custom hikari settings.
108+
*
109+
* @param connectionString postgres connectionString
110+
* @param hikariOptions optional custom hikari options
111+
* @return pair of HikariConfig and account(hostName)
112+
*/
113+
public static Pair<HikariConfig, String> parseToHikariConfig(String connectionString,
114+
PostgresHikariOptions hikariOptions) {
87115

88116
Checker.checkNotBlank(connectionString, "connectionString");
89-
var config = new HikariConfig();
90117
URI uri;
91118
try {
92119
// use URI to parse connectionString
@@ -100,6 +127,8 @@ public static Pair<HikariConfig, String> parseToHikariConfig(String connectionSt
100127
throw new IllegalArgumentException("connectionString not valid for postgres: " + connectionString, e);
101128
}
102129

130+
var config = hikariOptions == null ? new HikariConfig() : new HikariConfig(hikariOptions.getHikariProperties());
131+
103132
String jdbcUrl = String.format("jdbc:postgresql://%s:%d%s",
104133
uri.getHost(), uri.getPort(), uri.getRawPath());
105134

@@ -128,6 +157,10 @@ public static Pair<HikariConfig, String> parseToHikariConfig(String connectionSt
128157
}
129158
config.setDriverClassName("org.postgresql.Driver");
130159

160+
if (hikariOptions != null) {
161+
hikariOptions.applyTo(config);
162+
}
163+
131164
var account = uri.getHost();
132165

133166
return Pair.of(config, account);
@@ -309,4 +342,4 @@ public void closeClient() {
309342
this.getDataSource().close();
310343
}
311344

312-
}
345+
}

0 commit comments

Comments
 (0)