Created
October 20, 2021 10:05
-
-
Save monirz/dd34417a7328feec71983922cd827a70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE batches( | |
id NUMBER(19) GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY, | |
user_id VARCHAR2(50) NOT NULL, | |
bId VARCHAR2(50) UNIQUE NOT NULL, | |
actionType NUMBER(3) NOT NULL, | |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"database/sql" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"github.com/go-chi/chi" | |
uuid "github.com/satori/go.uuid" | |
"github.com/godror/godror" | |
_ "github.com/godror/godror" | |
) | |
func init() { | |
} | |
var DB *sql.DB | |
func main() { | |
var P godror.ConnectionParams | |
P.Username, P.Password = "oradb", godror.NewPassword("password") | |
P.ConnectString = "localhost" + ":" + "49161" + "/ORCLCDB.localdomain:dedicated?connect_timeout=5" | |
P.SessionTimeout = 15 * time.Second | |
// P.SetSessionParamOnInit("NLS_NUMERIC_CHARACTERS", ",.") | |
P.PoolParams.SessionIncrement = 1 | |
P.PoolParams.MaxSessions = 20000 | |
P.PoolParams.MinSessions = 1000 | |
log.Println(P) | |
DB = sql.OpenDB(godror.NewConnector(P)) | |
if err := DB.Ping(); err != nil { | |
log.Fatal(err) | |
} | |
DB.SetMaxIdleConns(25000) | |
DB.SetMaxOpenConns(19000) | |
r := chi.NewRouter() | |
r.Post("/api/batch", CreateBatchHandler) | |
fmt.Println("Listening on :8099") | |
if err := http.ListenAndServe(":8099", r); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func CreateBatchHandler(w http.ResponseWriter, r *http.Request) { | |
insertIntoBatch(r.Context()) | |
} | |
func insertIntoBatch(ctx context.Context) { | |
query1 := `INSERT INTO batches( | |
bId, | |
user_id, | |
actionType, | |
created_at | |
) | |
VALUES | |
(:1,:2,:3,:4,:5)` | |
createdAt := time.Now().Format("2-Jan-06 3:04:05.000000") | |
bid := uuid.NewV4().String() | |
uid := uuid.NewV4().String() | |
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute) | |
defer cancel() | |
_, err := DB.ExecContext(ctx, query1, bid, uid, 1, createdAt) | |
if err != nil { | |
log.Println("error inserting new record into batch:", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment