.net core 3.1 BuildSessionFactory error handling #2638
-
I have a problem which i have never come across previously (i have been using NHibernate with .NET framework for many many years) In my current project I am creating my session factory Fluently.Configure().....BuildSessionFactory() and adding it to my DI, that factory is later is then used to open the session and execute sql . In the happy path this is great no problems, all works great. The problem is, if there is an issue with the sql instance - like the database is taken offline, the startup of the website at the point when BuildSessionFactory() executes will error. This then fails to add a session factory to the DI and the site fails to load. And further to this, in .NET core 3 the initialisation will not be retried., therefore I get the same error regardless of if the database is now available. When trying to work around the issue and investigate it, I did manage to hackily replace the session factory (on error) with a temporary fake one (default implementation), to circumvent the initialisation of the app. And then further down the line redirect to the error page if this instance was found. But obviously, then i'm stuck in this situation until the app pool is recycled and the process starts again. Is there a reason that the DB connection is made at this initialisation point - would there be a way to move this part outside of the creation of the factory, so that in subsequent requests its able to retry? (ps i hope i have raised this in the correct repository) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
I believe it's needed to collect DB reserved keywords. To avoid it you can set Also see https://nhibernate.info/doc/nh/en/index.html#configuration-optional with more details about P.S. Documentation is actually wrong stating that |
Beta Was this translation helpful? Give feedback.
-
@bahusoid you are a genuis I hadnt realised thats what it was doing - i just assumed it HAD to do that connection on startup This has solved my issue - as it pushes the error down, after startup and then i can deal with this error and show the relavant error page, and i dont need to worry about recycling the app pool etc as it just worked once the db was available again. Nice one. On a side note, what do i loose by setting it to none? what does that setting actually do? it looks up the reserved words, and then uses them later to work out if it should escape them ? quote them or put [] square brackets? (depending on the db) |
Beta Was this translation helpful? Give feedback.
-
Yes. Without it you need to manually escape keywords whenever you use them using ` (backtick) like:
|
Beta Was this translation helpful? Give feedback.
-
If the options are ... none | keywords | auto-quote If i set it to auto-quote will it not do the db check and just not quote everything? |
Beta Was this translation helpful? Give feedback.
-
So i added the below - not sure if the secondary bit is needed, but im hoping it will quote keywords. |
Beta Was this translation helpful? Give feedback.
I believe it's needed to collect DB reserved keywords. To avoid it you can set
hbm2ddl.keywords
setting tonone
.Also see https://nhibernate.info/doc/nh/en/index.html#configuration-optional with more details about
hbm2ddl.keywords
setting.P.S. Documentation is actually wrong stating that
none
is default value forhbm2ddl.keywords
. Reserved keywords are collected by default.