2013-09-27 39 views

回答

4

当您只从数据库读取/选择数据并且不更改任何数据时 - 通过执行更新/插入/删除。

如果您可以指定readOnly,那么您应该将其作为资源密集程度低得多。

2

定义非常简单:当且仅当您确保没有更新,插入或删除操作发生在事务内部时,您可以使用readonly=true。 这优化了dbms的锁定行为(如果支持)。

readonly默认为false。

最好的问候,

SAM

编辑

/** 
* {@code true} if the transaction is read-only. 
* Defaults to {@code false}. 
* <p>This just serves as a hint for the actual transaction subsystem; 
* it will <i>not necessarily</i> cause failure of write access attempts. 
* A transaction manager which cannot interpret the read-only hint will 
* <i>not</i> throw an exception when asked for a read-only transaction. 
* @see org.springframework.transaction.interceptor.TransactionAttribute#isReadOnly() 
*/ 
boolean readOnly() default false; 
1

自定义添加更多的控制您的事务的隔离级别。

如果你知道一个方法是只读的,你应该指定它。

随着readonly = true,您正在向事务管理器说,一个特定的方法只能从DB中读取。这有两个好处:首先,它可以比其他的更快,因为它允许DBMS优化事务(如果支持的话)。其次,它可以帮助您避免死锁问题(例如特定的表被写入锁定时),因为您确保该方法不会执行INSERT或UPDATE。

但是,在这里您可以找到关于它的所有细节: http://docs.spring.io/spring/docs/2.5.x/reference/transaction.html