@@ -51,7 +51,7 @@ module.exports = (() => {
5151 */
5252 async query ( query , parameters , name ) {
5353 if ( this . getIsDisposed ( ) ) {
54- return Promise . reject ( `Unable to execute MySQL query, the ${ this . toString ( ) } has been disposed` ) ;
54+ throw new Error ( `Unable to execute query, the ${ this . toString ( ) } has been disposed` ) ;
5555 }
5656
5757 assert . argumentIsRequired ( query , 'query' , String ) ;
@@ -78,6 +78,68 @@ module.exports = (() => {
7878 } ) ;
7979 }
8080
81+ /**
82+ * Finalizes instance operations and disposes instance. If the graceful parameter is true, any outstanding
83+ * queries will be completed.
84+ *
85+ * @public
86+ * @async
87+ * @param {Boolean } graceful
88+ * @returns {Promise<void> }
89+ */
90+ async shutdown ( graceful ) {
91+ if ( this . getIsDisposed ( ) ) {
92+ throw new Error ( `Unable to shutdown, the [ ${ this . toString ( ) } ] has been disposed` ) ;
93+ }
94+
95+ if ( this . _connection === null ) {
96+ throw new Error ( `Unable to shutdown, the [ ${ this . toString ( ) } ] has been shutdown` ) ;
97+ }
98+
99+ assert . argumentIsRequired ( graceful , 'graceful' , Boolean ) ;
100+
101+ const connection = this . _connection ;
102+ this . _connection = null ;
103+
104+ this . dispose ( ) ;
105+
106+ let shutdownPromise ;
107+
108+ if ( graceful ) {
109+ shutdownPromise = new Promise ( ( resolve , reject ) => {
110+ connection . end ( ( error ) => {
111+ if ( error ) {
112+ reject ( error ) ;
113+ }
114+
115+ logger . info ( `Shutdown [ ${ this . toString ( ) } ] [ ${ this . id } ] gracefully` ) ;
116+
117+ resolve ( ) ;
118+ } ) ;
119+ } ) ;
120+ } else {
121+ shutdownPromise = new Promise ( ( resolve ) => {
122+ connection . destroy ( ) ;
123+
124+ logger . info ( `Shutdown [ ${ this . toString ( ) } ] [ ${ this . id } ] immediately` ) ;
125+
126+ resolve ( ) ;
127+ } ) ;
128+ }
129+
130+ return shutdownPromise ;
131+ }
132+
133+ _onDispose ( ) {
134+ if ( this . _connection !== null ) {
135+ this . _connection . destroy ( ) ;
136+
137+ this . _connection = null ;
138+ }
139+
140+ logger . info ( `Disposed [ ${ this . toString ( ) } ] [ ${ this . id } ]` ) ;
141+ }
142+
81143 toString ( ) {
82144 return '[Client]' ;
83145 }
0 commit comments