jooq事物处理

jooq的事物直接用spring的transactional是没用的(至少我没弄好),现在给出spring结合jooq的一种方案、

主要代码:
spring的配置


import org.jooq.Transaction;
import org.jooq.TransactionContext;
import org.jooq.TransactionProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @author Created by pangkunkun on 2018/5/30.
 */
public class SpringTransactionalProvider implements TransactionProvider {

    private static final Logger log = LoggerFactory.getLogger(SpringTransactionalProvider.class);

    @Autowired
    private DataSourceTransactionManager dstm;

    public SpringTransactionalProvider(DataSourceTransactionManager dstm){
        this.dstm = dstm;
    }

    @Override
    public void begin(TransactionContext var1){
        log.info("begin transaction");

        TransactionStatus ts = dstm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_NESTED));
        var1.transaction(new SpringTransaction(ts));
    }

    @Override
    public void commit(TransactionContext var1) {
        log.info("commit transaction");

        dstm.commit(((SpringTransaction)var1.transaction()).ts);
    }

    @Override
    public void rollback(TransactionContext var1) {
        log.info("rollback transaction");

        dstm.rollback(((SpringTransaction)var1.transaction()).ts);
    }


    class SpringTransaction implements Transaction {
        final TransactionStatus ts;

        SpringTransaction(TransactionStatus ts){
            this.ts = ts;
        }
    }
}

jooq的配置引入上面配置的spring的transactional

    private static final String DEFAULT_DB = "jooqdb";

    private DataSource dataSource;

    public DSLContext getDSLContent(String dbName){
        Settings settings = new Settings();
        settings.withRenderSchema(true)
                .withRenderMapping(
                        new RenderMapping()
                                .withSchemata(
                                        new MappedSchema()
                                                //jooq生成代码时的db
                                                .withInput(DEFAULT_DB)
                                                //现在要使用的db
                                                .withOutput(dbName)));
        Configuration conf = new DefaultConfiguration();
        TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(dataSource);
        DataSourceTransactionManager txMgr =  new DataSourceTransactionManager(dataSource);
        conf.set(new DataSourceConnectionProvider(proxy))
                .set(new SpringTransactionalProvider(txMgr))
                .set(settings)
                .set(SQLDialect.MYSQL);
        return DSL.using(conf);
    }


    public void updateUsers(){
        getDSLContent(DEFAULT_DB).transaction(configuration -> {
            for (int i = 0; i < 3; i++){
                if (i == 2){
                    int j = i/0;
                }
                getDSLContent(DEFAULT_DB).update(USER).set(USER.PASSWORD,(i+3)+"").execute();
            }
        });
    }

上面的

    if (i == 2){
        int j = i/0;
    }

这块代码是为了抛出异常验证事物回滚用的,正常使用的时候记得去掉。

完整代码git位置jooq-study 。

你可能感兴趣的:(SpringBoot,jooq)