|
|
@ -1,6 +1,7 @@
|
|
|
|
package cokr.xit.foundation.boot;
|
|
|
|
package cokr.xit.foundation.boot;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
import javax.sql.DataSource;
|
|
|
@ -13,7 +14,9 @@ import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
|
|
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
|
|
|
|
import org.springframework.transaction.TransactionDefinition;
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
|
|
|
|
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
|
|
|
|
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
|
|
|
|
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
|
|
|
|
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
|
|
|
|
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
|
|
|
|
import org.springframework.transaction.interceptor.TransactionInterceptor;
|
|
|
|
import org.springframework.transaction.interceptor.TransactionInterceptor;
|
|
|
@ -39,25 +42,31 @@ public class TransactionConfig {
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public TransactionInterceptor txAdvice() {
|
|
|
|
public TransactionInterceptor txAdvice() {
|
|
|
|
RuleBasedTransactionAttribute write = new RuleBasedTransactionAttribute();
|
|
|
|
RuleBasedTransactionAttribute read = new RuleBasedTransactionAttribute(
|
|
|
|
write.getRollbackRules().add(new RollbackRuleAttribute(Throwable.class));
|
|
|
|
TransactionDefinition.PROPAGATION_REQUIRED,
|
|
|
|
|
|
|
|
List.of(new RollbackRuleAttribute(Throwable.class))
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
read.setReadOnly(true);
|
|
|
|
|
|
|
|
RuleBasedTransactionAttribute write = new RuleBasedTransactionAttribute(
|
|
|
|
|
|
|
|
TransactionDefinition.PROPAGATION_REQUIRED,
|
|
|
|
|
|
|
|
List.of(new RollbackRuleAttribute(Throwable.class))
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
Properties txAttrs = new Properties();
|
|
|
|
NameMatchTransactionAttributeSource txAttrSrc = new NameMatchTransactionAttributeSource();
|
|
|
|
txAttrs.setProperty("*", write.toString());
|
|
|
|
txAttrSrc.setNameMap(Map.of(
|
|
|
|
|
|
|
|
"get*", read,
|
|
|
|
|
|
|
|
"*", write
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
TransactionInterceptor bean = new TransactionInterceptor();
|
|
|
|
TransactionInterceptor bean = new TransactionInterceptor();
|
|
|
|
bean.setTransactionManager(txManager());
|
|
|
|
bean.setTransactionManager(txManager());
|
|
|
|
bean.setTransactionAttributes(txAttrs);
|
|
|
|
bean.setTransactionAttributeSource(txAttrSrc);
|
|
|
|
return bean;
|
|
|
|
return bean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public Advisor txAdvisor() {
|
|
|
|
public Advisor txAdvisor() {
|
|
|
|
String expression = "execution(* cokr.xit..service.bean..*ServiceBean.*(..))";
|
|
|
|
String expression = "execution(* cokr.xit..service.bean.*ServiceBean.*(..))";
|
|
|
|
|
|
|
|
|
|
|
|
AspectJExpressionPointcut serviceMethod = new AspectJExpressionPointcut();
|
|
|
|
|
|
|
|
serviceMethod.setExpression(expression);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AspectJExpressionPointcut requiredTx = new AspectJExpressionPointcut();
|
|
|
|
AspectJExpressionPointcut requiredTx = new AspectJExpressionPointcut();
|
|
|
|
requiredTx.setExpression(expression);
|
|
|
|
requiredTx.setExpression(expression);
|
|
|
|
|
|
|
|
|
|
|
|