参照:
readOnly=true表明所注解的方法或类只是读取数据。
readOnly=false表明所注解的方法或类是增加,删除,修改数据。
今天下班后,以前同事小胖问我Spring Service类中的注解@Transactional readOnly=true的作用。做为他眼中的高人,我自然要装下A-C。居然想都没有想就说是注解事务控制,然后给他扯了一通数据库的隔离级别,什么read uncommit之类的,说readOnly是隔离级别最低的,在spring查询方法里用,性能最高。
ps:之前的项目多数基于xml,亲自用annotation的机会很少,都是小弟们在实战。
示例:
@Component("channelService") @Transactional(readOnly = true) public class ChannelServiceImpl implements IChannelService { @Resource(name = "productService") private IProductService productService; /** * 根据频道名字查询频道。没有就返回NULL。 * * @param name 频道名称不能为空。 * @return (假设频道名称是加了唯一约束的,否则可能结果不止一条导致异常) */ @MethodCache(expire = 3600) @Transactional(readOnly = true) public ChannelVO getChannelByName(String name) { if (name == null || "".im())) { throw new IllegalArgumentException("name=" + name); } ShopChannels channel = (ShopChannels) ateCriteria() .add(Restrictions.eq("name", name)) .uniqueResult(); if (channel == null) { return new ChannelVO(); } ChannelVO vo = new ChannelVO(); Instance().map(channel, vo); //增加频道对应商品分类id 只取第一个分类 Set<ProductClass> listCates = ProductClasses(); if (listCates != null && !listCates.isEmpty()) { vo.setProductClass("" + listCates.iterator().next().getSid()); } List<Integer> list = new ArrayList<Integer>(); Iterator<ProductClass> iterator = listCates.iterator(); while (iterator.hasNext()) { ProductClass productClass = (); list.Sid()); } vo.setAllProductClass(list); return vo; } }
数据库隔离相关内容
一般的应用作隔离级别时,往往采用(2),(3)两种,(1),(4)两种前者轻后者重,但是Hibernate为了提高performance和scalability,在数据库一层中采用的是(2)的隔离级别,然后在程序中进行控制,从而实现了(3)的隔离级别,因此提高了企业应用的事务处理效率。当然Hibernate对于数据库一层的隔离级别也可以显示指定。Hibernate在程序中的控制方法有:version number, timestamp,对于遗留database也有optimistic-lock,而version number有时不能解决特殊的事务并发引起来的(3)问题,那么就需要针对特殊情况进行细粒度的事务控制,可以看一下LOCKMODE。
回家反思缓存策略
回家一想,不对,应该不完全是隔离级别的东西,自已可能肯定搞错了,于是赶紧翻看hibernate3.3 reference.果然没说对。应该是二级缓存的策略问题。但不知为什么reference中没有讲到query cache annotation的东西,只是一页带过。提高性能中二级缓存策略倒是讲了不少。
备忘:
Chapter 19. Improving performance
19.2. The Second Level Cache
19.2.1. Cache mappings
The <cache> element of a class or collection mapping has the following form:
<cache usage="transactional|read-write|nonstrict-read-write|read-only" region="RegionName" include="all|non-lazy" />
usage (required) specifies the caching strategy: transactional, read-write, nonstrict-
回归正题@Transactional
再想想,还是不对,hibernate中并没有提到最开始annotation的东西,于是继续纠结Spring3.0 Reference Documentation。
Using @Transactional
In addition to the XML-based declarative approach to transaction configuration, you can use an
annotation-based approach. Declaring transaction semantics directly in the Java source code puts the declarations much closer to the affected code. There is not much danger of undue coupling, because code that is meant to be used transactionally is almost always deployed that way anyway.
annotation事务控制,代替xml的。其它废话少翻。
You can place the @Transactional annotation before an interface definition, a method on an Interface, a class definition, or a public method on a class. However, the mere presence of the
@Transactional annotation is not enough to activate the transactional behavior. The
@Transactional annotation is simply metadata that can be consumed by some runtime infrastructure that is @Transactional-aware and that can use the metadata to configure the appropriate beans with transactional behavior. In the preceding example, the <tx:annotation-driven/> element switches
onthetransactionalbehavior.
//文档例子: @Transactional(readOnly = true) public class DefaultFooService implements FooService { public Foo getFoo(String fooName) { // do something } // these settings have precedence for this method @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public void updateFoo(Foo foo) { // do something } }
@Transactional settings
//默认传播机制:PROPAGATION_REQUIRED * Propagation setting is PROPAGATION_REQUIRED. //默认隔离级别:ISOLATION_DEFAULT * Isolation level is ISOLATION_DEFAULT. //默认事务是read/write.到此知道查询配readOnly的作用。 * Transaction is read/write. //事务默认超时时间取决于事务系统,也有可能没有,如果事务系统不支持。 * Transaction timeout defaults to the default timeout of the underlying transaction system, or to none if time outs are not supported. //任何的RuntimeExcetipn将触发回滚,任何的checkedException不触发回滚。 * Any RuntimeException triggers rollback,and any checkedException does not.
明细配置:
本文发布于:2024-01-28 05:14:04,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17063900505042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |