Skip to content

Commit 18989c2

Browse files
authored
Merge pull request #3894 from SpencerTorres/clickhouse-go-batch-close
Update clickhouse-go examples to use `batch.Close()`
2 parents 1ba6ed7 + f24f834 commit 18989c2

File tree

1 file changed

+34
-1
lines changed
  • docs/integrations/language-clients/go

1 file changed

+34
-1
lines changed

docs/integrations/language-clients/go/index.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,9 @@ Note the ability to pass a Context to the query. This can be used to pass specif
514514
515515
### Batch Insert {#batch-insert}
516516
517-
To insert a large number of rows, the client provides batch semantics. This requires the preparation of a batch to which rows can be appended. This is finally sent via the `Send()` method. Batches will be held in memory until Send is executed.
517+
To insert a large number of rows, the client provides batch semantics. This requires the preparation of a batch to which rows can be appended. This is finally sent via the `Send()` method. Batches are held in memory until `Send` is executed.
518+
519+
It is recommended to call `Close` on the batch to prevent leaking connections. This can be done via the `defer` keyword after preparing the batch. This will clean up the connection if `Send` never gets called. Note that this will result in 0 row inserts showing up in the query log if no rows were appended.
518520
519521
```go
520522
conn, err := GetNativeConnection(nil, nil, nil)
@@ -547,6 +549,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
547549
if err != nil {
548550
return err
549551
}
552+
defer batch.Close()
553+
550554
for i := 0; i < 1000; i++ {
551555
err := batch.Append(
552556
uint8(42),
@@ -568,6 +572,7 @@ for i := 0; i < 1000; i++ {
568572
return err
569573
}
570574
}
575+
571576
return batch.Send()
572577
```
573578
@@ -582,6 +587,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
582587
if err != nil {
583588
return err
584589
}
590+
defer batch.Close()
591+
585592
for i := 0; i < 1000; i++ {
586593
err := batch.Append(
587594
"2006-01-02 15:04:05.999",
@@ -590,6 +597,7 @@ for i := 0; i < 1000; i++ {
590597
return err
591598
}
592599
}
600+
593601
return batch.Send()
594602
```
595603
@@ -697,6 +705,8 @@ batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example")
697705
if err != nil {
698706
return err
699707
}
708+
defer batch.Close()
709+
700710
var (
701711
col1 []uint64
702712
col2 []string
@@ -721,6 +731,7 @@ if err := batch.Column(2).Append(col3); err != nil {
721731
if err := batch.Column(3).Append(col4); err != nil {
722732
return err
723733
}
734+
724735
return batch.Send()
725736
```
726737
@@ -778,6 +789,8 @@ batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO example")
778789
if err != nil {
779790
return err
780791
}
792+
defer batch.Close()
793+
781794
for i := 0; i < 1_000; i++ {
782795
err := batch.AppendStruct(&row{
783796
Col1: uint64(i),
@@ -828,6 +841,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
828841
if err != nil {
829842
return err
830843
}
844+
defer batch.Close()
845+
831846
var i int64
832847
for i = 0; i < 10; i++ {
833848
err := batch.Append(
@@ -869,6 +884,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
869884
if err != nil {
870885
return err
871886
}
887+
defer batch.Close()
888+
872889
var i int64
873890
for i = 0; i < 10; i++ {
874891
err := batch.Append(
@@ -936,6 +953,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
936953
if err != nil {
937954
return err
938955
}
956+
defer batch.Close()
957+
939958
// both named and unnamed can be added with slices. Note we can use strongly typed lists and maps if all elements are the same type
940959
if err = batch.Append([]interface{}{"Clicky McClickHouse", uint8(42)}, []interface{}{"Clicky McClickHouse Snr", uint8(78)}, []string{"Dale", "521211"}); err != nil {
941960
return err
@@ -1002,6 +1021,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
10021021
if err != nil {
10031022
return err
10041023
}
1024+
defer batch.Close()
1025+
10051026
var i int64
10061027
for i = 0; i < 10; i++ {
10071028
err := batch.Append(
@@ -1099,6 +1120,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
10991120
if err != nil {
11001121
return err
11011122
}
1123+
defer batch.Close()
1124+
11021125
var i uint8
11031126
for i = 0; i < 10; i++ {
11041127
col1_1_data := []string{strconv.Itoa(int(i)), strconv.Itoa(int(i + 1)), strconv.Itoa(int(i + 2))}
@@ -1158,6 +1181,7 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
11581181
if err != nil {
11591182
return err
11601183
}
1184+
defer batch.Close()
11611185

11621186
if err = batch.Append(
11631187
orb.Point{11, 22},
@@ -1238,6 +1262,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
12381262
if err != nil {
12391263
return err
12401264
}
1265+
defer batch.Close()
1266+
12411267
col1Data, _ := uuid.NewUUID()
12421268
if err = batch.Append(
12431269
col1Data,
@@ -1283,6 +1309,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
12831309
if err != nil {
12841310
return err
12851311
}
1312+
defer batch.Close()
1313+
12861314
if err = batch.Append(
12871315
decimal.New(25, 4),
12881316
decimal.New(30, 5),
@@ -1336,6 +1364,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
13361364
if err != nil {
13371365
return err
13381366
}
1367+
defer batch.Close()
1368+
13391369
if err = batch.Append(
13401370
nil,
13411371
nil,
@@ -1387,6 +1417,7 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
13871417
if err != nil {
13881418
return err
13891419
}
1420+
defer batch.Close()
13901421

13911422
col1Data, _ := new(big.Int).SetString("170141183460469231731687303715884105727", 10)
13921423
col2Data := big.NewInt(128)
@@ -1467,6 +1498,8 @@ batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
14671498
if err != nil {
14681499
return err
14691500
}
1501+
defer batch.Close()
1502+
14701503
for i := 0; i < 1000; i++ {
14711504
if err := batch.Append([]string{strconv.Itoa(i), strconv.Itoa(i + 1), strconv.Itoa(i + 2), strconv.Itoa(i + 3)}); err != nil {
14721505
return err

0 commit comments

Comments
 (0)