001/*
002 * Copyright 2006-2007 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 *      https://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.batch.support.transaction;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.springframework.transaction.TransactionDefinition;
023import org.springframework.transaction.TransactionException;
024import org.springframework.transaction.support.AbstractPlatformTransactionManager;
025import org.springframework.transaction.support.DefaultTransactionStatus;
026import org.springframework.transaction.support.TransactionSynchronizationManager;
027
028@SuppressWarnings("serial")
029public class ResourcelessTransactionManager extends AbstractPlatformTransactionManager {
030
031    @Override
032        protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
033                ((ResourcelessTransaction) transaction).begin();
034        }
035
036    @Override
037        protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
038                if (logger.isDebugEnabled()) {
039                        logger.debug("Committing resourceless transaction on [" + status.getTransaction() + "]");
040                }
041        }
042
043    @Override
044        protected Object doGetTransaction() throws TransactionException {
045                Object transaction = new ResourcelessTransaction();
046                List<Object> resources;
047                if (!TransactionSynchronizationManager.hasResource(this)) {
048                        resources = new ArrayList<Object>();
049                        TransactionSynchronizationManager.bindResource(this, resources);
050                }
051                else {
052                        @SuppressWarnings("unchecked")
053                        List<Object> stack = (List<Object>) TransactionSynchronizationManager.getResource(this);
054                        resources = stack;
055                }
056                resources.add(transaction);
057                return transaction;
058        }
059
060    @Override
061        protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
062                if (logger.isDebugEnabled()) {
063                        logger.debug("Rolling back resourceless transaction on [" + status.getTransaction() + "]");
064                }
065        }
066
067    @Override
068        protected boolean isExistingTransaction(Object transaction) throws TransactionException {
069                if (TransactionSynchronizationManager.hasResource(this)) {
070                        List<?> stack = (List<?>) TransactionSynchronizationManager.getResource(this);
071                        return stack.size() > 1;
072                }
073                return ((ResourcelessTransaction) transaction).isActive();
074        }
075
076    @Override
077        protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException {
078        }
079
080    @Override
081        protected void doCleanupAfterCompletion(Object transaction) {
082                List<?> resources = (List<?>) TransactionSynchronizationManager.getResource(this);
083                resources.clear();
084                TransactionSynchronizationManager.unbindResource(this);
085                ((ResourcelessTransaction) transaction).clear();
086        }
087
088        private static class ResourcelessTransaction {
089
090                private boolean active = false;
091
092                public boolean isActive() {
093                        return active;
094                }
095
096                public void begin() {
097                        active = true;
098                }
099
100                public void clear() {
101                        active = false;
102                }
103
104        }
105
106}