Skip to content

Commit

Permalink
Connection Pooling Enhancement to create connection in advance
Browse files Browse the repository at this point in the history
Connection Pooling Enhancement to create connection in advance
  • Loading branch information
bimalkjha authored Oct 14, 2022
2 parents cd4d1b0 + f6595c3 commit 254b1b5
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 60 deletions.
72 changes: 72 additions & 0 deletions API_DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
10. [.Columns()](#ColumnsApi)
11. [.Next()](#NextApi)
12. [.Scan(options)](#ScanApi)
13. [.Init(N,connStr)](#InitApi)
14. [.SetConnMaxLifetime(N)](#SetConnMaxLifetimeApi)

### <a name="OpenApi"></a> 1) .Open(drivername,ConnectionString)

Expand Down Expand Up @@ -350,6 +352,76 @@ func oper() error {
}
```

### <a name="InitApi"></a> 13) .Init(N,connStr)

Initialize Pool with N no of active connections using supplied connection string. It is a synchronous API. We do not need an asynchronous version of this API.
* **N** - No of connections to be initialized.
* **connStr** - The connection string for your database
```go
func oper() error {
fmt.Println("connecting to database")

var ret = pool.init(5, connStr)
if ret != true {
fmt.Println(ret)
}

db := pool.Open(connStr, "SetConnMaxLifetime=10")
for i:=0; i<20; i++ {
db := pool.Open(con, "SetConnMaxLifetime=10")
if db != nil {
st, err := db.Prepare("select * from SAMPLE")
if err != nil {
fmt.Println("Error: ", err)
} else {
go func() {
ExecQuery(st)
db.Close()
}()
}
}
}

time.Sleep(30*time.Second)
pool.Release()
return nil
}
func ExecQuery(st *sql.Stmt) error {
res, err := st5.Query()
if err != nil {
fmt.Println(err)
}
cols, _ := res.Columns()

fmt.Printf("%s %s %s %s\n", cols[0], cols[1], cols[2], cols[3])
defer res.Close()
for res.Next() {
var t, x, m, n string
err = res.Scan(&t, &x, &m, &n)
fmt.Printf("%v %v %v %v\n", t, x, m, n)
}
return nil
}
```

### <a name="SetConnMaxLifetime"></a> 14) .SetConnMaxLifetime(T)

Set the maximum length of time a connection can held open before it is closed.
* **T** - Maximum lenght of time the connection can held open.

```go
func oper() error {
fmt.Println("connecting to database")a

pool.SetConnMaxLifetime(10)
ret := pool.Init(3, con)
if ret != true {
fmt.Println(ret)
}
return nil
}
```

## Create and Drop Database APIs

### <a name="CreateDb"></a> .CreateDb(dbName, connectionString, options...)
Expand Down
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,64 @@ func main() {
```
To run the sample:- go run example3.go

### example4.go:-(POOLING- Limit on the number of connetions)

```go
package main

import (
"database/sql"
"fmt"
"time"
a "github.com/ibmdb/go_ibm_db"
)

func ExecQuery(st *sql.Stmt) error {
res, err := st.Query()
if err != nil {
fmt.Println(err)
}
cols, _ := res.Columns()

fmt.Printf("%s %s %s %s\n", cols[0], cols[1], cols[2], cols[3])
defer res.Close()
for res.Next() {
var t, x, m, n string
err = res.Scan(&t, &x, &m, &n)
fmt.Printf("%v %v %v %v\n", t, x, m, n)
}
return nil
}

func main() {
con := "HOSTNAME=host;PORT=number;DATABASE=name;UID=username;PWD=password"
pool := a.Pconnect("PoolSize=5")

ret := pool.Init(5, con)
if ret != true {
fmt.Println("Pool initializtion failed")
}

for i:=0; i<20; i++ {
db1 := pool.Open(con, "SetConnMaxLifetime=10")
if db1 != nil {
st1, err1 := db1.Prepare("select * from VMSAMPLE")
if err1 != nil {
fmt.Println("err1 : ", err1)
}else{
go func() {
execquery(st1)
db1.Close()
}()
}
}
}
time.Sleep(30*time.Second)
pool.Release()
}
```
To run the sample:- go run example4.go

For Running the Tests:
======================

Expand Down
Loading

0 comments on commit 254b1b5

Please # to comment.