On Sun, 23 Jul 2017 07:00:47 GMT, John wrote:

Hi everybody, I am new to D and I was experimenting with vibe.d and I wanted to use an orm with mysql. I found hibernateD and I was wondering if anyone in this forum has used this package with vibe.d.
I just want to know how I can share and access the connection in every route.

The example on the hibernated page shows the following call in the main method:

Connection conn = ds.getConnection();

I believe you could declare

Connection conn

above the main method, and then have

conn = ds.getConnection();

shared static this() {
    // stuff ...
    DataSource ds = new ConnectionPoolDataSourceImpl(driver, url, params);
    conn = ds.getConnection();
}

in the main method.

Each request handler should then be able to access and use the connection object without problem.

I'm using the MySQL native library which this is based on in the same way and works fine.

Here's an example of my project app.d file that uses MySQL native - the same principals should apply:

import std.stdio;
import std.conv;
import std.string;
import std.exception;

import ctini.rtini;
import mysql;
import vibe.vibe;

import api.handlers.all;
import helpers.emailHelper;

MongoClient mongoClient;
Connection relationalConn;

SMTPSettings smtpSettings;

void main()
{
	auto router = new URLRouter;
	router.registerRestInterface(new AuthHandler(relationalConn, mongoClient, smtpSettings));

	auto settings = new HTTPServerSettings;
	settings.port = 8080;
	settings.bindAddresses = ["::1", "127.0.0.1"];
	settings.errorPageHandler = delegate(HTTPServerRequest req, HTTPServerResponse res, HTTPServerErrorInfo errorInfo) {

		struct APIResponse
		{
			uint code;
			string message;
			string info;
		}

		APIResponse response;
		response.code = errorInfo.code;
		response.message = errorInfo.message;

		if (errorInfo.exception !is null) {
			response.info = errorInfo.exception.msg;
		}

		res.writeBody(response.serializeToJsonString());
	};	

	listenHTTP(settings, router);
	runApplication();
}

shared static this()
{
	// Connect to mysql
	string dbConnectionStr;
	string mongoHost;

	try {
		auto config = iniConfig("config.ini");

		version(release) {
			dbConnectionStr = format(
				"host=%s;port=%d;user=%s;pwd=%s;db=%s",
				config.MySQLProduction.host!string,
				config.MySQLProduction.port!int,
				config.MySQLProduction.user!string,
				config.MySQLProduction.password!string,
				config.MySQLProduction.database!string
			);

			mongoHost = config.MongoProduction.host!string;

			smtpSettings.host = config.SMTPProduction.host!string;
			smtpSettings.port = to!ushort(config.SMTPProduction.port!uint);
			smtpSettings.username = config.SMTPProduction.username!string;
			smtpSettings.password = config.SMTPProduction.password!string;
		} else {
			dbConnectionStr = format(
				"host=%s;port=%d;user=%s;pwd=%s;db=%s",
				config.MySQLDev.host!string,
				config.MySQLDev.port!int,
				config.MySQLDev.user!string,
				config.MySQLDev.password!string,
				config.MySQLDev.database!string
			);

			mongoHost = config.MongoDev.host!string;

			smtpSettings.host = config.SMTPDev.host!string;
			smtpSettings.port = to!ushort(config.SMTPDev.port!uint);
			smtpSettings.username = config.SMTPDev.username!string;
			smtpSettings.password = config.SMTPDev.password!string;			
		}

		writeln(smtpSettings);
	} catch(Exception e) {
		writeln("Exception: ", e.msg);
	}

	enforce(dbConnectionStr != "", "Mysql connection string is empty!");
	enforce(mongoHost != "", "Mongo host string is empty!");

	// Connect to MySQL
	relationalConn = new Connection(dbConnectionStr);

	// Connect to Mongo
	mongoClient = connectMongoDB(mongoHost);

	scope(exit) {
		relationalConn.close();
	}
}