Skip to content

Commit 9685c33

Browse files
committed
fix: Connections leaks in database connections
1 parent afb8650 commit 9685c33

File tree

7 files changed

+10
-0
lines changed

7 files changed

+10
-0
lines changed

pkg/datasource/sql/conn_at_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func TestATConn_ExecContext(t *testing.T) {
116116

117117
conn, err := db.Conn(context.Background())
118118
assert.NoError(t, err)
119+
defer conn.Close()
119120

120121
_, err = conn.ExecContext(ctx, "SELECT 1")
121122
assert.NoError(t, err)
@@ -139,6 +140,7 @@ func TestATConn_ExecContext(t *testing.T) {
139140

140141
conn, err := db.Conn(context.Background())
141142
assert.NoError(t, err)
143+
defer conn.Close()
142144

143145
_, err = conn.ExecContext(context.Background(), "SELECT 1")
144146
assert.NoError(t, err)

pkg/datasource/sql/conn_xa_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ func TestXAConn_ExecContext(t *testing.T) {
199199

200200
conn, err := db.Conn(context.Background())
201201
assert.NoError(t, err)
202+
defer conn.Close()
202203

203204
_, err = conn.ExecContext(ctx, "SELECT 1")
204205
assert.NoError(t, err)
@@ -225,6 +226,7 @@ func TestXAConn_ExecContext(t *testing.T) {
225226

226227
conn, err := db.Conn(context.Background())
227228
assert.NoError(t, err)
229+
defer conn.Close()
228230

229231
_, err = conn.ExecContext(context.Background(), "SELECT 1")
230232
assert.NoError(t, err)

pkg/datasource/sql/datasource/mysql/meta_cache.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (c *TableMetaCache) GetTableMeta(ctx context.Context, dbName, tableName str
6565
return nil, err
6666
}
6767

68+
defer conn.Close()
6869
tableMeta, err := c.tableMetaCache.GetTableMeta(ctx, dbName, tableName, conn)
6970
if err != nil {
7071
return nil, err

pkg/datasource/sql/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ func (db *DBResource) ConnectionForXA(ctx context.Context, xaXid XAXid) (*XAConn
223223
if err != nil {
224224
return nil, fmt.Errorf("create xa resoruce err:%w", err)
225225
}
226+
defer newDriverConn.Close()
226227
xaConn := &XAConn{
227228
Conn: &Conn{
228229
targetConn: newDriverConn,

pkg/datasource/sql/driver_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func Test_seataATDriver_Open(t *testing.T) {
9191
conn, err := db.Conn(context.Background())
9292
assert.NotNil(t, err)
9393
assert.Nil(t, conn)
94+
conn.Close()
9495
}
9596

9697
func Test_seataATDriver_OpenConnector(t *testing.T) {

pkg/datasource/sql/undo/base/undo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ func (m *BaseUndoLogManager) Undo(ctx context.Context, dbType types.DBType, xid
253253
if err != nil {
254254
return err
255255
}
256+
defer conn.Close()
256257

257258
tx, err := conn.BeginTx(ctx, &sql.TxOptions{})
258259
if err != nil {

pkg/datasource/sql/undo_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestBatchDeleteUndoLogs(t *testing.T) {
3939

4040
sqlConn, err := db.Conn(context.Background())
4141
assert.Nil(t, err)
42+
defer sqlConn.Close()
4243

4344
undoLogManager := new(base.BaseUndoLogManager)
4445

@@ -86,6 +87,7 @@ func TestHasUndoLogTable(t *testing.T) {
8687
ctx := context.Background()
8788
sqlConn, err := db.Conn(ctx)
8889
assert.Nil(t, err)
90+
defer sqlConn.Close()
8991

9092
undoLogManager := new(base.BaseUndoLogManager)
9193

0 commit comments

Comments
 (0)