Spring supports the concept of read-only transactions. Because Spring doesn’t provide persistence functionality itself, the semantics of read-only transactions depend on the underlying persistence framework used along with Spring.
I use Spring with Hibernate and Oracle, and when I looked to understand the semantics of read-only transactions on this specific configuration, I found that there was very little information on the web. The existing information is scarce and not very clear, and as a result I had to do some research myself, which included hacking into Spring and Hibernate’s source code. Not that I don’t enjoy spending a few late hours reading good code, but so that you don’t have to do it yourself, here is what I found.
Spring
Spring’s documentation doesn’t say almost anything about what a read-only transaction really means. The best information I could find was:
Read-only status: a read-only transaction does not modify any data. Read-only transactions can be a useful optimization in some cases (such as when using Hibernate).
That’s basically all it says. Google and a little hacking shed some light on the real meaning of the sentence above: if the transaction is marked as read-only, Spring will set the Hibernate Session’s flush mode to FLUSH_NEVER, and will set the JDBC transaction to read-only. Now lets understand what it means and what are the implications in a Hibernate/Oracle setup.
Hibernate
Hibernate doesn’t have the concept of read-only sessions. But when a session’s flush mode is set to FLUSH_NEVER, which is what Spring does, two interesting things happen. First, running HQL queries no longer cause Hibernate to flush the session state to the database, which can provide a dramatic performance improvement. Secondly, Hibernate will not flush the changes before commiting the transaction. But the user can still call Session.flush() by hand, causing any modifications to be persisted to database. This is where Spring’s call to Connection.setReadOnly() comes handy.
Oracle
When using the Oracle JDBC driver, calling connection.setReadOnly(true) translates into the statement “SET TRANSACTION READ ONLY”. This statement limits the types of SQL statements that can be executed during the transaction. Only SELECTS (without ‘FOR UPDATE’) and a few other statements can be executed. Specifically, no UPDATEs, DELETEs, INSERTs or MERGEs can be executed. This behavior is Oracle-specific. Other RDBMS can have different semantics for read only transactions or simply not support it at all.
By setting the JDBC connection to read-only, Spring prevents a distracted user from persisting changes by flushing the Hibernate session to the database.
Notes
As we saw, with the two measures taken by Spring, the transaction is guaranteed to be read-only through the JDBC connection, and performance improvements are obtained by setting the Hibernate session to FLUSH_NEVER.
There is one thing that doesn’t happen, though. Even during Spring read-only transactions, Hibernate queries still save the state of persistent objects in the session cache. In theory it wouldn’t be necessary, since this state is used to detect modifications during session flushes. Depeding on the size and number of objects it can make a huge difference in terms of memory usage.
If you still want to prevent Hibernate from saving the object state in the session cache, you have to manually run the HQL queries in read-only mode. It would be a nice improvement to Hibernate to have a read-only mode to the session so that no object state is stored and no flush executed
分享到:
相关推荐
通过上述对几个常见 Hibernate 错误的解析及解决方法的介绍,我们可以发现大部分问题都与配置文件的设置、实体类的定义以及事务管理有关。在日常开发中,我们应该注意这些细节,以避免不必要的错误发生。同时,对于...
5. **Hibernate在Spring和Struts中的应用**: - Hibernate简化了数据访问层的代码,提供ORM(对象关系映射)解决方案,增强了反射机制,性能优秀,支持多种关系映射。 6. **延迟加载(Lazy Loading)**: - ...
例如,“add*”、“delete*”、“modify*”和“search*”方法将被标记为必须在一个新事务中执行(`propagation="REQUIRED"`),而其他未命名的方法默认为只读(`read-only="true"`)且支持现有事务(`propagation=...
在Java开发领域中,Spring框架因其强大的功能、灵活的配置和广泛的社区支持而备受青睐。本文将深入探讨一个典型的Spring配置文件,解析其中的关键配置项及其功能,帮助读者更好地理解如何利用Spring框架进行项目开发...
- **只读模式(Read-Only)**:在这种模式下,一旦数据放入缓存后就不会再发生变化,直到缓存失效或者被明确地清除。 ### 二、存储过程(Stored Procedure) 存储过程是一种预先编译好的SQL代码块,它被存储在...
12. **EJB事务级别**:EJB支持七级事务隔离级别,分别是Read Uncommitted、Read Committed、Repeatable Read、Serializable、Read Only、Batch Read Only、Client Controlled,每级对应不同的并发控制策略。...
这可以通过在Hibernate配置文件中设置`hibernate.connection.autocommit`属性为`false`,并在查询前手动设置事务,然后通过`Session`的`createSQLQuery()`或`createQuery()`方法附加`ResultSet.TYPE_SCROLL_...
spring和Hibernate继承后,定义事务管理特性的时候查询为什么要定义为read-only? 答:因为添加、删除和更新都涉及到了数据库的修改,而查询并未涉及到数据库修改,所以只需要定义只读,这样可以提高效率,进行更加...
- **Repeatable Read**:事务在执行过程中始终看到的是事务开始时的状态。 - **Serializable**:最高隔离级别,完全避免脏读、不可重复读、幻读等问题。 #### 第四层:LINUX 招式辨析 **1. awk 和 sed** - **awk*...