0

my queries

var m sync.Mutex
m.Lock()
defer m.Unlock()

_, err1 := database.Exec(`UPDATE  UserData SET coins = coins + ? WHERE rollno = ?`, transfer_data.Coins, transfer_data.ReceiverRollno)    
if err1 != nil {
   //if some error rollback databse to initial condition and print the error 
   fmt.Println("error lies in database.Exec() err1")
   fmt.Println(err1)
   tx.Rollback()
   return
   // panic(err)
}
    
_, err2 := database.Exec(`UPDATE UserData SET coins = coins - ?  WHERE rollno = ? AND coins - ? >= 0`, transfer_data.Coins, claims.Rollno, transfer_data.Coins)
    
if err2 != nil {
   //if some error rollback databse to initial condition and print the error
   fmt.Println(err2)
   fmt.Println("error lies in database.Exec() err2")
   tx.Rollback()
   return
}

//we are here so this means transaction is successful so Commit this change to the database
tx.Commit()

my table

UserCoin_info := `CREATE TABLE IF NOT EXISTS UserData(
   "rollno" INTEGER NOT NULL,
   "coins" INTEGER NOT NULL
);`
    
statement, err := db.Prepare(UserCoin_info)
if err != nil {
   panic(err)
}
statement.Exec()

I am getting database lock error as err1 while making a update in the first query, i tried using mutexes but still getting this error, i searched for possible solutions but didn't get any.

12
  • 2
    It would be helpful if you provided the original error message Commented Jun 29, 2021 at 4:49
  • i am getting this error lies in database.Exec() err1 database is locked Commented Jun 29, 2021 at 4:50
  • You seem to have a tx, but you seem to be not using it. Commented Jun 29, 2021 at 4:52
  • tx, err := database.Begin() if err != nil { fmt.Println("error lies in database.begin()") return } Commented Jun 29, 2021 at 4:56
  • If you run it on windows - it looks some other process has obtained the lock over the db file. Commented Jun 29, 2021 at 4:56

1 Answer 1

0

Database lock is result of not closing *Stmt and *Rows.

statement, err := db.Prepare(UserCoin_info)
if err != nil {
     panic(err)
}
defer statement.Close()

statement.Exec()

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.