本文整理汇总了Golang中upper/io/db.Database.Transaction方法的典型用法代码示例。如果您正苦于以下问题:Golang Database.Transaction方法的具体用法?Golang Database.Transaction怎么用?Golang Database.Transaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类upper/io/db.Database
的用法示例。
在下文中一共展示了Database.Transaction方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkAppendTxUpper
// Benchmarking Append() with transactions.
func BenchmarkAppendTxUpper(b *testing.B) {
var sess db.Database
var err error
if sess, err = db.Open(Adapter, settings); err != nil {
b.Fatal(err)
}
defer sess.Close()
var tx db.Tx
if tx, err = sess.Transaction(); err != nil {
b.Fatal(err)
}
var artist db.Collection
if artist, err = tx.Collection("artist"); err != nil {
b.Fatal(err)
}
if err = artist.Truncate(); err != nil {
b.Fatal(err)
}
item := struct {
Name string `db:"name"`
}{"Hayao Miyazaki"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err = artist.Append(item); err != nil {
b.Fatal(err)
}
}
if err = tx.Commit(); err != nil {
b.Fatal(err)
}
}
示例2: TestTransactionsAndRollback
// Attempts to test database transactions.
func TestTransactionsAndRollback(t *testing.T) {
var sess db.Database
var err error
type artistType struct {
ID int64 `db:"id,omitempty"`
Name string `db:"name"`
}
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
defer sess.Close()
// Simple transaction that should not fail.
var tx db.Tx
if tx, err = sess.Transaction(); err != nil {
t.Fatal(err)
}
var artist db.Collection
if artist, err = tx.Collection("artist"); err != nil {
t.Fatal(err)
}
if err = artist.Truncate(); err != nil {
t.Fatal(err)
}
// Simple transaction
if _, err = artist.Append(artistType{1, "First"}); err != nil {
t.Fatal(err)
}
if err = tx.Commit(); err != nil {
t.Fatal(err)
}
// Attempt to use the same transaction should fail.
if _, err = tx.Collection("artist"); err == nil {
t.Fatalf("Illegal, transaction has already been commited.")
}
// Use another transaction.
if tx, err = sess.Transaction(); err != nil {
t.Fatal(err)
}
if artist, err = tx.Collection("artist"); err != nil {
t.Fatal(err)
}
// Won't fail.
if _, err = artist.Append(artistType{2, "Second"}); err != nil {
t.Fatal(err)
}
// Won't fail.
if _, err = artist.Append(artistType{3, "Third"}); err != nil {
t.Fatal(err)
}
// Will fail.
if _, err = artist.Append(artistType{1, "Duplicated"}); err == nil {
t.Fatal("Should have failed, as we have already inserted ID 1.")
}
if err = tx.Rollback(); err != nil {
t.Fatal(err)
}
if err = tx.Commit(); err == nil {
t.Fatalf("Should have failed, as we've already rolled back.")
}
// Let's verify we still have one element.
if artist, err = sess.Collection("artist"); err != nil {
t.Fatal(err)
}
var count uint64
if count, err = artist.Find().Count(); err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("Expecting only one element.")
}
// Attempt to add some rows.
if tx, err = sess.Transaction(); err != nil {
t.Fatal(err)
}
if artist, err = tx.Collection("artist"); err != nil {
t.Fatal(err)
}
//.........这里部分代码省略.........