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 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. )
  23. type seataConnector struct {
  24. conf *seataServerConfig
  25. res *DBResource
  26. once sync.Once
  27. driver driver.Driver
  28. target driver.Connector
  29. }
  30. // Connect returns a connection to the database.
  31. // Connect may return a cached connection (one previously
  32. // closed), but doing so is unnecessary; the sql package
  33. // maintains a pool of idle connections for efficient re-use.
  34. //
  35. // The provided context.Context is for dialing purposes only
  36. // (see net.DialContext) and should not be stored or used for
  37. // other purposes. A default timeout should still be used
  38. // when dialing as a connection pool may call Connect
  39. // asynchronously to any query.
  40. //
  41. // The returned connection is only used by one goroutine at a
  42. // time.
  43. func (c *seataConnector) Connect(ctx context.Context) (driver.Conn, error) {
  44. conn, err := c.target.Connect(ctx)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &Conn{targetConn: conn, res: c.res}, nil
  49. }
  50. // Driver returns the underlying Driver of the Connector,
  51. // mainly to maintain compatibility with the Driver method
  52. // on sql.DB.
  53. func (c *seataConnector) Driver() driver.Driver {
  54. c.once.Do(func() {
  55. d := c.target.Driver()
  56. c.driver = d
  57. })
  58. return &SeataDriver{target: c.driver}
  59. }