All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.cloud.config.datasource.dynamic.transaction.JpaDataSourceTransactionManager 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.lang.NonNull;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.TransactionDefinition;

/**
 * 多数据源读写分离事务管理 - JPA
 *
 * @author longfei
 */
@Slf4j
public class JpaDataSourceTransactionManager extends JpaTransactionManager {
    private static final long serialVersionUID = 5409409243651160022L;

    @Override
    protected void doBegin(@NonNull Object transaction, TransactionDefinition definition) {
        if (definition.isReadOnly()) {
            log.trace("Begin jpa transaction with read-only mode");
            DataSourceContext.setReadOnlyDataSource();
        } else {
            log.trace("Begin jpa 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