Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Connection Parameters with NHibernate

We have a program where users can specify their database connection parameters. The usual suspects including host, port, username, password, and table name. We are connecting to the database using NHibernate. What we'd like to do is be able to build the configuration with NHibernate and then test the connection parameters before continuing with other operations; notifying the user of failure.

Is this possible to do through NHibernate, or will it require using each database type we support's specific driver and creating a custom TestConnection() method for each type?

like image 988
MedTechDeveloper Avatar asked Feb 17 '26 09:02

MedTechDeveloper


1 Answers

I realize this is an old post - but I guess an answer to a question never hurts.

I don't think there is a way to explicitly tell NHibernate to test out the connection string. However, when you instantiate the SessionFactory it will attempt to connect to the database. You could wrap your SessionFactory creation in a Try/Catch and handle the error that way.

I use Fluent NHibernate, but I'm sure the following example will still explain the situation.

Dim sf as SessionFactory
Try
    sf = CreateSessionFactory()
Catch ex as FluentNHibernate.Cfg.FluentConfigurationException
    Messagebox.Show(ex.InnerException.Message)
End Try

The ex.InnerException.Message contains the actual error and will tell you if:

  • The connection string was invalid
  • The server could not be found
  • The user/pass could not be authenticated
like image 58
Origin Avatar answered Feb 20 '26 02:02

Origin