001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.jooq; 018 019import org.jooq.TransactionContext; 020import org.jooq.TransactionProvider; 021 022import org.springframework.transaction.PlatformTransactionManager; 023import org.springframework.transaction.TransactionDefinition; 024import org.springframework.transaction.TransactionStatus; 025import org.springframework.transaction.support.DefaultTransactionDefinition; 026 027/** 028 * Allows Spring Transaction to be used with JOOQ. 029 * 030 * @author Lukas Eder 031 * @author Andreas Ahlenstorf 032 * @author Phillip Webb 033 * @since 1.5.10 034 */ 035public class SpringTransactionProvider implements TransactionProvider { 036 037 // Based on the jOOQ-spring-example from https://github.com/jOOQ/jOOQ 038 039 private final PlatformTransactionManager transactionManager; 040 041 public SpringTransactionProvider(PlatformTransactionManager transactionManager) { 042 this.transactionManager = transactionManager; 043 } 044 045 @Override 046 public void begin(TransactionContext context) { 047 TransactionDefinition definition = new DefaultTransactionDefinition( 048 TransactionDefinition.PROPAGATION_NESTED); 049 TransactionStatus status = this.transactionManager.getTransaction(definition); 050 context.transaction(new SpringTransaction(status)); 051 } 052 053 @Override 054 public void commit(TransactionContext ctx) { 055 this.transactionManager.commit(getTransactionStatus(ctx)); 056 } 057 058 @Override 059 public void rollback(TransactionContext ctx) { 060 this.transactionManager.rollback(getTransactionStatus(ctx)); 061 } 062 063 private TransactionStatus getTransactionStatus(TransactionContext ctx) { 064 SpringTransaction transaction = (SpringTransaction) ctx.transaction(); 065 return transaction.getTxStatus(); 066 } 067 068}