001/*
002 * Copyright 2002-2012 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.jdbc.support.lob;
018
019import org.springframework.jdbc.datasource.DataSourceUtils;
020import org.springframework.transaction.support.TransactionSynchronizationAdapter;
021import org.springframework.util.Assert;
022
023/**
024 * Callback for resource cleanup at the end of a Spring transaction.
025 * Invokes {@code LobCreator.close()} to clean up temporary LOBs
026 * that might have been created.
027 *
028 * @author Juergen Hoeller
029 * @since 2.0
030 * @see LobCreator#close()
031 */
032public class SpringLobCreatorSynchronization extends TransactionSynchronizationAdapter {
033
034        /**
035         * Order value for TransactionSynchronization objects that clean up LobCreators.
036         * Return CONNECTION_SYNCHRONIZATION_ORDER - 200 to execute LobCreator cleanup
037         * before Hibernate Session (- 100) and JDBC Connection cleanup, if any.
038         * @see org.springframework.jdbc.datasource.DataSourceUtils#CONNECTION_SYNCHRONIZATION_ORDER
039         */
040        public static final int LOB_CREATOR_SYNCHRONIZATION_ORDER =
041                        DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 200;
042
043
044        private final LobCreator lobCreator;
045
046        private boolean beforeCompletionCalled = false;
047
048
049        /**
050         * Create a SpringLobCreatorSynchronization for the given LobCreator.
051         * @param lobCreator the LobCreator to close after transaction completion
052         */
053        public SpringLobCreatorSynchronization(LobCreator lobCreator) {
054                Assert.notNull(lobCreator, "LobCreator must not be null");
055                this.lobCreator = lobCreator;
056        }
057
058        @Override
059        public int getOrder() {
060                return LOB_CREATOR_SYNCHRONIZATION_ORDER;
061        }
062
063
064        @Override
065        public void beforeCompletion() {
066                // Close the LobCreator early if possible, to avoid issues with strict JTA
067                // implementations that issue warnings when doing JDBC operations after
068                // transaction completion.
069                this.beforeCompletionCalled = true;
070                this.lobCreator.close();
071        }
072
073        @Override
074        public void afterCompletion(int status) {
075                if (!this.beforeCompletionCalled) {
076                        // beforeCompletion not called before (probably because of flushing on commit
077                        // in the transaction manager, after the chain of beforeCompletion calls).
078                        // Close the LobCreator here.
079                        this.lobCreator.close();
080                }
081        }
082
083}