Hibernate Envers SQL auditing - sql

I was wondering if anyone had succeeded in auditing a native query (SQL) with Hibernate Envers? I know this is probably just wrong, but it would spare me a lot of refactoring time.
Cheers
Nick

I just want to leave my thoughts here so others might benefit when they choose to Envers. We tried Hibernate envers in one our recent project and it did not work out. Below are the reason
Hibernate Envers captures the Audit information only when the updates happen through Persistence Context.
We did not like one audit table for each entity. It was too much schema pollution.
We have lot of batch jobs and data synchronization scripts that updates data directly using sql queries. Any update that is happening outside the persistence context will not be captured in these Hibernate ENvers created Audit tables.
SO we went with Database trigger appraoach with just only one AUDIT table which will capture the table_name, column_name, primary_key, old_value and new_value. It worked for us.

Related

Work around for envers auditing for bulk update

In the application which I am working on I use spring, hibernate and envers for auditing. envers works with calls like, hibernateTemplate.insert, hibernateTemplate.save, hibernateTemplate.saveOrUpdate. But it doesnt seem to work when i call hibernateTemplate.bulkUpdate.
I googled for solutions and found that envers doesnt support bulkUpdate.
A work around has been provided in the link below but i am not able to get it.
Envers Bulk insert/updates
It would be of help if someone can provide a workaround/sample for this.
Thanks
The documentation is correct. HQL and native SQL operations are not audited.
Since the performance of the bulk update will be affected by auditing, you may wish to change your design - for example, if you have a parent entity with related children, and you are performing a bulk update on the child records, you could update attributes of the parent record and then call saveOrUpdate after doing the bulk update.
Another option, is to manually perform the bulk update on the audit table(s) also, but its not going to be elegant. I managed to get around the issue in my case by changing the design as per the above.

NHibernate, Caching and custom SQL queries

We're using NHibernate with Memcache as the second level cache. Occasionally there is a need for more advanced queries or bulk query operations. From the book Nhibernate in Action they recommend the following:
"It’s our view that ORM isn’t suitable for mass-update (or mass-delete) operations. If
you have a use case like this, a different strategy is almost always better: call a stored
procedure in the database, or use direct SQL UPDATE and DELETE statements for that
particular use case."
My concern is that queries against the underlying database do not reflect in the cache (at least until cache expiry) and I was wondering if anyone has come up with any effective strategies for mixing and matching NHibernate with custom SQL statements?
Is there any way of getting say a bulk Update statement (executed with custom sql) to reflect in the second level cache? I am aware of being able to manually evict, but this removes the items from cache and thefore increases hits on the database.
Does the community have any solutions that have been found to be effective in dealing with this problem?
As far as I know there is no method to keep the 2nd level cache up to date with massupdates. But you can partially evict the cache as described in: http://www.nhforge.org/doc/nh/en/index.html#performance-sessioncache.

How to find the last time a table is queried in oracle

I want to get the information about when a table is queried in oracle.
Is there any log in oracle which shows the queries. I was looking around v$sqlarea and v$sqltext but, the system admin does not allow me to reach those tables.
In a default installation I know of no way to reliably get this info. You may be able to catch SQL Statements that were recently run in v$sql* views, but v$sql* views are transient in nature and are used to support normal operations of the database. Statements can age out so it is not a reliable way to audit.
What is a proper reliable way to get this info? Oracle Auditing. It contains the ability to record fine grained information about how your database objects are touched.
In this case you will want to investigate the AUDIT SELECT. After doing the basic config for auditing (usually done by a DBA) then SELECT auditing can be set up for specific tables like this:
AUDIT SELECT ON employees;
When a user SELECTS from employee, either directly or through a view, a record will be written to the audit trail (text file or SYS.AUD$ depending on configuration). The trail will have username, timestamp, table_name, and some other information to help you determine what the user was doing at the time.
Here is a 9i reference for auditing that gives an overview including info on AUDIT SELECT: http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c25audit.htm
Be aware that fine grained auditing can slow things down. Whatever you are auditing now has a new layer of activity that must be completed (writing to the audit trail). If you have a business need to know who sees what data that is understandable, but make sure to be aware of the performance implications.

NHibernate 3 + Transaction Deadlocks (S#arp Architecture)

I appear to be getting a lot of random deadlocks when reading data from one of my tables. This table contains alot of information and is very frequently read and updated.
I am using S#arp Architechture 1.9 which uses the Transaction attribute on all my data access / update code.
Is there anything special which I need to do to ensure I don't get deadlocks, Should i update / read my data in a certain way.
Not too sure where to start on this one.
NHibernate 3
S#arpArchitecture 1.9
SQL Server 2008 R2
Thanks.
Are you getting actual deadlocks or blocked reads? If it is the former, consider rebuilding indexes and statistics.

Nhibernate Auditing

I have a question about auditing. Most auditing examples use one audit table to track changes. However, we need one audit table per "regular" table. In other words, tblCustomer would also have tblCustomer_History. I can't figure out how to use a listener, and on update populate the history table as well. Any ideas? I'd hate to fall back on SQL Server triggers.
If you don't want to build your own audit solution, maybe you should have a look at NHibernate Envers?
https://bitbucket.org/RogerKratz/nhibernate.envers