55 "database/sql"
66 "database/sql/driver"
77 "fmt"
8+ "sync/atomic"
89
910 "github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
1011 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
@@ -52,17 +53,25 @@ func (d *sqlDriver) OpenConnector(dataSourceName string) (driver.Connector, erro
5253 return nil , xerrors .WithStackTrace (fmt .Errorf ("failed to connect by data source name '%s': %w" , dataSourceName , err ))
5354 }
5455
55- c , err := connector (db , db .databaseSQLOptions ... )
56+ c , err := Connector (db , db .databaseSQLOptions ... )
5657 if err != nil {
5758 return nil , xerrors .WithStackTrace (fmt .Errorf ("failed to create connector: %w" , err ))
5859 }
5960
60- d .attach (c , db )
61-
6261 return c , nil
6362}
6463
64+ var globalConnectorCounter atomic.Uint64
65+
6566func (d * sqlDriver ) attach (c * xsql.Connector , parent * Driver ) {
67+ c .ID = globalConnectorCounter .Add (1 )
68+
69+ if parent != nil {
70+ parent .onClose .Append (func (_ * Driver ) {
71+ _ = c .Close ()
72+ })
73+ }
74+
6675 d .connectors .Set (c , parent )
6776}
6877
@@ -239,7 +248,7 @@ type SQLConnector interface {
239248 Close () error
240249}
241250
242- func connector (parent * Driver , opts ... ConnectorOption ) (* xsql. Connector , error ) {
251+ func Connector (parent * Driver , opts ... ConnectorOption ) (SQLConnector , error ) {
243252 c , err := xsql .Open (parent , parent .metaBalancer ,
244253 append (
245254 append (
@@ -255,16 +264,7 @@ func connector(parent *Driver, opts ...ConnectorOption) (*xsql.Connector, error)
255264 return nil , xerrors .WithStackTrace (err )
256265 }
257266
258- return c , nil
259- }
260-
261- func Connector (parent * Driver , opts ... ConnectorOption ) (SQLConnector , error ) {
262- c , err := connector (parent , opts ... )
263- if err != nil {
264- return nil , xerrors .WithStackTrace (err )
265- }
266-
267- d .attach (c , nil )
267+ d .attach (c , parent )
268268
269269 return c , nil
270270}
0 commit comments