Comments (9)
Before we do that, what's the use case for this? The driver manages connections automatically, and except for in special circumstances, the user shouldn't have to worry about them. Note that the driver doesn't allow closing the connection either, and unless you disable client persistence (which we don't recommend except for very special circumstances) even unsetting the client instance will not close the connection.
from laravel-mongodb.
@hans-thomas: regarding your solution in #2649 (comment), listing database names goes beyond what's needed to ensure the driver actually connects, and in some environments that might also require special privileges on the authenticated user. The simplest approach would be to issue a ping
command (admin
database would be a fine choice). Alternatively, you can call Manager::selectServer()
since topology discovery is a precondition of server selection, and that will ensure the driver initializes its connections. When selecting a server, I'd also suggest using "primaryPreferred" instead of "primary" to avoid potential server selection failures if the primary was temporarily unavailable (assuming we only care about socket initialization here).
Per your other comment in #2649 (comment), what is the relation to SQLiteDriver? Or were you just using that as an example of the ConnectsToDatabase trait?
from laravel-mongodb.
Thank you for your comment. I will give it a try later.
Per your other comment in #2649 (comment), what is the relation to SQLiteDriver? Or were you just using that as an example of the ConnectsToDatabase trait?
Nothing, I just wanted to clarify that there is no connect
method publically available on DB
facade. I used SQLiteDriver
for example to check connect
method availability.
from laravel-mongodb.
Closing as solutions to check the connection have been provided by @jmikola:
use Illuminate\Support\Facades\DB;
use MongoDB\Driver\ReadPreference;
use MongoDB\Laravel\Connection;
DB::connection()->getMongoClient()->getManager()->selectServer(new ReadPreference(ReadPreference::PRIMARY_PREFERRED));
The method Illuminate\Database\Connectors\ConnectorInterface::connect()
must return a PDO
instance, which we can't do. So we will not implement this method.
from laravel-mongodb.
That seems acceptable to me. Let’s do it.
from laravel-mongodb.
I think this should answer your need.
DB::connection()->getMongoClient()->listDatabaseNames()
If it connects successfully, it will return database names as ArrayIterator
.
But if it fails, it will throw MongoDB\Driver\Exception\ConnectionTimeoutException
exception.
from laravel-mongodb.
Would you like to implement the connect
function ? It seems to be in Eloquent.
from laravel-mongodb.
@GromNaN The connect
method is declared here that used in SQLiteDriver class. The SQLiteDriver
is registered in SQLiteConnection in a protected getter method. So, there is no way to call connect
.
I can create a method on MongoDB\Laravel\Connection
to check the connection using the given configs and name it checkConnection
or something like this to check the connection status and return a boolean
as the result. do you agree with this solution?
from laravel-mongodb.
@GromNaN I think it's better to make a new ping
method on Connection
class to make it easy for users to access this feature.
from laravel-mongodb.
Related Issues (20)
- ObjectId in query builder not working HOT 6
- function addUpdatedAtColumn use array_merge bug HOT 1
- Cannot query with an array position HOT 2
- Add documentation on polymorphic relationships
- Fetching data via tinker HOT 4
- [Feature Request] Queryable Encryption or CSFLE HOT 1
- Call to undefined method MongoDB\Laravel\Query\Builder::compileWhereSub() HOT 2
- Is MongoDB support Laravel Octane (FrankenPHP server) HOT 1
- whereHas issue HOT 1
- Fatal casting issues HOT 1
- Saving/refreshing model doesn't reset `Model::$unset` HOT 3
- Register the Mongo driver through the make function instead of resolving function HOT 5
- Need to support upsert function in Laravel 10 HOT 1
- datetime casts not considering timezone HOT 2
- Transaction already in progress HOT 6
- Connection string is missing the option to create dsn as a SRV dsn HOT 3
- First argument of MongoDB\Laravel\Query\Builder::where must be a field path as "string". Got "null" HOT 6
- seperate read, write connection not working HOT 1
- castAttribute does not handle nullable immutable date times
- DatabaseTransactions and mongodb
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-mongodb.