nakagami / firebirdsql

Firebird RDBMS sql driver for Go (golang)
MIT License
227 stars 60 forks source link

Not properly work after transaction #35

Closed heretic13 closed 7 years ago

heretic13 commented 8 years ago

`package main

import ( "database/sql" "log"

_ "github.com/nakagami/firebirdsql"

)

func main() {

conn, err := sql.Open("firebirdsql", "SYSDBA:masterkey@127.0.0.1/d:/Temp/TestDatabases/TEST.FDB")
defer conn.Close()

if err != nil {
    log.Fatal(err)
}

// контроль связи с БД
err = conn.Ping()
if err != nil {
    log.Fatal(err)
}

tx, err := conn.Begin()

if err != nil {
    log.Fatal(err)
}

err = tx.Commit()

if err != nil {
    log.Fatal(err)
}

const query = `CREATE TABLE SensorLog(SN INTEGER NOT NULL);`

_, err = conn.Exec(query)

if err != nil {
    log.Fatal(err)
}

}`

heretic13 commented 8 years ago

Table not created after transaction. If I comment transaction, it's working.

This is normal?

heretic13 commented 8 years ago

maybe problem in this

connection.go:

func (fc *firebirdsqlConn) Begin() (driver.Tx, error) { fc.isAutocommit = false }

func (fc *firebirdsqlConn) Exec(query string, args []driver.Value) (result driver.Result, err error) { if fc.isAutocommit { fc.tx.Commit() fc.tx, err = newFirebirdsqlTx(fc.wp, fc.isolationLevel) } }

heretic13 commented 8 years ago

After transaction rollback or commit nothing changed in connection state (isAutocommit value).

nakagami commented 7 years ago

Thanks, I realize something wrong.

I think that changing isAutcommit state in Begin() is evil. But still I don't know how to fix this problem now. Wait for several days. (or send pull request).

nakagami commented 7 years ago

I have been modify and refactoring, and add test codes. https://github.com/nakagami/firebirdsql/blob/master/transaction_test.go

I think, this problem fixed. Please test it with master HEAD.

heretic13 commented 7 years ago

I checked. This solution does not work. Tested by example, which I quoted above. Begin () makes the transaction with property tx.isAutocommit = false. When I do a tx.Commit() - nothing changes.

If I do then conn.Exec ():

if fc.isAutocommit && fc.tx.isAutocommit { fc.tx.Commit () }

fc.isAutocommit = true fc.tx.isAutocommit = false

fc.tx.Commit () - not performed

heretic13 commented 7 years ago

I think that once executed tx.Commit () or tx.Rollback () the connection must be changed fc.tx.isAutocommit flag or forget the old transaction and create a new transaction for its own needs (as is done in newFirebirdsqlConn ())

nakagami commented 7 years ago

Thanks, I think I wrote regression test and fix code.

Restart transaction in Commit() and Rollback() Is this OK ?

heretic13 commented 7 years ago

Hmm... It's working!

nakagami commented 7 years ago

thanks