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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/seata/seata-go/pkg/datasource/sql/types"
  23. )
  24. type seataATConnector struct {
  25. *seataConnector
  26. transType types.TransactionType
  27. }
  28. func (c *seataATConnector) Connect(ctx context.Context) (driver.Conn, error) {
  29. conn, err := c.seataConnector.Connect(ctx)
  30. if err != nil {
  31. return nil, err
  32. }
  33. _conn, _ := conn.(*Conn)
  34. return &ATConn{
  35. Conn: _conn,
  36. }, nil
  37. }
  38. func (c *seataATConnector) Driver() driver.Driver {
  39. return &seataATDriver{
  40. seataDriver: c.seataConnector.Driver().(*seataDriver),
  41. }
  42. }
  43. type seataXAConnector struct {
  44. *seataConnector
  45. transType types.TransactionType
  46. }
  47. func (c *seataXAConnector) Connect(ctx context.Context) (driver.Conn, error) {
  48. conn, err := c.seataConnector.Connect(ctx)
  49. if err != nil {
  50. return nil, err
  51. }
  52. _conn, _ := conn.(*Conn)
  53. return &XAConn{
  54. Conn: _conn,
  55. }, nil
  56. }
  57. func (c *seataXAConnector) Driver() driver.Driver {
  58. return &seataXADriver{
  59. seataDriver: c.seataConnector.Driver().(*seataDriver),
  60. }
  61. }
  62. // A Connector represents a driver in a fixed configuration
  63. // and can create any number of equivalent Conns for use
  64. // by multiple goroutines.
  65. //
  66. // A Connector can be passed to sql.OpenDB, to allow drivers
  67. // to implement their own sql.DB constructors, or returned by
  68. // DriverContext's OpenConnector method, to allow drivers
  69. // access to context and to avoid repeated parsing of driver
  70. // configuration.
  71. //
  72. // If a Connector implements io.Closer, the sql package's DB.Close
  73. // method will call Close and return error (if any).
  74. type seataConnector struct {
  75. transType types.TransactionType
  76. conf *seataServerConfig
  77. res *DBResource
  78. once sync.Once
  79. driver driver.Driver
  80. target driver.Connector
  81. }
  82. // Connect returns a connection to the database.
  83. // Connect may return a cached connection (one previously
  84. // closed), but doing so is unnecessary; the sql package
  85. // maintains a pool of idle connections for efficient re-use.
  86. //
  87. // The provided context.Context is for dialing purposes only
  88. // (see net.DialContext) and should not be stored or used for
  89. // other purposes. A default timeout should still be used
  90. // when dialing as a connection pool may call Connect
  91. // asynchronously to any query.
  92. //
  93. // The returned connection is only used by one goroutine at a
  94. // time.
  95. func (c *seataConnector) Connect(ctx context.Context) (driver.Conn, error) {
  96. conn, err := c.target.Connect(ctx)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return &Conn{txType: types.Local, targetConn: conn, res: c.res}, nil
  101. }
  102. // Driver returns the underlying Driver of the Connector,
  103. // mainly to maintain compatibility with the Driver method
  104. // on sql.DB.
  105. func (c *seataConnector) Driver() driver.Driver {
  106. c.once.Do(func() {
  107. d := c.target.Driver()
  108. c.driver = d
  109. })
  110. return &seataDriver{
  111. target: c.driver,
  112. }
  113. }