0
votes

I have an integration test that does a transaction rollback at the end. Recently I added an HQL command to the test. As soon as I did that, many of the other writes in the test began to be committed to the database, as if the HQL command had caused the transaction to be committed. The Rollback command is called at the end, and does not throw an exception, but the data gets committed anyway. Is there any way an HQL command could cause a transaction to commit itself?

The error does not occur if I use a SQL command rather than an HQL command.

Environment: ASP.Net, NHibernate, Oracle.

1
is it the same session and is the transaction opened befor all other write operations? - Firo

1 Answers

1
votes

You're confusing flush and commit. A flush is when NHibernate executes SQL statements to insert/update/delete rows in the database. A commit is when those inserts/updates/deletes become permanent and visible to other transactions.

A flush is done when a HQL query is executed because the query might have to return some entities which have been modified in memory, but whose state has not be written to database yet.

For example, suppose you have the following pseudo-code

1. select all blue bikes
2. change them all to red
3. select all red bikes

You would want the last query to return all the bikes you just made red. So NHibernate flushes the bikes to the database before issuing the last query in order for the query to find them.