gavin wrote:
Juergen, this is just wrong. There is no "notransactional database access". What you are describing is autocommit, where a transaction is automatically committed at the end of each request to the database.
Gavin, I know :-) You might have noticed that I explicitly stated that I agree in general. I also didn't talk about non-transactional
database access, just about non-transactional
Hibernate Session usage when you expect to almost always hit the cache. I completely agree that transactional demarcation for actual database operations makes sense.
The special case I outlined is: If you can assume that 99% of your read operations will hit the second-level cache, it is significantly less overhead to just the hit the cache and not touch the JDBC driver in the first place, not even for transaction handling. Consider, for example, applications that often fetch the same persistent objects via id.
Demarcating a JDBC transaction at least means fetching a JDBC Connection from the pool and invoking commit at the end of the operation. (If default auto-commit is "true", you also need to set auto-commit to "false" at begin.) As this is not necessary when you just hit the cache, it unnecessarily incurs resource management overhead.
With Oracle, it makes a strong difference whether the pool has default auto-commit set to "false": The overhead is significantly less then. Unfortunately, you cannot influence the pool definition in some scenarios: You'll have to live with explicitly invoking setAutoCommit(false) then, which does incur a significant overhead on Oracle.
At least that's what I was told by various long-time Oracle users; I also experienced it myself recently. Particularly for setAutoCommit(false), the difference between Oracle and MySQL
is significant. And as I said, why go through JDBC resource handling in the first place - for single loads that almost always hit the second-level cache?
Juergen