Skip to content

Commit

Permalink
updates readme
Browse files Browse the repository at this point in the history
  • Loading branch information
gosom committed Mar 25, 2023
1 parent c902eda commit 91d6fcc
Showing 1 changed file with 44 additions and 47 deletions.
91 changes: 44 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,57 @@ import (
)

func main() {
// Connect to the database
db1, err := sql.Open("pgx", "postgres://user:password@host1/mydb1?sslmode=disable")
if err != nil {
panic(err)
}
defer db1.Close()
db2, err := sql.Open("pgx", "postgres://user:password@host2/mydb2?sslmode=disable")
if err != nil {
panic(err)
}
defer db2.Close()

// Create some tables for testing
// One table for users in db1
_, err = db1.Exec("CREATE TABLE IF NOT EXISTS users (id uuid not null primary key, name text)")
if err != nil {
return nil, nil, err
}
// One table for orders in db2
_, err = db2.Exec(`CREATE TABLE IF NOT EXISTS orders(
id uuid not null primary key,
user_id uuid not null,
amount int not null)`)
if err != nil {
return nil, nil, err
}
// Connect to the database
db1, err := sql.Open("pgx", "postgres://user:password@host1/mydb1?sslmode=disable")
if err != nil {
panic(err)
}
defer db1.Close()
db2, err := sql.Open("pgx", "postgres://user:password@host2/mydb2?sslmode=disable")
if err != nil {
panic(err)
}
defer db2.Close()

// Create some tables for testing
// One table for users in db1
_, err = db1.Exec("CREATE TABLE IF NOT EXISTS users (id uuid not null primary key, name text)")
if err != nil {
return nil, nil, err
}
// One table for orders in db2
_, err = db2.Exec(`CREATE TABLE IF NOT EXISTS orders(
id uuid not null primary key,
user_id uuid not null,
amount int not null)`)
if err != nil {
return nil, nil, err
}

userID := "47f89b20-cb3a-11ed-8475-aba23b81b15d"
name := "John Doe"
amount := 10

// Create the participants for the 2 phase commit
p1 := gosql2pc.NewParticipant(db1, func(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, "INSERT INTO users (id, name) VALUES ($1, $2)", userID, name)
return err
})

p2 := gosql2pc.NewParticipant(orderdb, func(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, "INSERT INTO orders (id, user_id, amount) VALUES ($1, $2, $3)", orderID, userID, amount)
return err
})

// setup the parameters for the transaction
params := gosql2pc.Params{
Participants: []gosql2pc.Participant{p1, p2},
}

// run the transaction
if err := gosql2pc.Do(context.Background(), params); err != nil {
p1 := gosql2pc.NewParticipant(db1, func(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, "INSERT INTO users (id, name) VALUES ($1, $2)", userID, name)
return err
})

p2 := gosql2pc.NewParticipant(orderdb, func(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, "INSERT INTO orders (id, user_id, amount) VALUES ($1, $2, $3)", orderID, userID, amount)
return err
})

// setup the parameters for the transaction
params := gosql2pc.Params{
Participants: []gosql2pc.Participant{p1, p2},
}

// run the transaction
if err := gosql2pc.Do(context.Background(), params); err != nil {
panic(err)
}
}
}
```

Expand All @@ -94,6 +94,3 @@ Contributions to go-sql-2pc are always welcome. If you find a bug or want to sug
go-sql-2pc is licensed under the MIT License. See the LICENSE file for more information.





0 comments on commit 91d6fcc

Please sign in to comment.