Java Code Examples for org.springframework.transaction.interceptor.DefaultTransactionAttribute#setReadOnly()

The following examples show how to use org.springframework.transaction.interceptor.DefaultTransactionAttribute#setReadOnly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionAdviceConfig.java    From SpringBoot2.0 with Apache License 2.0 6 votes vote down vote up
@Bean
public TransactionInterceptor txAdvice() {

    DefaultTransactionAttribute required = new DefaultTransactionAttribute();
    required.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

    DefaultTransactionAttribute readonly = new DefaultTransactionAttribute();
    readonly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    readonly.setReadOnly(true);

    NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
    source.addTransactionalMethod("add*", required);
    source.addTransactionalMethod("save*", required);
    source.addTransactionalMethod("delete*", required);
    source.addTransactionalMethod("update*", required);
    source.addTransactionalMethod("exec*", required);
    source.addTransactionalMethod("set*", required);
    source.addTransactionalMethod("do*", required);

    source.addTransactionalMethod("get*", readonly);
    source.addTransactionalMethod("query*", readonly);
    source.addTransactionalMethod("find*", readonly);
    source.addTransactionalMethod("list*", readonly);
    source.addTransactionalMethod("count*", readonly);
    return new TransactionInterceptor(transactionManager, source);
}
 
Example 2
Source File: ProgrammaticTransactionConfig.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
public TransactionInterceptor defaultTransactionInterceptor(PlatformTransactionManager transactionManager,
                                                            List<Class<? extends Exception>> additionalRollbackRuleExceptions) {
    TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
    Properties transactionAttributes = new Properties();

    List<RollbackRuleAttribute> rollbackRules = Lists.newArrayList();
    rollbackRules.add(new RollbackRuleAttribute(Exception.class));
    //回滚异常
    if (additionalRollbackRuleExceptions != null && !additionalRollbackRuleExceptions.isEmpty()) {
        for (Class<? extends Exception> clazz : additionalRollbackRuleExceptions) {
            rollbackRules.add(new RollbackRuleAttribute(clazz));
        }
    }
    DefaultTransactionAttribute readOnlyTransactionAttributes =
            new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);
    readOnlyTransactionAttributes.setReadOnly(true);

    RuleBasedTransactionAttribute writeTransactionAttributes =
            new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, rollbackRules);

    String readOnlyTransactionAttributesDefinition = readOnlyTransactionAttributes.toString();
    String writeTransactionAttributesDefinition = writeTransactionAttributes.toString();
    // read-only
    transactionAttributes.setProperty("is*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("has*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("get*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("list*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("search*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("find*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("count*", readOnlyTransactionAttributesDefinition);
    // write et rollback-rule
    transactionAttributes.setProperty("*", writeTransactionAttributesDefinition);

    transactionInterceptor.setTransactionAttributes(transactionAttributes);
    transactionInterceptor.setTransactionManager(this.transactionManager);
    return transactionInterceptor;
}