1、类结构图
2、类分析
import org.springframework.core.Constants;
import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;
import java.io.Serializable;
/**
* 可以定义:事务传播、隔离级别、超时等。
*
* 这个类是{@link TransactionDefinition}接口的默认实现,
* 提供bean风格的配置和合理的默认值(PROPAGATION_REQUIRED、ISOLATION_DEFAULT、TIMEOUT_DEFAULT、readOnly=false)。
*
* 这个类是
* {@link TransactionTemplate}和
* {@link org.springframework.transaction.interceptor.DefaultTransactionAttribute}
* 的基类。
*/
@SuppressWarnings("serial")
public class DefaultTransactionDefinition implements TransactionDefinition, Serializable {
/** TransactionDefinition中定义的传播常数的前缀。 */
public static final String PREFIX_PROPAGATION = "PROPAGATION_";
/** 在TransactionDefinition中定义的隔离常数的前缀。 */
public static final String PREFIX_ISOLATION = "ISOLATION_";
/** 描述字符串中事务超时值的前缀。 */
public static final String PREFIX_TIMEOUT = "timeout_";
/** 描述字符串中只读事务的标记。 */
public static final String READ_ONLY_MARKER = "readOnly";
/** 事务定义的常数实例。 */
static final Constants constants = new Constants(TransactionDefinition.class);
private int propagationBehavior = PROPAGATION_REQUIRED;
private int isolationLevel = ISOLATION_DEFAULT;
private int timeout = TIMEOUT_DEFAULT;
private boolean readOnly = false;
@Nullable
private String name;
/**
* 使用默认设置创建一个新的DefaultTransactionDefinition。
* 可以通过bean属性设置器进行修改。
*
* @see #setPropagationBehavior
* @see #setIsolationLevel
* @see #setTimeout
* @see #setReadOnly
* @see #setName
*/
public DefaultTransactionDefinition() {
}
/**
* Copy constructor.
* Definition can be modified through bean property setters.
*
* @see #setPropagationBehavior
* @see #setIsolationLevel
* @see #setTimeout
* @see #setReadOnly
* @see #setName
*/
public DefaultTransactionDefinition(TransactionDefinition other) {
this.propagationBehavior = other.getPropagationBehavior();
this.isolationLevel = other.getIsolationLevel();
this.timeout = other.getTimeout();
this.readOnly = other.isReadOnly();
this.name = other.getName();
}
/**
* 使用给定的传播行为创建一个新的DefaultTransactionDefinition。
* 可以通过bean属性设置器进行修改。
*
* @param propagationBehavior TransactionDefinition接口中的传播常数之一
* @see #setIsolationLevel
* @see #setTimeout
* @see #setReadOnly
*/
public DefaultTransactionDefinition(int propagationBehavior) {
this.propagationBehavior = propagationBehavior;
}
/**
* 通过TransactionDefinition中相应常数的名称设置传播行为,
* 如:“PROPAGATION_REQUIRED”。
*
* @param constantName name of the constant
* @throws IllegalArgumentException if the supplied value is not resolvable
* to one of the {@code PROPAGATION_} constants or is {@code null}
* @see #setPropagationBehavior
* @see #PROPAGATION_REQUIRED
*/
public final void setPropagationBehaviorName(String constantName) throws IllegalArgumentException {
if (!constantName.startsWith(PREFIX_PROPAGATION)) {
throw new IllegalArgumentException("Only propagation constants allowed");
}
setPropagationBehavior(constants.asNumber(constantName).intValue());
}
/**
* 设置传播行为。必须是TransactionDefinition接口中的传播常数之一。默认是PROPAGATION_REQUIRED。
* 专门设计用于{@link #PROPAGATION_REQUIRED}或{@link #PROPAGATION_REQUIRES_NEW},因为它只适用于新启动的事务。
* 如果您希望在参与具有不同隔离级别的现有事务时拒绝隔离级别声明,请考虑将您的事务管理器上的“validateExistingTransactions”标志切换为“true”。
* 请注意,不支持自定义隔离级别的事务管理器在给出除{@link #ISOLATION_DEFAULT}之外的任何其他级别时将抛出异常。
*
* @throws IllegalArgumentException if the supplied value is not one of the
* {@code PROPAGATION_} constants
* @see #PROPAGATION_REQUIRED
*/
public final void setPropagationBehavior(int propagationBehavior) {
if (!constants.getValues(PREFIX_PROPAGATION).contains(propagationBehavior)) {
throw new IllegalArgumentException("Only values of propagation constants allowed");
}
this.propagationBehavior = propagationBehavior;
}
@Override
public final int getPropagationBehavior() {
return this.propagationBehavior;
}
/**
* 通过TransactionDefinition中相应常量的名称设置隔离级别,例如:“ISOLATION_DEFAULT”。
*
* @param constantName name of the constant
* @throws IllegalArgumentException if the supplied value is not resolvable
* to one of the {@code ISOLATION_} constants or is {@code null}
* @see #setIsolationLevel
* @see #ISOLATION_DEFAULT
*/
public final void setIsolationLevelName(String constantName) throws IllegalArgumentException {
if (!constantName.startsWith(PREFIX_ISOLATION)) {
throw new IllegalArgumentException("Only isolation constants allowed");
}
setIsolationLevel(constants.asNumber(constantName).intValue());
}
/**
* 设置隔离级别。必须是TransactionDefinition接口中的一个隔离常量。
* 默认是ISOLATION_DEFAULT。
* 专门设计用于{@link #PROPAGATION_REQUIRED}或{@link #PROPAGATION_REQUIRES_NEW},因为它只适用于新启动的事务。
* 如果您希望在参与具有不同隔离级别的现有事务时拒绝隔离级别声明,
* 请考虑将您的事务管理器上的“validateExistingTransactions”标志切换为“true”。
*
* 请注意,不支持自定义隔离级别的事务管理器在给出除{@link #ISOLATION_DEFAULT}之外的任何其他级别时将抛出异常。
*
* @throws IllegalArgumentException if the supplied value is not one of the
* {@code ISOLATION_} constants
* @see #ISOLATION_DEFAULT
*/
public final void setIsolationLevel(int isolationLevel) {
if (!constants.getValues(PREFIX_ISOLATION).contains(isolationLevel)) {
throw new IllegalArgumentException("Only values of isolation constants allowed");
}
this.isolationLevel = isolationLevel;
}
@Override
public final int getIsolationLevel() {
return this.isolationLevel;
}
/**
* 将超时设置为应用秒数。
* 默认值是TIMEOUT_DEFAULT(-1)。
* 专门设计用于{@link #PROPAGATION_REQUIRED}或{@link #PROPAGATION_REQUIRES_NEW},因为它只适用于新启动的事务。
* 注意,不支持超时的事务管理器在给出除{@link #TIMEOUT_DEFAULT}之外的其他超时时将抛出异常。
*
* @see #TIMEOUT_DEFAULT
*/
public final void setTimeout(int timeout) {
if (timeout < TIMEOUT_DEFAULT) {
throw new IllegalArgumentException("Timeout must be a positive integer or TIMEOUT_DEFAULT");
}
this.timeout = timeout;
}
@Override
public final int getTimeout() {
return this.timeout;
}
/**
* 设置是否优化为只读事务。
* 默认设置是“false”。
* 只读标志适用于任何事务上下文,
* 无论是由实际的资源事务({@link #PROPAGATION_REQUIRED}/{@link #PROPAGATION_REQUIRES_NEW})支持,
* 还是在资源级别上进行非事务操作({@link #PROPAGATION_SUPPORTS})。
*
* 在后一种情况下,该标记将仅应用于应用程序内的托管资源,如Hibernate {@code Session}。
* 这只是作为实际事务子系统的提示;
* 它不一定会导致写访问尝试失败。
* 不能解释只读提示的事务管理器在请求只读事务时不会抛出异常。
*/
public final void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
@Override
public final boolean isReadOnly() {
return this.readOnly;
}
/**
* 设置此事务的名称。默认是没有的。
* 如果适用,这将作为事务名称显示在事务监视器中(例如,WebLogic的)。
*/
public final void setName(String name) {
this.name = name;
}
@Override
@Nullable
public final String getName() {
return this.name;
}
/**
* 这个实现比较{@code toString()}的结果。
* @see #toString()
*/
@Override
public boolean equals(Object other) {
return (this == other || (other instanceof TransactionDefinition && toString().equals(other.toString())));
}
/**
* 这个实现返回{@code toString()}的散列码。
* @see #toString()
*/
@Override
public int hashCode() {
return toString().hashCode();
}
/**
* 返回此事务定义的标识说明。
* 该格式与{@link org.springframework.transaction.interceptor.TransactionAttributeEditor}使用的格式相匹配,
* 以便能够将{@code toString}结果提供给
* {@link org.springframework.transaction.interceptor.TransactionAttribute}类型的bean属性。
* 必须在子类中重写正确的{@code =}和{@code hashCode}行为。
* 或者,{@link #equals}和{@link #hashCode}可以自己重写。
*
* @see #getDefinitionDescription()
* @see org.springframework.transaction.interceptor.TransactionAttributeEditor
*/
@Override
public String toString() {
return getDefinitionDescription().toString();
}
/**
* 返回此事务定义的标识说明。
* 可用于子类,以包含在它们的{@code toString()}结果中。
*/
protected final StringBuilder getDefinitionDescription() {
StringBuilder result = new StringBuilder();
result.append(constants.toCode(this.propagationBehavior, PREFIX_PROPAGATION));
result.append(',');
result.append(constants.toCode(this.isolationLevel, PREFIX_ISOLATION));
if (this.timeout != TIMEOUT_DEFAULT) {
result.append(',');
result.append(PREFIX_TIMEOUT).append(this.timeout);
}
if (this.readOnly) {
result.append(',');
result.append(READ_ONLY_MARKER);
}
return result;
}
}
来源:oschina
链接:https://my.oschina.net/u/3777515/blog/3164558