Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction rollback does not close COPY IN prepared statement and hangs as result #892

Open
yurkovoznyak opened this issue Aug 29, 2019 · 0 comments

Comments

@yurkovoznyak
Copy link

yurkovoznyak commented Aug 29, 2019

In the transaction, I use a prepared statement with COPY IN query.
I have some function which runs ~5-10 sec and may return an error. If I receive error I need to rollback transaction.
According to database/sql doc prepared statement should be closed on transaction Commit or Rollback

Test which describes the behavior below:

func TestCopyInPrepareRollback(t *testing.T) {
	db := openTestConn(t)
	defer db.Close()

	txn, err := db.Begin()
	if err != nil {
		txn.Rollback()
		t.Fatal(err)
	}

	_, err = txn.Exec("CREATE TEMP TABLE temp (num INTEGER)")
	if err != nil {
		txn.Rollback()
		t.Fatal(err)
	}

	stmt, err := txn.Prepare(CopyIn("temp", "num"))
	if err != nil {
		txn.Rollback()
		t.Fatal(err)
	}
	defer stmt.Close()

	someLongRunningErrorFunc := func() error {
		time.Sleep(time.Second * 5)  // without sleep works OK
		return fmt.Errorf("Some external error")
	}

	if err = someLongRunningErrorFunc(); err != nil {
                // stmt.Close()  // this fixes hanged Rollback
		txn.Rollback()  // on this statement test hangs
	}
}

Go version:
go version go1.12.5 linux/amd64

lib/pq version v1.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant