Giter Club home page Giter Club logo

Comments (9)

alcaeus avatar alcaeus commented on June 8, 2024 1

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.

jmikola avatar jmikola commented on June 8, 2024 1

@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.

hans-thomas avatar hans-thomas commented on June 8, 2024 1

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.

GromNaN avatar GromNaN commented on June 8, 2024 1

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.

GromNaN avatar GromNaN commented on June 8, 2024 1

That seems acceptable to me. Let’s do it.

from laravel-mongodb.

hans-thomas avatar hans-thomas commented on June 8, 2024

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.

GromNaN avatar GromNaN commented on June 8, 2024

Would you like to implement the connect function ? It seems to be in Eloquent.

from laravel-mongodb.

hans-thomas avatar hans-thomas commented on June 8, 2024

@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.

hans-thomas avatar hans-thomas commented on June 8, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.