How to Insert into NHibernate DB after NHibernate Session - nhibernate

Let's say if I encounter an error in NHibernate db update, and when such an exception is thrown, I want to log it in the log table in the db, via NHibernate again.
Now, since I can't reuse NHibernate session after an exception thrown, I will get an error when I log into the log table. How to best handle this situation?

Open a new session.
Or use a logging framework like log4net with ADO.NET appender and do it completely separately out of NHibernate environment.

IMO logging should be a cross cutting concern of your application. You should not try to reuse the same session in order to log errors. NHibernate already uses log4net. All you need is to configure an appender that will write logs to the SQL database.

Related

Execute sql statement before normal execution with aop

I'm trying to create a Multitenant application with spring.
I'm trying to have a different schema for each Tenant on a PostgreSql database.
I first created a TenantAwareDataSource extending org.springframework.jdbc.datasource.AbstractDataSource where basically I manage a Map of org.apache.commons.dbcp.BasicDataSource, configuring setConnectionInitSqls() for each tenant. (The datasource the project had before was org.apache.commons.dbcp.BasicDataSource)
But then discussing it with a friend, we came up with the idea of changing the schema for every statment executed with an aspect (aop), simply adding a set search_path to statement just before normal execution.
This could greatly simplify the problems related to having too many connections to the database (a connection pool for every tenant at any given time).
Has anybody executed additional statements using AOP?
Any pitfalls to overcome?
I'm thinking on put back org.apache.commons.dbcp.BasicDataSource and intercept java.sql.Statements.exe*(..)
I'm not very experienced with Spring persistence. Or SQL statement execution interception for that matter (haha).
Is it ok?
I found this article but I don't think I need to obtain a reference for each connection.
Am I right?
Also found this one. The author is using org.springframework.jdbc.core.JdbcOperations. Not sure it's the case in my Spring Roo generated project.
Thank you all.

How to log NHibernate query?

I have an Asp.Net MVC 4 project in VB.Net, which use Fluent Nhibernate, NHibernate and NLog. I want to log the NHibernate queries. I want to save the query at a table in my DB, but it also can be showed in a log file. Does anyone know how I can do this?
Check out this answer on how to configure NHibernate to use NLog.
NHibernate will log all queries to the NHibernate.SQL logger.

Hibernate not getting manually INSERTed object

I was given a pre-created project that uses Hibernate and was asked to add some features to it.
The database it came with has a user table and there is a login servlet page that uses hibernate to log in a user with provided credentials.
To get the very first user in the DB I manually INSERTed it with an SQL tool. Now when I try to log in using those credentials it fails. Is there anything extra I need to do to be able to use that manually added user?
No, to read existing data, there's nothing that needs to be done to the row itself in the database to make Hibernate read it. Your problem is one of a myriad of other possibilities, such as your Hibernate being configured to read from some other database or table, or encountering an error that you need to go find in the log.

Connect NHibernate to different databases with same schema

We are in the process of splitting our db into several smaller ones. The schemas will be exactly the same and we will control which db the system connects to when the client logs in. I receive an error if I do not set a connection string in my nhibernate configuration. I do not want to create a factory for each db. Is it possible to have a session factory provide a Session that I can set the connection string before using it?
Have not used it but there is a method ChangedDatabase on the Session.Connection. Maybe that would work?
Maybe you can use NHibernate.Shards, in the NHcontrib repository

NHibernate - logging

I'm using log4net with NHibernate and i'm logging SQL statements generated by NHibernate.
Is there any way to instruct NHibernate to only log DML SQL statements (inserts, updates, deletes), for example something like "NHibernate.SQL" + ".DML" in log configuration?
You should just be able to add another logger node to your configuration file for NHibernate.DML That will log both.
No. Unfortunately there is no built in way for what you want.