@@ -350,3 +350,39 @@ ret = columntable(res)
350
350
351
351
# load on closed connection should throw
352
352
@test_throws ArgumentError MySQL. load (ct, conn, " test194" )
353
+
354
+ @testset " transactions" begin
355
+ DBInterface. execute (conn, " DROP TABLE IF EXISTS TransactionTest" )
356
+ DBInterface. execute (conn, " CREATE TABLE TransactionTest (a int)" )
357
+
358
+ conn2 = DBInterface. connect (MySQL. Connection, " " , " " ; option_file= joinpath (dirname (pathof (MySQL)), " ../test/" , " my.ini" ))
359
+
360
+ try
361
+ # happy path
362
+ DBInterface. transaction (conn) do
363
+ DBInterface. execute (conn, " INSERT INTO TransactionTest (a) VALUES (1)" )
364
+
365
+ # we can see the result inside our transaction
366
+ result = DBInterface. execute (conn, " SELECT * FROM TransactionTest" ) |> Tables. columntable
367
+ @test result. a == [1 ]
368
+
369
+ # and can't see it outside our transaction
370
+ result = DBInterface. execute (conn2, " SELECT * FROM TransactionTest" ) |> Tables. columntable
371
+ @test isempty (result. a)
372
+ end
373
+ result = DBInterface. execute (conn, " SELECT * FROM TransactionTest" ) |> Tables. columntable
374
+ @test result. a == [1 ]
375
+ result = DBInterface. execute (conn2, " SELECT * FROM TransactionTest" ) |> Tables. columntable
376
+ @test result. a == [1 ]
377
+
378
+ # roll back due to exception
379
+ @test_throws ErrorException DBInterface. transaction (conn) do
380
+ DBInterface. execute (conn, " INSERT INTO TransactionTest (a) VALUES (2)" )
381
+ error (" force rollback" )
382
+ end
383
+ result = DBInterface. execute (conn, " SELECT * FROM TransactionTest" ) |> Tables. columntable
384
+ @test result. a == [1 ] # the table did not change
385
+ finally
386
+ close (conn2)
387
+ end
388
+ end
0 commit comments