mysql2 icon indicating copy to clipboard operation
mysql2 copied to clipboard

Transactions examples needed

Open angerangel opened this issue 5 months ago • 3 comments

Please add some examples with transactions. I get command out of sync error

the only way for me is to avoid transactions and prepare the statement list, one per line in a variable and then:

mySQLstatements.each_line  do |line|
  client.query(line)
end

but I risk that someone else execute a query between statements.

angerangel avatar Jul 19 '25 09:07 angerangel

You forgot to write versions and that error is so short? When did you get it? Details are important, so write them.

Faq avatar Jul 21 '25 05:07 Faq

Code is like this:

tMaxCrmid = 0
tSQL = "start transaction; \n"
results_codici.each do |row|
  tMaxCrmid = tMaxCrmid + 1
  cprod = row["codice"]
  tSQL << "INSERT into table1 (cola,colb,colc,cold,cole) VALUES ( "
  tSQL <<  "#{tMaxCrmid},'Products',now(),now(),'#{cprod}');\n"
  tSQL <<  "INSERT into table2 (cola,colb,colc,cold) VALUES ("
  tSQL << "#{tMaxCrmid}, '#{cprod}', 0,now() ) ;\n"
end
tSQL << "COMMIT;"
client.query(tSQL)

It generates more than 100 lines in tSQL variable.

angerangel avatar Jul 30 '25 16:07 angerangel

There's a garbage collection problem. I started investigating a solution a few years ago in https://github.com/brianmario/mysql2/pull/1048 but never got that PR finished.

sodabrew avatar Jul 30 '25 20:07 sodabrew