RejectedSoftware Forums

Sign up

Pages: 1 2

static this db initalization failure

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

Re: static this db initalization failure

On Wed, 02 Nov 2016 00:55:55 GMT, Uiy Uiy wrote:

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

The event loop is not running yet in static this.

Re: static this db initalization failure

On Sat, 05 Nov 2016 01:29:10 GMT, ChangLong wrote:

On Wed, 02 Nov 2016 00:55:55 GMT, Uiy Uiy wrote:

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

The event loop is not running yet in static this.

So, the question is how to get it to run before(order the calls of the static this's). There is obviously a dependency issue.

Re: static this db initalization failure

On Mon, 07 Nov 2016 05:02:47 GMT, Uiy Uiy wrote:

On Sat, 05 Nov 2016 01:29:10 GMT, ChangLong wrote:

On Wed, 02 Nov 2016 00:55:55 GMT, Uiy Uiy wrote:

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

The event loop is not running yet in static this.

So, the question is how to get it to run before(order the calls of the static this's). There is obviously a dependency issue.

If it used in web server, you can use connect pool or create new one for each request.

if not from web server, you can run it from the task delegate.

Re: static this db initalization failure

On Mon, 07 Nov 2016 10:21:58 GMT, ChangLong wrote:

On Mon, 07 Nov 2016 05:02:47 GMT, Uiy Uiy wrote:

On Sat, 05 Nov 2016 01:29:10 GMT, ChangLong wrote:

On Wed, 02 Nov 2016 00:55:55 GMT, Uiy Uiy wrote:

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

The event loop is not running yet in static this.

So, the question is how to get it to run before(order the calls of the static this's). There is obviously a dependency issue.

If it used in web server, you can use connect pool or create new one for each request.

if not from web server, you can run it from the task delegate.

Where do I find out how to do this? (both cases)

Re: static this db initalization failure

On Wed, 02 Nov 2016 00:55:55 GMT, Uiy Uiy wrote:

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

I just tried this locally and it worked for me for the latest master version of vibe.d. Which version are you using and what error exactly occurs?

Re: static this db initalization failure

On Thu, 10 Nov 2016 09:17:15 GMT, Sönke Ludwig wrote:

On Wed, 02 Nov 2016 00:55:55 GMT, Uiy Uiy wrote:

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

I just tried this locally and it worked for me for the latest master version of vibe.d. Which version are you using and what error exactly occurs?

latest all(non-beta).

I used this in a separate module, so that might be the issue. I think it is simply the order of execution of the static this.

The errors are

First-chance exception: object.Exception Failed to connect to host 127.0.0.1:27017: Connection refused [WSAECONNREFUSED ] at vibe\core\drivers\libevent2.d(326)

First-chance exception: vibe.db.mongo.connection.MongoDriverException Failed to connect to MongoDB server at 127.0.0.1:27017. at vibe\db\mongo\connection.d(169)

First-chance exception: core.demangle.Demangle.ParseException

If I simply initialize the variables in the vibe.d's static this function, everything works(copy and paste the init code above).

Is there a way to pass a function to be executed by vibe.d after it is done initializing but before normal execution?

Re: static this db initalization failure

On Fri, 11 Nov 2016 05:11:19 GMT, Uiy Uiy wrote:

Is there a way to pass a function to be executed by vibe.d after it is done initializing but before normal execution?

I suggest using the singleton with lazy initialization, wherein the connection is to be opened at the first call.

Re: static this db initalization failure

On Fri, 11 Nov 2016 05:41:09 GMT, Alexey Kulentsov wrote:

On Fri, 11 Nov 2016 05:11:19 GMT, Uiy Uiy wrote:

Is there a way to pass a function to be executed by vibe.d after it is done initializing but before normal execution?
I suggest using the singleton with lazy initialization, wherein the connection is to be opened at the first call.

This seems to be overkill simply to initialize a variable in the correct order.

Re: static this db initalization failure

On Sat, 12 Nov 2016 05:36:16 GMT, Uiy Uiy wrote:

This seems to be overkill simply to initialize a variable in the correct order.

Correct order of static vars initialization is not simple.

Pages: 1 2