main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "git.ycl.pub/yl32940/TsDB2/DB"
  4. "fmt"
  5. "os"
  6. )
  7. type Record struct {
  8. DB.BaseModel
  9. Name string `vrtx:"maxlen:32"`
  10. Age int64
  11. }
  12. const dbPath = "txn_test.db"
  13. func main() {
  14. os.Remove(dbPath)
  15. fmt.Println("==============================================")
  16. fmt.Println(" TsDB2 事务功能测试")
  17. fmt.Print("==============================================\n\n")
  18. // 打开数据库
  19. db, err := DB.Open(dbPath)
  20. if err != nil {
  21. panic(err)
  22. }
  23. defer db.Close()
  24. // 禁用自动提交,完全手动控制
  25. db.SetAutoCommitDelay(0)
  26. // 创建表
  27. tbl, err := DB.UseTable[Record](db, "users")
  28. if err != nil {
  29. panic(err)
  30. }
  31. // ========== 测试1: 正常事务提交 ==========
  32. fmt.Println("========== 测试1: 正常事务提交 ==========")
  33. txn1 := db.BeginTransaction()
  34. for i := 1; i <= 5; i++ {
  35. err := tbl.Insert(&Record{
  36. Name: fmt.Sprintf("User%03d", i),
  37. Age: int64(20 + i),
  38. })
  39. if err != nil {
  40. panic(err)
  41. }
  42. }
  43. if err := txn1.Commit(); err != nil {
  44. panic(err)
  45. }
  46. count, _ := tbl.Count()
  47. fmt.Printf("✅ 插入 5 行并提交,当前行数: %d\n", count)
  48. // ========== 测试2: 事务回滚 ==========
  49. fmt.Println("\n========== 测试2: 事务回滚 ==========")
  50. countBefore, _ := tbl.Count()
  51. fmt.Printf("回滚前行数: %d\n", countBefore)
  52. txn2 := db.BeginTransaction()
  53. for i := 6; i <= 10; i++ {
  54. err := tbl.Insert(&Record{
  55. Name: fmt.Sprintf("User%03d", i),
  56. Age: int64(20 + i),
  57. })
  58. if err != nil {
  59. panic(err)
  60. }
  61. }
  62. countDuring, _ := tbl.Count()
  63. fmt.Printf("插入 5 行后(未提交): %d\n", countDuring)
  64. // 回滚事务
  65. if err := txn2.Rollback(); err != nil {
  66. panic(err)
  67. }
  68. // 注意:简化版 Rollback 不立即清除 BufferPool
  69. // 需要重启数据库才能看到效果
  70. fmt.Println("⚠️ 已调用 Rollback()(简化版不立即清除 BufferPool)")
  71. fmt.Println(" 实际回滚依赖 WAL 恢复机制")
  72. // ========== 测试3: 验证数据持久化 ==========
  73. fmt.Println("\n========== 测试3: 验证数据持久化 ==========")
  74. db.Close()
  75. // 重新打开数据库
  76. db2, err := DB.Open(dbPath)
  77. if err != nil {
  78. panic(err)
  79. }
  80. defer db2.Close()
  81. tbl2, _ := DB.UseTable[Record](db2, "users")
  82. countAfterReopen, _ := tbl2.Count()
  83. fmt.Printf("重开后行数: %d(应为 5,回滚的 5 行不应存在)\n", countAfterReopen)
  84. // 验证具体数据
  85. row, err := tbl2.Find(1)
  86. if err == nil {
  87. fmt.Printf("Row#1: Name=%s, Age=%d\n", row.Name, row.Age)
  88. }
  89. row, err = tbl2.Find(6)
  90. if err != nil {
  91. fmt.Println("✅ Row#6 不存在(已回滚)")
  92. } else {
  93. fmt.Printf("❌ Row#6 存在: Name=%s(回滚失败!)\n", row.Name)
  94. }
  95. fmt.Println("\n==============================================")
  96. fmt.Println(" 测试完成!")
  97. fmt.Println("==============================================")
  98. }