Add new createLazyConnection() method to only connect only on demand (on first command) #87
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a new
Factory::createLazyConnection()
method.It helps with establishing a TCP/IP connection to your MySQL database
and issuing the initial authentication handshake.
This method immediately returns a "virtual" connection implementing the
ConnectionInterface
that can be used tointerface with your MySQL database. Internally, it lazily creates the
underlying database connection (which may take some time) only once the
first request is invoked on this instance and will queue all outstanding
requests until the underlying connection is ready.
From a consumer side this means that you can start sending queries to the
database right away while the actual connection may still be outstanding.
It will ensure that all commands will be executed in the order they are
enqueued once the connection is ready. If the database connection fails,
it will emit an
error
event, reject all outstanding commands andclose
the connection as described in the
ConnectionInterface
. In other words,it behaves just like a real connection and frees you from having to deal
with its async resolution.
Note that creating the underlying connection will be deferred until the
first request is invoked. Accordingly, any eventual connection issues
will be detected once this instance is first used. Similarly, calling
quit()
on this instance before invoking any requests will succeedimmediately and will not wait for an actual underlying connection.
Depending on your particular use case, you may prefer this method or the
underlying
createConnection()
which resolves with a promise. For manysimple use cases it may be easier to create a lazy connection.
Builds on top of #86 and #84
This is also done in preparation for a future connection pool (#27)