001/*
002 * Copyright 2012-2018 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.transaction;
018
019import java.time.Duration;
020import java.time.temporal.ChronoUnit;
021
022import org.springframework.boot.context.properties.ConfigurationProperties;
023import org.springframework.boot.convert.DurationUnit;
024import org.springframework.transaction.support.AbstractPlatformTransactionManager;
025
026/**
027 * Configuration properties that can be applied to an
028 * {@link AbstractPlatformTransactionManager}.
029 *
030 * @author Kazuki Shimizu
031 * @author Phillip Webb
032 * @since 1.5.0
033 */
034@ConfigurationProperties(prefix = "spring.transaction")
035public class TransactionProperties implements
036                PlatformTransactionManagerCustomizer<AbstractPlatformTransactionManager> {
037
038        /**
039         * Default transaction timeout. If a duration suffix is not specified, seconds will be
040         * used.
041         */
042        @DurationUnit(ChronoUnit.SECONDS)
043        private Duration defaultTimeout;
044
045        /**
046         * Whether to roll back on commit failures.
047         */
048        private Boolean rollbackOnCommitFailure;
049
050        public Duration getDefaultTimeout() {
051                return this.defaultTimeout;
052        }
053
054        public void setDefaultTimeout(Duration defaultTimeout) {
055                this.defaultTimeout = defaultTimeout;
056        }
057
058        public Boolean getRollbackOnCommitFailure() {
059                return this.rollbackOnCommitFailure;
060        }
061
062        public void setRollbackOnCommitFailure(Boolean rollbackOnCommitFailure) {
063                this.rollbackOnCommitFailure = rollbackOnCommitFailure;
064        }
065
066        @Override
067        public void customize(AbstractPlatformTransactionManager transactionManager) {
068                if (this.defaultTimeout != null) {
069                        transactionManager.setDefaultTimeout((int) this.defaultTimeout.getSeconds());
070                }
071                if (this.rollbackOnCommitFailure != null) {
072                        transactionManager.setRollbackOnCommitFailure(this.rollbackOnCommitFailure);
073                }
074        }
075
076}