[NHibernate] Viewing SQL statements generated by NHibernate

Source: Internet
Author: User

Recently contacted a project with Spring.net combined NHibernate, the first use, there are many configurations, data operation once the problem, it is difficult to find the reason, then how to see NHibernate sent to the database SQL statement?

Of course we can look at it through the log4net log, but it's a little cumbersome, so we find another way to see it.

The onpreparestatement(SqlString sql) defined in the Iinterceptor interface in NHibernate can observe the generated SQL, Emptyinterceptor to achieve.

[Serializable]  Public class   iinterceptor{   
Other methods omit public virtual SqlString onpreparestatement (SqlString sql); }

All we have to do is inherit the emptyinterceptor and rewrite the onpreparestatement method.

 Public class  sqlwatcheremptyinterceptor{    publicoverride   SqlCommand . SqlString Onpreparestatement (sqlcommand.sqlstring sql)    {        returnbase. Onpreparestatement (SQL);}    }

In the overridden method, you can use any method to monitor the SQL statements you want to monitor.

Finally, you can add the interception class you created in the place where you created the ISession.

 Public Static ISession opensession () {     return sessionfactory.opensession (new   sqlwatcher ());}

The results are as follows:

[NHibernate] Viewing SQL statements generated by NHibernate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.