Skip to content

Commit c3b51a3

Browse files
authored
Merge pull request #205239 from saimicrosoft/hsc_app_stacks_extended_latest
HSC - Connection pooling to App Stacks docs
2 parents 21644a3 + 862b287 commit c3b51a3

File tree

5 files changed

+334
-243
lines changed

5 files changed

+334
-243
lines changed

articles/postgresql/hyperscale/howto-app-stacks-csharp.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: saimicrosoft
66
ms.service: postgresql
77
ms.subservice: hyperscale-citus
88
ms.topic: how-to
9-
ms.date: 06/20/2022
9+
ms.date: 07/26/2022
1010
---
1111

1212
# C# app to connect and query Hyperscale (Citus)
@@ -38,9 +38,11 @@ To get the database credentials, you can use the **Connection strings** tab in t
3838
Use the following code to connect and load the data using CREATE TABLE and INSERT INTO SQL statements. The code uses these `NpgsqlCommand` class methods:
3939

4040
* [Open()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_Open) to establish a connection to Hyperscale (Citus),
41-
* [CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) to set the CommandText property,
41+
* [CreateCommand()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html#Npgsql_NpgsqlConnection_CreateCommand) to set the CommandText property
4242
* [ExecuteNonQuery()](https://www.npgsql.org/doc/api/Npgsql.NpgsqlCommand.html#Npgsql_NpgsqlCommand_ExecuteNonQuery) to run database commands.
4343

44+
[!INCLUDE[why-connection-pooling](includes/why-connection-pooling.md)]
45+
4446
```csharp
4547
using System;
4648
using Npgsql;
@@ -52,7 +54,7 @@ namespace Driver
5254
static void Main(string[] args)
5355
{
5456
// Replace below argument with connection string from portal.
55-
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require;");
57+
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require; Pooling = true; Minimum Pool Size=0; Maximum Pool Size =50 ");
5658

5759
connStr.TrustServerCertificate = true;
5860

@@ -113,7 +115,7 @@ namespace Driver
113115
static void Main(string[] args)
114116
{
115117
// Replace below argument with connection string from portal.
116-
var connStr = new NpgsqlConnectionStringBuilder("Server = <host>; Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require;");
118+
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require; Pooling = true; Minimum Pool Size=0; Maximum Pool Size =50");
117119

118120
connStr.TrustServerCertificate = true;
119121

@@ -155,11 +157,11 @@ namespace Driver
155157
static void Main(string[] args)
156158
{
157159
// Replace below argument with connection string from portal.
158-
var connStr = new NpgsqlConnectionStringBuilder("Server = <host>; Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require;");
160+
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require; Pooling = true; Minimum Pool Size=0; Maximum Pool Size =50 ");
159161

160162
connStr.TrustServerCertificate = true;
161163

162-
using (var conn = new NpgsqlConnection(connString))
164+
using (var conn = new NpgsqlConnection(connStr))
163165
{
164166
Console.Out.WriteLine("Opening connection");
165167
conn.Open();
@@ -203,7 +205,7 @@ namespace Driver
203205
static void Main(string[] args)
204206
{
205207
// Replace below argument with connection string from portal.
206-
var connStr = new NpgsqlConnectionStringBuilder("Server = <host>; Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require;");
208+
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require; Pooling = true; Minimum Pool Size=0; Maximum Pool Size =50 ");
207209

208210
connStr.TrustServerCertificate = true;
209211

@@ -241,7 +243,7 @@ namespace Driver
241243
static void Main(string[] args)
242244
{
243245
// Replace below argument with connection string from portal.
244-
var connStr = new NpgsqlConnectionStringBuilder("Server = <host>; Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require;");
246+
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require; Pooling = true; Minimum Pool Size=0; Maximum Pool Size =50 ");
245247

246248
connStr.TrustServerCertificate = true;
247249

@@ -285,7 +287,7 @@ public class csvtotable
285287
String sFromFilePath = "C:\\Users\\Documents\\pharmacies.csv";
286288

287289
// Replace below argument with connection string from portal.
288-
var connStr = new NpgsqlConnectionStringBuilder("Server = <host>; Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require;");
290+
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require; Pooling = true; Minimum Pool Size=0; Maximum Pool Size =50 ");
289291

290292
connStr.TrustServerCertificate = true;
291293

@@ -325,7 +327,7 @@ namespace Driver
325327
{
326328

327329
// Replace below argument with connection string from portal.
328-
var connStr = new NpgsqlConnectionStringBuilder("Server = <host>; Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require;");
330+
var connStr = new NpgsqlConnectionStringBuilder("Server = <host> Database = citus; Port = 5432; User Id = citus; Password = {your password}; Ssl Mode = Require; Pooling = true; Minimum Pool Size=0; Maximum Pool Size =50 ");
329331

330332
connStr.TrustServerCertificate = true;
331333

articles/postgresql/hyperscale/howto-app-stacks-java.md

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: saimicrosoft
66
ms.service: postgresql
77
ms.subservice: hyperscale-citus
88
ms.topic: how-to
9-
ms.date: 06/20/2022
9+
ms.date: 07/26/2022
1010
---
1111

1212
# Java app to connect and query Hyperscale (Citus)
@@ -71,6 +71,12 @@ Using your favorite IDE, create a new Java project with groupId **test** and art
7171
<artifactId>postgresql</artifactId>
7272
<version>42.2.12</version>
7373
</dependency>
74+
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
75+
<dependency>
76+
<groupId>com.zaxxer</groupId>
77+
<artifactId>HikariCP</artifactId>
78+
<version>5.0.0</version>
79+
</dependency>
7480
<dependency>
7581
<groupId>org.junit.jupiter</groupId>
7682
<artifactId>junit-jupiter-params</artifactId>
@@ -101,6 +107,7 @@ This file configures [Apache Maven](https://maven.apache.org/) to use:
101107
Create a `src/main/resources/application.properties` file, and add:
102108

103109
``` properties
110+
driver.class.name=org.postgresql.Driver
104111
url=jdbc:postgresql://<host>:5432/citus?ssl=true&sslmode=require
105112
user=citus
106113
password=<password>
@@ -142,6 +149,61 @@ select create_distributed_table('public.pharmacy','pharmacy_id');
142149

143150
Next, add the Java code that will use JDBC to store and retrieve data from your Hyperscale (Citus) server group.
144151

152+
#### Connection Pooling Setup
153+
154+
Using the code below, create a `DButil.java` file, which contains the `DButil`
155+
class. The `DBUtil` class sets up a connection pool to PostgreSQL using
156+
[HikariCP](https://github.com/brettwooldridge/HikariCP). In the example
157+
application, we'll be using this class to connect to PostgreSQL and start
158+
querying.
159+
160+
[!INCLUDE[why-connection-pooling](includes/why-connection-pooling.md)]
161+
162+
```java
163+
//DButil.java
164+
package test.crud;
165+
166+
import java.io.FileInputStream;
167+
import java.io.IOException;
168+
import java.sql.SQLException;
169+
import java.util.Properties;
170+
171+
import javax.sql.DataSource;
172+
173+
import com.zaxxer.hikari.HikariDataSource;
174+
175+
public class DButil {
176+
private static final String DB_USERNAME = "db.username";
177+
private static final String DB_PASSWORD = "db.password";
178+
private static final String DB_URL = "db.url";
179+
private static final String DB_DRIVER_CLASS = "driver.class.name";
180+
private static Properties properties = null;
181+
private static HikariDataSource datasource;
182+
183+
static {
184+
try {
185+
properties = new Properties();
186+
properties.load(new FileInputStream("src/main/java/application.properties"));
187+
188+
datasource = new HikariDataSource();
189+
datasource.setDriverClassName(properties.getProperty(DB_DRIVER_CLASS ));
190+
datasource.setJdbcUrl(properties.getProperty(DB_URL));
191+
datasource.setUsername(properties.getProperty(DB_USERNAME));
192+
datasource.setPassword(properties.getProperty(DB_PASSWORD));
193+
datasource.setMinimumIdle(100);
194+
datasource.setMaximumPoolSize(1000000000);
195+
datasource.setAutoCommit(false);
196+
datasource.setLoginTimeout(3);
197+
} catch (IOException | SQLException e) {
198+
e.printStackTrace();
199+
}
200+
}
201+
public static DataSource getDataSource() {
202+
return datasource;
203+
}
204+
}
205+
```
206+
145207
Create a `src/main/java/DemoApplication.java` file that contains:
146208

147209
``` java
@@ -168,11 +230,9 @@ public class DemoApplication {
168230
}
169231
public static void main(String[] args)throws Exception
170232
{
171-
log.info("Loading application properties");
172-
Properties properties = new Properties();
173-
properties.load(DemoApplication.class.getClassLoader().getResourceAsStream("application.properties"));
174233
log.info("Connecting to the database");
175-
Connection connection = DriverManager.getConnection(properties.getProperty("url"), properties);
234+
Connection connection = DButil.getDataSource().getConnection();
235+
System.out.println("The Connection Object is of Class: " + connection.getClass());
176236
log.info("Database connection test: " + connection.getCatalog());
177237
log.info("Creating table");
178238
log.info("Creating index");
@@ -272,12 +332,12 @@ public class Pharmacy {
272332
}
273333
@Override
274334
public String toString() {
275-
return "TPharmacy{"
276-
"pharmacy_id=" + pharmacy_id
277-
", pharmacy_name='" + pharmacy_name + '\''
278-
", city='" + city + '\''
279-
", state='" + state + '\''
280-
", zip_code='" + zip_code + '\''
335+
return "TPharmacy{" +
336+
"pharmacy_id=" + pharmacy_id +
337+
", pharmacy_name='" + pharmacy_name + '\'' +
338+
", city='" + city + '\'' +
339+
", state='" + state + '\'' +
340+
", zip_code='" + zip_code + '\'' +
281341
'}';
282342
}
283343
}
@@ -472,14 +532,17 @@ The following code is an example for copying data from a CSV file to a database
472532
It requires the file [pharmacies.csv](https://download.microsoft.com/download/d/8/d/d8d5673e-7cbf-4e13-b3e9-047b05fc1d46/pharmacies.csv).
473533

474534
```java
475-
public static long copyFromFile(Connection connection, String filePath, String tableName)
476-
throws SQLException, IOException
477-
{
535+
public static long
536+
copyFromFile(Connection connection, String filePath, String tableName)
537+
throws SQLException, IOException {
478538
long count = 0;
479539
FileInputStream fileInputStream = null;
480540

481541
try {
482-
CopyManager copyManager = new CopyManager((BaseConnection) connection);
542+
Connection unwrap = connection.unwrap(Connection.class);
543+
BaseConnection connSec = (BaseConnection) unwrap;
544+
545+
CopyManager copyManager = new CopyManager((BaseConnection) connSec);
483546
fileInputStream = new FileInputStream(filePath);
484547
count = copyManager.copyIn("COPY " + tableName + " FROM STDIN delimiter ',' csv", fileInputStream);
485548
} finally {
@@ -531,16 +594,20 @@ The following code is an example for copying in-memory data to table.
531594
```java
532595
private static void inMemory(Connection connection) throws SQLException,IOException {
533596
log.info("Copying in-memory data into table");
534-
String[] input = {"5000,Target,Sunnyvale,California,94001"};
597+
String[] input = {"0,Target,Sunnyvale,California,94001"};
598+
599+
Connection unwrap = connection.unwrap(Connection.class);
600+
BaseConnection connSec = (BaseConnection) unwrap;
535601

536-
CopyManager copyManager = new CopyManager((BaseConnection) connection);
537-
String copyCommand = "COPY pharmacy FROM STDIN with csv " ;
602+
CopyManager copyManager = new CopyManager((BaseConnection) connSec);
603+
String copyCommand = "COPY pharmacy FROM STDIN with csv";
538604

539605
for (String var : input)
540606
{
541607
Reader reader = new StringReader(var);
542608
copyManager.copyIn(copyCommand, reader);
543609
}
610+
544611
copyManager.copyIn(copyCommand);
545612
}
546613
```

0 commit comments

Comments
 (0)