kurt/kurt #15

Merged
cjm merged 2 commits from kurt/kurt into dev 4 weeks ago

@ -7,7 +7,7 @@ import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.*;
@ -17,79 +17,58 @@ import java.util.Collections;
import java.util.HashMap;
/**
* @ClassName : EgovConfigAppTransaction.java
* @ClassName : EgovConfigTransaction.java
* @Description : Transaction
*
* @author :
* @since : 2021. 7. 20
* @version : 1.0
*
* <pre>
* << (Modification Information) >>
*
*
* ------------- ------------ ---------------------
* 2021. 7. 20
* </pre>
*
*/
@Slf4j
@Configuration
public class EgovConfigTransaction {
@Autowired
DataSource dataSource;
@Autowired
DataSource dataSource;
@PostConstruct
public void init() {
log.info("Datasource type: {}", dataSource.getClass().getName());
}
@PostConstruct
public void init() {
log.info("Datasource type: {}", dataSource.getClass().getName());
}
@Bean
public DataSourceTransactionManager txManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
// ✅ 더 이상 DataSourceTransactionManager 직접 생성 X
// JPAConfig 에서 만든 "transactionManager" (JpaTransactionManager)를 주입받아 사용
// -------------------------------------------------------------
// TransactionAdvice 설정
// -------------------------------------------------------------
@Bean
public TransactionInterceptor txAdvice(PlatformTransactionManager transactionManager) {
TransactionInterceptor txAdvice = new TransactionInterceptor();
txAdvice.setTransactionManager(transactionManager);
txAdvice.setTransactionAttributeSource(getNameMatchTransactionAttributeSource());
return txAdvice;
}
@Bean
public TransactionInterceptor txAdvice(DataSourceTransactionManager txManager) {
TransactionInterceptor txAdvice = new TransactionInterceptor();
txAdvice.setTransactionManager(txManager);
txAdvice.setTransactionAttributeSource(getNameMatchTransactionAttributeSource());
return txAdvice;
}
private NameMatchTransactionAttributeSource getNameMatchTransactionAttributeSource() {
NameMatchTransactionAttributeSource txAttributeSource = new NameMatchTransactionAttributeSource();
txAttributeSource.setNameMap(getRuleBasedTxAttributeMap());
return txAttributeSource;
}
private NameMatchTransactionAttributeSource getNameMatchTransactionAttributeSource() {
NameMatchTransactionAttributeSource txAttributeSource = new NameMatchTransactionAttributeSource();
txAttributeSource.setNameMap(getRuleBasedTxAttributeMap());
return txAttributeSource;
}
private HashMap<String, TransactionAttribute> getRuleBasedTxAttributeMap() {
HashMap<String, TransactionAttribute> txMethods = new HashMap<>();
private HashMap<String, TransactionAttribute> getRuleBasedTxAttributeMap() {
HashMap<String, TransactionAttribute> txMethods = new HashMap<String, TransactionAttribute>();
RuleBasedTransactionAttribute txAttribute = new RuleBasedTransactionAttribute();
txAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
txAttribute.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
RuleBasedTransactionAttribute txAttribute = new RuleBasedTransactionAttribute();
txAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
txAttribute.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
txMethods.put("*", txAttribute);
// 이름 패턴별 트랜잭션 속성 설정
// 여기서는 모든 메서드에 동일 룰 적용
txMethods.put("*", txAttribute);
return txMethods;
}
return txMethods;
}
// -------------------------------------------------------------
// TransactionAdvisor 설정
// -------------------------------------------------------------
@Bean
public Advisor txAdvisor(DataSourceTransactionManager txManager) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(
"execution(* go.kr.project..impl.*Impl.*(..)) or execution(* egovframework.com..*Impl.*(..))");
return new DefaultPointcutAdvisor(pointcut, txAdvice(txManager));
}
@Bean
public Advisor txAdvisor(PlatformTransactionManager transactionManager) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(
"execution(* go.kr.project..impl.*Impl.*(..)) or execution(* egovframework.com..*Impl.*(..))"
);
return new DefaultPointcutAdvisor(pointcut, txAdvice(transactionManager));
}
}

@ -3,9 +3,8 @@ package egovframework.config.JPAConf;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@ -17,22 +16,28 @@ public class JPAConfig {
@PersistenceContext
private EntityManager em;
/** QueryDSL용 JPAQueryFactory */
@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(em);
}
/**
*
*/
@Bean(name = "transactionManager")
@Primary
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
// @Bean
// public JpaVendorAdapter jpaVendorAdapter() {
// HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
// adapter.setShowSql(false);
// adapter.setGenerateDdl(false);
// adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
// return adapter;
// }
// 필요하면 나중에 다시 켜서 쓰면 됨
// @Bean
// public JpaVendorAdapter jpaVendorAdapter() {
// HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
// adapter.setShowSql(false);
// adapter.setGenerateDdl(false);
// adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
// return adapter;
// }
}

Loading…
Cancel
Save