java事务的四个属性可概括为ACID (Atomicity,Consistency,Isolation,Durability)
原子性(Atomicity) 保证多个操作的原子性,从而将他们组织在一起构成工作单元。
一致性(Consistency) 在事务完成后,一致性保证系统的状态是一致的。
隔离性(Isolation) 在事务执行过程中,隔离性能保证各个事务不能够看到对方的中间结构。
持续性(Durability) 他能够保证事务对守管资源所作的更新永久生效。
在j2ee中使用事务的两种方式是
容器管理事务(Container-Managed Transaction ,CMT)
事务的实现由ejb容器完成,我们仅仅需要在ejb部署描述符配置便可以。
如下面的部署描述符
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Employee</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
上面的部署符 表示
对bean Employee 的虽有方法( <method-name>*</method-name>,也可以指定其中的某一方法)
的属性取职值为required。
属性取值就是 定义对应的方法在事务中的所执行的方式,属性值有
Required, RequiresNew,Supports,Mandatory,NotSupported,Never
各属性的详细信息参照《精通EJB 》第三版 第12章 “事务”。
另一种实现事务的方式就是编程式事务。
不考虑复杂的事务类实现细节,编程式事务可使用java事务api JTA 来实现。
package javax.transaction;
public abstract interface UserTransaction {
// Methods
void begin() throws NotSupportedException, SystemException;
void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException;
void rollback() throws IllegalStateException, SecurityException, SystemException;
void setRollbackOnly() throws IllegalStateException, SystemException;
int getStatus() throws SystemException;
void setTransactionTimeout(int _int) throws SystemException;
}