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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "seata.apache.org/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. res *DBResource
  78. once sync.Once
  79. driver driver.Driver
  80. target driver.Connector
  81. cfg *mysql.Config
  82. }
  83. // Connect returns a connection to the database.
  84. // Connect may return a cached connection (one previously
  85. // closed), but doing so is unnecessary; the sql package
  86. // maintains a pool of idle connections for efficient re-use.
  87. //
  88. // The provided context.Context is for dialing purposes only
  89. // (see net.DialContext) and should not be stored or used for
  90. // other purposes. A default timeout should still be used
  91. // when dialing as a connection pool may call Connect
  92. // asynchronously to any query.
  93. //
  94. // The returned connection is only used by one goroutine at a
  95. // time.
  96. func (c *seataConnector) Connect(ctx context.Context) (driver.Conn, error) {
  97. conn, err := c.target.Connect(ctx)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return &Conn{
  102. targetConn: conn,
  103. res: c.res,
  104. txCtx: types.NewTxCtx(),
  105. autoCommit: true,
  106. dbName: c.cfg.DBName,
  107. dbType: types.DBTypeMySQL,
  108. }, nil
  109. }
  110. // Driver returns the underlying Driver of the Connector,
  111. // mainly to maintain compatibility with the Driver method
  112. // on sql.DB.
  113. func (c *seataConnector) Driver() driver.Driver {
  114. c.once.Do(func() {
  115. d := c.target.Driver()
  116. c.driver = d
  117. })
  118. return &seataDriver{
  119. target: c.driver,
  120. }
  121. }