You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

connector.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package sql
  18. import (
  19. "context"
  20. "database/sql/driver"
  21. "sync"
  22. "github.com/go-sql-driver/mysql"
  23. "github.com/seata/seata-go/pkg/datasource/sql/types"
  24. )
  25. type seataATConnector struct {
  26. *seataConnector
  27. transType types.TransactionMode
  28. }
  29. func (c *seataATConnector) Connect(ctx context.Context) (driver.Conn, error) {
  30. conn, err := c.seataConnector.Connect(ctx)
  31. if err != nil {
  32. return nil, err
  33. }
  34. _conn, _ := conn.(*Conn)
  35. return &ATConn{
  36. Conn: _conn,
  37. }, nil
  38. }
  39. func (c *seataATConnector) Driver() driver.Driver {
  40. return &seataATDriver{
  41. seataDriver: c.seataConnector.Driver().(*seataDriver),
  42. }
  43. }
  44. type seataXAConnector struct {
  45. *seataConnector
  46. transType types.TransactionMode
  47. }
  48. func (c *seataXAConnector) Connect(ctx context.Context) (driver.Conn, error) {
  49. conn, err := c.seataConnector.Connect(ctx)
  50. if err != nil {
  51. return nil, err
  52. }
  53. _conn, _ := conn.(*Conn)
  54. return &XAConn{
  55. Conn: _conn,
  56. }, nil
  57. }
  58. func (c *seataXAConnector) Driver() driver.Driver {
  59. return &seataXADriver{
  60. seataDriver: c.seataConnector.Driver().(*seataDriver),
  61. }
  62. }
  63. // A Connector represents a driver in a fixed configuration
  64. // and can create any number of equivalent Conns for use
  65. // by multiple goroutines.
  66. //
  67. // A Connector can be passed to sql.OpenDB, to allow drivers
  68. // to implement their own sql.DB constructors, or returned by
  69. // DriverContext's OpenConnector method, to allow drivers
  70. // access to context and to avoid repeated parsing of driver
  71. // configuration.
  72. //
  73. // If a Connector implements io.Closer, the sql package's DB.Close
  74. // method will call Close and return error (if any).
  75. type seataConnector struct {
  76. transType types.TransactionMode
  77. conf *seataServerConfig
  78. res *DBResource
  79. once sync.Once
  80. driver driver.Driver
  81. target driver.Connector
  82. cfg *mysql.Config
  83. }
  84. // Connect returns a connection to the database.
  85. // Connect may return a cached connection (one previously
  86. // closed), but doing so is unnecessary; the sql package
  87. // maintains a pool of idle connections for efficient re-use.
  88. //
  89. // The provided context.Context is for dialing purposes only
  90. // (see net.DialContext) and should not be stored or used for
  91. // other purposes. A default timeout should still be used
  92. // when dialing as a connection pool may call Connect
  93. // asynchronously to any query.
  94. //
  95. // The returned connection is only used by one goroutine at a
  96. // time.
  97. func (c *seataConnector) Connect(ctx context.Context) (driver.Conn, error) {
  98. conn, err := c.target.Connect(ctx)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return &Conn{
  103. targetConn: conn,
  104. res: c.res,
  105. txCtx: types.NewTxCtx(),
  106. autoCommit: true,
  107. dbName: c.cfg.DBName,
  108. dbType: types.DBTypeMySQL,
  109. }, nil
  110. }
  111. // Driver returns the underlying Driver of the Connector,
  112. // mainly to maintain compatibility with the Driver method
  113. // on sql.DB.
  114. func (c *seataConnector) Driver() driver.Driver {
  115. c.once.Do(func() {
  116. d := c.target.Driver()
  117. c.driver = d
  118. })
  119. return &seataDriver{
  120. target: c.driver,
  121. }
  122. }