com.feingto.cloud.config.datasource.dynamic.transaction.DynamicDataSourceTransactionManager Maven / Gradle / Ivy
The newest version!
package com.feingto.cloud.config.datasource.dynamic.transaction;
import com.feingto.cloud.config.datasource.dynamic.support.DataSourceContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.NonNull;
import org.springframework.transaction.TransactionDefinition;
/**
* 多数据源读写分离事务管理
*
* @author longfei
*/
@Slf4j
public class DynamicDataSourceTransactionManager extends DataSourceTransactionManager {
private static final long serialVersionUID = 599566420697268981L;
@Override
protected void doBegin(@NonNull Object transaction, TransactionDefinition definition) {
if (definition.isReadOnly()) {
log.trace("Begin transaction with read-only mode");
DataSourceContext.setReadOnlyDataSource();
} else {
log.trace("Begin transaction with read-write mode");
DataSourceContext.setDbType(DataSourceContext.PRIMARY);
}
super.doBegin(transaction, definition);
}
@Override
protected void doCleanupAfterCompletion(@NonNull Object transaction) {
super.doCleanupAfterCompletion(transaction);
DataSourceContext.removeDbType();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy