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 javax.transaction.Status; 020import javax.transaction.TransactionManager; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024 025import org.springframework.transaction.TransactionSystemException; 026import org.springframework.transaction.support.TransactionSynchronizationManager; 027 028/** 029 * Helper class for registering a transaction synchronization for closing 030 * a LobCreator, preferring Spring transaction synchronization and falling 031 * back to plain JTA transaction synchronization. 032 * 033 * @author Juergen Hoeller 034 * @since 2.0 035 * @see SpringLobCreatorSynchronization 036 * @see org.springframework.transaction.support.TransactionSynchronizationManager 037 * @see JtaLobCreatorSynchronization 038 * @see javax.transaction.Transaction#registerSynchronization 039 */ 040public abstract class LobCreatorUtils { 041 042 private static final Log logger = LogFactory.getLog(LobCreatorUtils.class); 043 044 045 /** 046 * Register a transaction synchronization for closing the given LobCreator, 047 * preferring Spring transaction synchronization and falling back to 048 * plain JTA transaction synchronization. 049 * @param lobCreator the LobCreator to close after transaction completion 050 * @param jtaTransactionManager the JTA TransactionManager to fall back to 051 * when no Spring transaction synchronization is active (may be {@code null}) 052 * @throws IllegalStateException if there is neither active Spring transaction 053 * synchronization nor active JTA transaction synchronization 054 */ 055 public static void registerTransactionSynchronization( 056 LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException { 057 058 if (TransactionSynchronizationManager.isSynchronizationActive()) { 059 logger.debug("Registering Spring transaction synchronization for LobCreator"); 060 TransactionSynchronizationManager.registerSynchronization( 061 new SpringLobCreatorSynchronization(lobCreator)); 062 } 063 else { 064 if (jtaTransactionManager != null) { 065 try { 066 int jtaStatus = jtaTransactionManager.getStatus(); 067 if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) { 068 logger.debug("Registering JTA transaction synchronization for LobCreator"); 069 jtaTransactionManager.getTransaction().registerSynchronization( 070 new JtaLobCreatorSynchronization(lobCreator)); 071 return; 072 } 073 } 074 catch (Throwable ex) { 075 throw new TransactionSystemException( 076 "Could not register synchronization with JTA TransactionManager", ex); 077 } 078 } 079 throw new IllegalStateException("Active Spring transaction synchronization or active " + 080 "JTA transaction with specified [javax.transaction.TransactionManager] required"); 081 } 082 } 083 084}