Query Listener priority - datasource not found

We have a multi-tenant app where each tenant is a separate database. We’re trying to containerize our app and while we can load the known datasources on server startup, we need a way to add datasources for tenants created after the application has started.

Was playing around with Query Listener as a way to check if a datasource is loaded, and if not, do a tenant lookup and add the datasource. But then I ran into a roadblock - if the datasource doesn’t exist, Lucee will throw an exception prior to calling the Query Listener’s before method.

Knowing that, we’ll have to alter our approach, but wondered if there was a specific reason why it was implemented this way?

Thanks all!

Nobody has asked this before!

The underlying assumption is that by the time you reach a query, your data source is configured, either in the admin, application cfc constructor, or one of those onXXXstart methods?

Just wrap that query in a try catch, if you want to continue that approach?

1 Like

In our main application, we’re doing it by validating the tenant datasource when the session starts. However, in the case I’m trying to solve is our backend administration app that does reporting and manages options across tenants. So unfortunately there’s not just one query that would have to be wrapped, or a single entry point like a tenant session starting. Catching the exception in the global error handler wouldn’t handle all cases either. That’s what made the Query Listener so appealing.

I think in this case we’ll have to write some sort of UDF and use that when we reference the datasource in the cfquery attribute.

Yeah a function to return the datasource to the query would work