seam事务管理

小弟最近在学习seam框架,想研究一下其中的seam框架。请大哥们讲讲seam事务处理须要的一些配置,谢谢了先。
[该贴被admin于2009-02-18 16:02修改过]

Seam是一个支撑EJB3的综合框架,事务主要是业务层使用,也就是主要JTA/EJB事务的配置。

很庆幸很感激你的回复。

是的,我比较厌恶的是那些配置文件,从以前的struts到现在学习seam,无不为此困。,回头去看,我花了太多的时间来“配置”,结果呢仍然没有好的效果。框架嘛,一方面把这问题的解决过程分层管理,配置层间的联系,这本是不可少的,想想也是,要了解一个框架并应用某个框架是多么痛苦的事。

seam的配置是非常方便的,非常有限的xml(这些xml几乎在所有的基于seam的project里面都是一样的,且一旦创建后,几乎不会再去修改它们了),其他都是用annotation来配置(如果需要,可以方便的用外部xml配置来覆盖annotation配置)。

components.xml:
seam核心的、也是唯一的一个正真完全由seam使用的xml,所有配置 —— 无论seam自己的、seam对jsf的整合、seam对jbpm的整合、seam对ejb的整合……等等 —— 均遵循统一的原则 (one way principle)

Seam managed transactions 的配置:
默认情况下,seam使用JTA transaction component来容器管理和/或编程式的EJB transactions。

* 如果在Java EE 5+ 环境中,你应该在components.xml中安装EJB synchronization component


<transaction:ejb-transaction />

如果是在非Java EE 5+ 环境中,seam会尝试自动发现事务同步机制并使用之,如果seam无法发现正确的事务同步机制,你可以使用以下配置之一:

* JPA RESOURCE_LOCAL transactions with the javax.persistence.EntityTransaction interface. EntityTransaction begins the transaction at the beginning of the apply request values phase.


<transaction:entity-transaction entity-manager="#{em}"/>

#{em} is the name of the persistence:managed-persistence-context
component. If your managed persistence context is named entityManager, you can opt to leave
out the entity-manager attribute.

* Hibernate managed transactions with the org.hibernate.Transaction interface.
HibernateTransaction begins the transaction at the beginning of the apply request values
phase.


<transaction:hibernate-transaction session="#{hibernateSession}"/>

#{hibernateSession} is the name of the project's persistence:managed-hibernate-session
component. If your managed hibernate session is named session, you can opt to leave out the
session attribute.

* Spring managed transactions with the
org.springframework.transaction.PlatformTransactionManager interface. The Spring
PlatformTransactionManagement manager may begin the transaction at the beginning of the
apply request values phase if the userConversationContext attribute is set.


<spring:spring-transaction platform-transaction-manager="#{transactionManager}"/>

The spring:spring-transaction component will utilize Springs transaction synchronization
capabilities for synchronization callbacks.

* Explicitly disable Seam managed transactions


<core:init transaction-management-enabled="false"/>
<transaction:no-transaction />


详见 seam reference doc [http://docs.jboss.com/seam/latest/reference/en-US/html/]:

* 9.2. Seam managed transactions

* 27.5. Using Spring PlatformTransactionManagement


[该贴被dearshor于2009-02-19 20:42修改过]

Seam managed transactions 通过 conversation 来控制事务。
如果使用hibernate作为EJB 3.0 persistence vendor,那么seam可以让你在开启一个conversation的时候指定flushMode=FlushModeType.MANUAL,这样可以将数据一直保存在内存中,仅在conversation成功结束时才将更改写入数据库(而不是在每个事务结束后立即写入),从而真正实现了atomic convention。
(仅使用EJB 3.0 persistence无法实现atomic convention!Gavin King 原话:
As the result of a truly stupid
and shortsighted decision by certain non-JBoss, non-Sun and non-Sybase members of the EJB
3.0 expert group, there is currently no simple, usable and portable way to implement atomic
conversations using EJB 3.0 persistence.)

seam 实现了conversation scoped的 Persistence context,支持可以跨越多个request的optimistic transactions,无须merge(),无须在每一个request开始时re-load data,不用担心LazyInitializationException or NonUniqueObjectException。

tve很感谢你的介绍。


Seam managed transactions 通过 conversation 来控制事务。
如果使用Hibernate作为EJB 3.0 persistence vendor,那么seam可以让你在开启一个conversation的时候指定flushMode=FlushModeType.MANUAL,这样可以将数据一直保存在内存中,仅在conversation成功结束时才将更改写入数据库(而不是在每个事务结束后立即写入),从而真正实现了atomic convention。

@to
dearshor:
请问,Seam的Conversation在非JTA事务时,使用FlushModeType.MANUAL 是同样的效果吗?