volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

are txns thread safe in sqlboiler? #1105

Closed tilakraj9560 closed 2 years ago

tilakraj9560 commented 2 years ago

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v4.4.0

What is your database and version (eg. Postgresql 10)

13.6

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

models.TabelName(where(name = ?)).All(ctx, tx)

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

Further information. What did you do, what did you expect?

i created one tx object and passed to the different go routines which call the same method, to look for some rows.

tx, err := c.dbClient.StartTx(ctx)
if err != nil {
         return err
}

l := len(4)
var wg sync.WaitGroup
errChan := make(chan error, 4)
wg.Add(4)

// call for each value; arr []
for i := range arr {
   go func(i int, wg *sync.WaitGroup, tx) {
        calls -> xyz(tx)
    }
 }

xyz(tx)    {
configMaps, err := models.Tablename(qm.WhereIn("id in ?", uuids...)).All(ctx, tx)
if err != nil {
         return nil, utils.PrintError(c.logger, codes.Internal, utils.InternalServerError, err)
      }
}

Running it like that throws error -> failed to assign queries. are tx not thread safe in sql boiler?

This whole flow works perfectly, if they happen in sequence instead of go routines. Are txns thread safe with sql boiler?

stephenafamo commented 2 years ago
  1. A transaction by itself is as thread safe as your DB client. SQLBoiler does nothing to the DB connection. If your driver says it is thread safe, then it is thread safe.
  2. I am unsure why you are using a transaction in this specific snippet. You are only doing data retrieval, no updates or inserts. Is there a need for a transaction here?
tilakraj9560 commented 2 years ago

Hi @stephenafamo i got a mail today. prob i missed the reply. I know, i have exposed my internal methods with tx object. since complete flow happens in one txn which involves multiple changes to tables. If we don't use same tx object before committing, i wont be able to read that data.

tilakraj9560 commented 2 years ago

underlying driver is psql driver. i believe i need to create seperate txn object for each go routine.

stephenafamo commented 2 years ago

Alright makes sense. As far as the psql driver is thread-safe, sqlboiler operations will be too.