Skip to content

Commit 2b2ce0c

Browse files
committed
*: fix the issue of executing DDL after executing SQL failure in txn (pingcap#8044)
* ddl, executor: fix the issue of executing DDL after executing SQL failure in txn
1 parent 500c9bb commit 2b2ce0c

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

ddl/ddl.go

-5
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,6 @@ func (d *ddl) asyncNotifyWorker(jobTp model.ActionType) {
476476
}
477477

478478
func (d *ddl) doDDLJob(ctx sessionctx.Context, job *model.Job) error {
479-
// For every DDL, we must commit current transaction.
480-
if err := ctx.NewTxn(); err != nil {
481-
return errors.Trace(err)
482-
}
483-
484479
// Get a global job ID and put the DDL job in the queue.
485480
err := d.addDDLJob(ctx, job)
486481
if err != nil {

ddl/foreign_key_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ func (s *testForeighKeySuite) testCreateForeignKey(c *C, tblInfo *model.TableInf
7878
BinlogInfo: &model.HistoryInfo{},
7979
Args: []interface{}{fkInfo},
8080
}
81-
err := s.d.doDDLJob(s.ctx, job)
81+
err := s.ctx.NewTxn()
82+
c.Assert(err, IsNil)
83+
err = s.d.doDDLJob(s.ctx, job)
8284
c.Assert(err, IsNil)
8385
return job
8486
}

executor/ddl.go

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func (e *DDLExec) Next(ctx context.Context, chk *chunk.Chunk) (err error) {
6767
return nil
6868
}
6969
e.done = true
70+
71+
// For each DDL, we should commit the previous transaction and create a new transaction.
72+
if err = e.ctx.NewTxn(); err != nil {
73+
return errors.Trace(err)
74+
}
7075
defer func() { e.ctx.GetSessionVars().StmtCtx.IsDDLJobInQueue = false }()
7176

7277
switch x := e.stmt.(type) {

executor/ddl_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ func (s *testSuite) TestTruncateTable(c *C) {
4646
result.Check(nil)
4747
}
4848

49+
// TestInTxnExecDDLFail tests the following case:
50+
// 1. Execute the SQL of "begin";
51+
// 2. A SQL that will fail to execute;
52+
// 3. Execute DDL.
53+
func (s *testSuite) TestInTxnExecDDLFail(c *C) {
54+
tk := testkit.NewTestKit(c, s.store)
55+
tk.MustExec("use test")
56+
tk.MustExec("create table t (i int key);")
57+
tk.MustExec("insert into t values (1);")
58+
tk.MustExec("begin;")
59+
tk.MustExec("insert into t values (1);")
60+
_, err := tk.Exec("truncate table t;")
61+
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1' for key 'PRIMARY'")
62+
result := tk.MustQuery("select count(*) from t")
63+
result.Check(testkit.Rows("1"))
64+
}
65+
4966
func (s *testSuite) TestCreateTable(c *C) {
5067
tk := testkit.NewTestKit(c, s.store)
5168
tk.MustExec("use test")

0 commit comments

Comments
 (0)