Closed sureshmula2 closed 9 months ago
@nineinchnick Anything on this?
I can't reproduce this. What do you pass as the context?
The code is completely fine when I don't set the query_max_execution_time
. There's an issue when setting the query_max_execution_time
as it fails to execute two query statements.
func newTestContextHelper(t testing.TB, tzName string, testTimeout time.Duration) context.Context {
// create a new context with the provided timeout duration.
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
t.Cleanup(cancel)
ctx = NewTimezoneContext(ctx, timezones.TzName(tzName))
ctx = auth.NewContext(ctx, auth.NewClaims("XXX", "XX", "XX", "XX", "XX"))
ctx = request.NewContext(
ctx,
request.NewRequestContext("XXX", "XX", "XX"))
return ctx
}
What's NewTimezoneContext
?
I ran this program, connecting to Trino 437 running in a container:
package main
import (
"context"
"database/sql"
"fmt"
_ "github.com/trinodb/trino-go-client/trino"
)
func main() {
check(context.Background())
}
func check(ctx context.Context) error {
db, err := sql.Open("trino", "http://example@localhost:8080")
if err != nil {
return err
}
timeoutStr := "10m" // session timeout 10 minutes
conn, err := db.Conn(ctx) //getting connection
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, fmt.Sprintf("SET SESSION query_max_execution_time = '%s'", timeoutStr)) // setting session timeout as 10 minutes
if err != nil {
conn.Close()
return err
}
_, err = conn.QueryContext(ctx, "select 1")
if err != nil {
return err
}
_, err = conn.QueryContext(ctx, "select 2")
if err != nil {
return err
}
fmt.Printf("Fin!\n")
return nil
}
It works just fine.
Apologies for pinging again, @nineinchnick.
Adding defer conn.Close()
after creating a connection results in a panic. We're aiming to avoid hanging connections after executing queries. I also attempted to close the connection at the end of the program, but it still fails.
package main
import (
"context"
"database/sql"
"fmt"
_ "github.com/trinodb/trino-go-client/trino"
)
func main() {
check(context.Background())
}
func check(ctx context.Context) error {
db, err := sql.Open("trino", "http://example@localhost:8080")
if err != nil {
return err
}
timeoutStr := "10m" // session timeout 10 minutes
conn, err := db.Conn(ctx) //getting connection
defer conn.Close()
if err != nil {
return err
}
_, err = conn.ExecContext(ctx, fmt.Sprintf("SET SESSION query_max_execution_time = '%s'", timeoutStr)) // setting session timeout as 10 minutes
if err != nil {
conn.Close()
return err
}
_, err = conn.QueryContext(ctx, "select 1")
if err != nil {
return err
}
_, err = conn.QueryContext(ctx, "select 2")
if err != nil {
return err
}
fmt.Printf("Fin!\n")
return nil
}
When I ran it, it didn't panic, but got stuck. It works when I call close results of QueryContext()
. This is expected, closing rows is required, if you don't need them you should use ExecContext()
instead.
rows, err = conn.QueryContext(ctx, "select 2")
if err != nil {
return err
}
err = rows.Close()
if err != nil {
return err
}
Thank you. Leaving the connection open isn't ideal, and attempting to close it seems to cause it to get stuck. What course of action do you suggest in this situation?
Trino connections are stateless, it's basically just a http connection. But if you close all other resources (statements, rows) you should be able to close the connection.
Also the driver's close method is a noop, so it's not the driver's issue. This is how the Go's database/sql interface works.
I found an interesting thing, where I can't run two queries on a connection. I created a connection and set the query_max_execution_time to 10 minutes. Then, I execute only one query, I don't encounter any problem. However, if I execute more than one query, it fails with the following error. I believe this should not happen.
You can use following steps to reproduce the issue.