snapshot of https://github.com/ThreeDotsLabs/watermill.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
master / ci (push) Failing after 0s
Some checks failed
master / ci (push) Failing after 0s
This commit is contained in:
44
_examples/pubsubs/sqlite-zombiezen/transaction.go
Normal file
44
_examples/pubsubs/sqlite-zombiezen/transaction.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill-sqlite/wmsqlitezombiezen"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
"zombiezen.com/go/sqlite"
|
||||
"zombiezen.com/go/sqlite/sqlitex"
|
||||
)
|
||||
|
||||
func publishWithInTransaction(connectionDSN string) {
|
||||
var err error
|
||||
// create a new connection for each transaction
|
||||
// unless you guard it with a sync.Mutex
|
||||
conn, err := sqlite.OpenConn(connectionDSN)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
closer := sqlitex.Transaction(conn)
|
||||
defer func() {
|
||||
if closer(&err); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
publisher, err := wmsqlitezombiezen.NewPublisher(
|
||||
conn,
|
||||
wmsqlitezombiezen.PublisherOptions{
|
||||
// schema must be initialized elsewhere before using
|
||||
// the publisher within the transaction
|
||||
InitializeSchema: false,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msg := message.NewMessage(watermill.NewUUID(), []byte(`{"message": "Hello, world!"}`))
|
||||
if err = publisher.Publish("example_topic", msg); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user