github.com/XiaoMi/Gaea@v1.2.5/backend/pooled_connection.go (about) 1 // Copyright 2019 The Gaea Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package backend 16 17 import ( 18 "time" 19 20 "github.com/XiaoMi/Gaea/mysql" 21 ) 22 23 // PooledConnect app use this object to exec sql 24 type pooledConnectImpl struct { 25 directConnection *DirectConnection 26 pool *connectionPoolImpl 27 returnTime time.Time 28 } 29 30 // Recycle return PooledConnect to the pool 31 func (pc *pooledConnectImpl) Recycle() { 32 //if has error,the connection can’t be recycled 33 if pc.directConnection.pkgErr != nil { 34 pc.Close() 35 } 36 37 if pc.IsClosed() { 38 pc.pool.Put(nil) 39 } else { 40 pc.pool.Put(pc) 41 pc.returnTime = time.Now() 42 } 43 } 44 45 // Reconnect replaces the existing underlying connection with a new one. 46 // If we get "MySQL server has gone away (errno 2006)", then call Reconnect 47 func (pc *pooledConnectImpl) Reconnect() error { 48 pc.directConnection.Close() 49 newConn, err := NewDirectConnection(pc.pool.addr, pc.pool.user, pc.pool.password, pc.pool.db, pc.pool.charset, pc.pool.collationID) 50 if err != nil { 51 return err 52 } 53 pc.directConnection = newConn 54 return nil 55 } 56 57 // Close implement util.Resource interface 58 func (pc *pooledConnectImpl) Close() { 59 pc.directConnection.Close() 60 } 61 62 // IsClosed check if pooled connection closed 63 func (pc *pooledConnectImpl) IsClosed() bool { 64 if pc.directConnection == nil { 65 return true 66 } 67 return pc.directConnection.IsClosed() 68 } 69 70 // UseDB wrapper of direct connection, init database 71 func (pc *pooledConnectImpl) UseDB(db string) error { 72 return pc.directConnection.UseDB(db) 73 } 74 75 func (pc *pooledConnectImpl) Ping() error { 76 return pc.directConnection.Ping() 77 } 78 // Execute wrapper of direct connection, execute sql 79 func (pc *pooledConnectImpl) Execute(sql string, maxRows int) (*mysql.Result, error) { 80 return pc.directConnection.Execute(sql, maxRows) 81 } 82 83 // SetAutoCommit wrapper of direct connection, set autocommit 84 func (pc *pooledConnectImpl) SetAutoCommit(v uint8) error { 85 return pc.directConnection.SetAutoCommit(v) 86 } 87 88 // Begin wrapper of direct connection, begin transaction 89 func (pc *pooledConnectImpl) Begin() error { 90 return pc.directConnection.Begin() 91 } 92 93 // Commit wrapper of direct connection, commit transaction 94 func (pc *pooledConnectImpl) Commit() error { 95 return pc.directConnection.Commit() 96 } 97 98 // Rollback wrapper of direct connection, rollback transaction 99 func (pc *pooledConnectImpl) Rollback() error { 100 return pc.directConnection.Rollback() 101 } 102 103 // SetCharset wrapper of direct connection, set charset of connection 104 func (pc *pooledConnectImpl) SetCharset(charset string, collation mysql.CollationID) (bool, error) { 105 return pc.directConnection.SetCharset(charset, collation) 106 } 107 108 // FieldList wrapper of direct connection, send field list to mysql 109 func (pc *pooledConnectImpl) FieldList(table string, wildcard string) ([]*mysql.Field, error) { 110 return pc.directConnection.FieldList(table, wildcard) 111 } 112 113 // GetAddr wrapper of return addr of direct connection 114 func (pc *pooledConnectImpl) GetAddr() string { 115 return pc.directConnection.GetAddr() 116 } 117 118 // SetSessionVariables set pc variables according to session 119 func (pc *pooledConnectImpl) SetSessionVariables(frontend *mysql.SessionVariables) (bool, error) { 120 return pc.directConnection.SetSessionVariables(frontend) 121 } 122 123 // WriteSetStatement exec sql 124 func (pc *pooledConnectImpl) WriteSetStatement() error { 125 return pc.directConnection.WriteSetStatement() 126 } 127 128 func (pc *pooledConnectImpl) GetConnectionID() int64 { 129 return int64(pc.directConnection.conn.ConnectionID) 130 } 131 132 func (pc *pooledConnectImpl) GetReturnTime() time.Time { 133 return pc.returnTime 134 }